You are here

function nodeorder_node_presave in Node Order 8

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

Implements hook_node_presave().

Related topics

File

./nodeorder.module, line 167
Nodeorder module.

Code

function nodeorder_node_presave(NodeInterface $node) {

  /** @var \Drupal\nodeorder\NodeOrderManagerInterface $nodeorder_manager */
  $nodeorder_manager = \Drupal::service('nodeorder.manager');
  if ($nodeorder_manager
    ->canBeOrdered($node)) {

    // Store the old node orders for use in nodeorder_node_update().
    $node->nodeorder = [];

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