You are here

function nodeorder_node_presave in Node Order 7

Same name and namespace in other branches
  1. 8 nodeorder.module \nodeorder_node_presave()

Implements hook_node_presave().

File

./nodeorder.module, line 725
Nodeorder module.

Code

function nodeorder_node_presave($node) {

  // @todo: As far as I can tell this hook has zero impact. All the real saving
  // happens on hook_node_insert, which is triggered after this. Once a node is
  // created then hook_node_load ensures that the needed array is present on the
  // future editing of a node.
  if (nodeorder_can_be_ordered($node)) {

    // This should only be triggered on node creation.
    if (!isset($node->nodeorder)) {
      $node->nodeorder = array();

      // When a node is created, store an element called 'nodeorder' that
      // contains an associative array of tid to weight.
      $query = db_select('taxonomy_index', 'ti')
        ->fields('ti', array(
        'tid',
        'weight',
      ))
        ->condition('nid', $node->nid);
      $result = $query
        ->execute();
      foreach ($result as $term_node) {
        $node->nodeorder[$term_node->tid] = $term_node->weight;
      }
    }
  }
}