You are here

protected function MenuTreeStorage::moveChildren in Drupal 8

Same name and namespace in other branches
  1. 9 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 529

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([
    'menu_name' => $fields['menu_name'],
  ]);
  $expressions = [];
  for ($i = 1; $i <= $fields['depth']; $i++) {
    $expressions[] = [
      "p{$i}",
      ":p_{$i}",
      [
        ":p_{$i}" => $fields["p{$i}"],
      ],
    ];
  }
  $j = $original['depth'] + 1;
  while ($i <= $this
    ->maxDepth() && $j <= $this
    ->maxDepth()) {
    $expressions[] = [
      'p' . $i++,
      'p' . $j++,
      [],
    ];
  }
  while ($i <= $this
    ->maxDepth()) {
    $expressions[] = [
      'p' . $i++,
      0,
      [],
    ];
  }
  $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', [
    ':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();
}