Updates the children of a menu link that is being moved.
The menu name, parents (p1 - p6), and depth are updated for all children of the link, and the has_children status of the previous parent is updated.
\Drupal\Core\Entity\EntityInterface $entity: A menu link entity.
protected function moveChildren(EntityInterface $entity) {
$query = $this->database
->update($this->entityInfo['base_table']);
$query
->fields(array(
'menu_name' => $entity->menu_name,
));
$p = 'p1';
$expressions = array();
for ($i = 1; $i <= $entity->depth; $p = 'p' . ++$i) {
$expressions[] = array(
$p,
":p_{$i}",
array(
":p_{$i}" => $entity->{$p},
),
);
}
$j = $entity->original->depth + 1;
while ($i <= MENU_MAX_DEPTH && $j <= MENU_MAX_DEPTH) {
$expressions[] = array(
'p' . $i++,
'p' . $j++,
array(),
);
}
while ($i <= MENU_MAX_DEPTH) {
$expressions[] = array(
'p' . $i++,
0,
array(),
);
}
$shift = $entity->depth - $entity->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', $entity->original->menu_name);
$p = 'p1';
for ($i = 1; $i <= MENU_MAX_DEPTH && $entity->original->{$p}; $p = 'p' . ++$i) {
$query
->condition($p, $entity->original->{$p});
}
$query
->execute();
// Check the has_children status of the parent, while excluding this item.
$this
->updateParentalStatus($entity->original, TRUE);
}