You are here

function nodeorder_node_update in Node Order 8

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

Implements hook_node_update().

Handle the weights, which were reset on rebuild of the taxonomy.

Related topics

File

./nodeorder.module, line 222
Nodeorder module.

Code

function nodeorder_node_update(NodeInterface $node) {

  /** @var \Drupal\nodeorder\NodeOrderManagerInterface $nodeorder_manager */
  $nodeorder_manager = \Drupal::service('nodeorder.manager');
  if (!$nodeorder_manager
    ->canBeOrdered($node)) {
    return;
  }
  $tids = $nodeorder_manager
    ->getOrderableTids($node, TRUE);
  $old_tids = $node->nodeorder;
  foreach ($tids as $tid) {

    // Restore weight of unchanged terms, or leave as is if zero.
    if (isset($old_tids[$tid])) {
      $old_weight = $old_tids[$tid];
      unset($old_tids[$tid]);
      if (!$old_weight) {
        continue;
      }
      \Drupal::database()
        ->update('taxonomy_index')
        ->fields([
        'weight' => $old_weight,
      ])
        ->condition('nid', $node
        ->id())
        ->condition('tid', $tid)
        ->execute();
    }
    else {
      $nodeorder_manager
        ->addToList($node, $tid);
    }
  }

  // Handle lists in which the node is removed.
  // Note that the old tids are at this point only the ones that were not
  // updated, the others were dropped when restoring above.
  foreach ($old_tids as $tid => $weight) {
    $nodeorder_manager
      ->handleListsDecrease($tid);
  }
}