Small node CRUD system for nodes of type "item".

Public

The "item" has only the title and it is created from a post

Get raw version
php
  1. function api_add_item_save() {
  2. if(!empty($_POST['itemName'])) {
  3. global $user;
  4. $node = new stdClass(); // We create a new node object
  5. $node->type = "item"; // Or any other content type you want
  6. $node->title = $_POST['itemName'];
  7. $node->language = LANGUAGE_NONE; // Or any language code if Locale module is enabled. More on this below *
  8. node_object_prepare($node); // Set some default values.
  9. $node->uid = $user->uid; // Or any id you wish
  10. $node = node_submit($node); // Prepare node for a submit
  11. node_save($node);
  12. } else {
  13. drupal_set_message('Error. Please try again.');
  14. }
  15. $destination = 'transfer/new/items';
  16. drupal_goto($destination);
  17. }

User entity query to pull out all the nodes of type "item" that were created by the logged in user.

Get raw version
php
  1. function get_my_items() {
  2. global $user;
  3. $output = NULL;
  4. $query = new EntityFieldQuery();
  5. $query->entityCondition('entity_type', 'node')
  6. ->entityCondition('bundle', 'item')
  7. ->propertyCondition('uid', $user->uid)
  8. ->propertyCondition('status', 1)
  9. ->addMetaData('account', $user); // Run the query as logged user. or user_load(1); as admin.
  10. $my_items = $query->execute();
  11.  
  12. $output = NULL;
  13. if(!empty($my_items)) {
  14. foreach ($my_items['node'] as $key => $value) {
  15. $output .= item_template($key);
  16. }
  17. }
  18.  
  19. //print_r($my_items['node']);
  20. return $output;
  21. }

Display the node of type "item" using a simple function (escapes the tpl system).

Get raw version
php
  1. function item_template_confirm($item_nid, $count) {
  2. $node = node_load($item_nid);
  3. $output ='
  4. <div class="row row-together">
  5. <div class="col-sm-12">
  6. <div class="well card card-white well-together">
  7. <div class="pull-right text-right">
  8. <span>' . $count . 'x</span>
  9. </div>
  10. <h4>
  11. ' . $node->title . '
  12. </h4>
  13. </div>
  14. </div>
  15. </div>
  16. ';
  17. return $output;
  18. }

Use the node.tpl.php rather than custom output function

Get raw version
php
  1. function output_node_render($nid) {
  2. $node = node_load($nid);
  3. $node_view = node_view($node);
  4. $output = drupal_render($node_view);
  5. return $output;
  6. }