You are here

protected function MenuTreeStorage::moveChildren in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/lib/Drupal/Core/Menu/MenuTreeStorage.php \Drupal\Core\Menu\MenuTreeStorage::moveChildren()

Re-parents a link's children when the link itself is moved.

Parameters

array $fields: The changed menu link.

array $original: The original menu link.

1 call to MenuTreeStorage::moveChildren()
MenuTreeStorage::preSave in core/lib/Drupal/Core/Menu/MenuTreeStorage.php
Fills in all the fields the database save needs, using the link definition.

File

core/lib/Drupal/Core/Menu/MenuTreeStorage.php, line 517
Contains \Drupal\Core\Menu\MenuTreeStorage.

Class

MenuTreeStorage
Provides a menu tree storage using the database.

Namespace

Drupal\Core\Menu

Code

protected function moveChildren($fields, $original) {
  $query = $this->connection
    ->update($this->table, $this->options);
  $query
    ->fields(array(
    'menu_name' => $fields['menu_name'],
  ));
  $expressions = array();
  for ($i = 1; $i <= $fields['depth']; $i++) {
    $expressions[] = array(
      "p{$i}",
      ":p_{$i}",
      array(
        ":p_{$i}" => $fields["p{$i}"],
      ),
    );
  }
  $j = $original['depth'] + 1;
  while ($i <= $this
    ->maxDepth() && $j <= $this
    ->maxDepth()) {
    $expressions[] = array(
      'p' . $i++,
      'p' . $j++,
      array(),
    );
  }
  while ($i <= $this
    ->maxDepth()) {
    $expressions[] = array(
      'p' . $i++,
      0,
      array(),
    );
  }
  $shift = $fields['depth'] - $original['depth'];
  if ($shift > 0) {

    // The order of expressions must be reversed so the new values don't
    // overwrite the old ones before they can be used because "Single-table
    // UPDATE assignments are generally evaluated from left to right".
    // @see http://dev.mysql.com/doc/refman/5.0/en/update.html
    $expressions = array_reverse($expressions);
  }
  foreach ($expressions as $expression) {
    $query
      ->expression($expression[0], $expression[1], $expression[2]);
  }
  $query
    ->expression('depth', 'depth + :depth', array(
    ':depth' => $shift,
  ));
  $query
    ->condition('menu_name', $original['menu_name']);
  for ($i = 1; $i <= $this
    ->maxDepth() && $original["p{$i}"]; $i++) {
    $query
      ->condition("p{$i}", $original["p{$i}"]);
  }
  $query
    ->execute();
}