public function MenuLinkStorageController::findChildrenRelativeDepth

Finds the depth of an item's children relative to its depth.

For example, if the item has a depth of 2 and the maximum of any child in the menu link tree is 5, the relative depth is 3.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: A menu link entity.

Return value

int The relative depth, or zero.

1 call to MenuLinkStorageController::findChildrenRelativeDepth()
MenuLinkStorageController::preSave in drupal/core/modules/menu_link/lib/Drupal/menu_link/MenuLinkStorageController.php
Overrides DatabaseStorageController::preSave().

File

drupal/core/modules/menu_link/lib/Drupal/menu_link/MenuLinkStorageController.php, line 574
Contains \Drupal\menu_link\MenuLinkStorageController.

Class

MenuLinkStorageController
Controller class for menu links.

Namespace

Drupal\menu_link

Code

public function findChildrenRelativeDepth(EntityInterface $entity) {

  // @todo Since all we need is a specific field from the base table, does it
  // make sense to convert to EFQ?
  $query = $this->database
    ->select('menu_links');
  $query
    ->addField('menu_links', 'depth');
  $query
    ->condition('menu_name', $entity->menu_name);
  $query
    ->orderBy('depth', 'DESC');
  $query
    ->range(0, 1);
  $i = 1;
  $p = 'p1';
  while ($i <= MENU_MAX_DEPTH && $entity->{$p}) {
    $query
      ->condition($p, $entity->{$p});
    $p = 'p' . ++$i;
  }
  $max_depth = $query
    ->execute()
    ->fetchField();
  return $max_depth > $entity->depth ? $max_depth - $entity->depth : 0;
}