You are here

protected function MenuTreeStorage::loadLinks in Drupal 8

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

Loads links in the given menu, according to the given tree parameters.

Parameters

string $menu_name: A menu name.

\Drupal\Core\Menu\MenuTreeParameters $parameters: The parameters to determine which menu links to be loaded into a tree. This method will set the absolute minimum depth, which is used in MenuTreeStorage::doBuildTreeData().

Return value

array A flat array of menu links that are part of the menu. Each array element is an associative array of information about the menu link, containing the fields from the {menu_tree} table. This array must be ordered depth-first.

2 calls to MenuTreeStorage::loadLinks()
MenuTreeStorage::loadAllChildren in core/lib/Drupal/Core/Menu/MenuTreeStorage.php
Loads all the enabled menu links that are below the given ID.
MenuTreeStorage::loadTreeData in core/lib/Drupal/Core/Menu/MenuTreeStorage.php
Loads a menu link tree from the storage.

File

core/lib/Drupal/Core/Menu/MenuTreeStorage.php, line 885

Class

MenuTreeStorage
Provides a menu tree storage using the database.

Namespace

Drupal\Core\Menu

Code

protected function loadLinks($menu_name, MenuTreeParameters $parameters) {
  $query = $this->connection
    ->select($this->table, $this->options);
  $query
    ->fields($this->table);

  // Allow a custom root to be specified for loading a menu link tree. If
  // omitted, the default root (i.e. the actual root, '') is used.
  if ($parameters->root !== '') {
    $root = $this
      ->loadFull($parameters->root);

    // If the custom root does not exist, we cannot load the links below it.
    if (!$root) {
      return [];
    }

    // When specifying a custom root, we only want to find links whose
    // parent IDs match that of the root; that's how we ignore the rest of the
    // tree. In other words: we exclude everything unreachable from the
    // custom root.
    for ($i = 1; $i <= $root['depth']; $i++) {
      $query
        ->condition("p{$i}", $root["p{$i}"]);
    }

    // When specifying a custom root, the menu is determined by that root.
    $menu_name = $root['menu_name'];

    // If the custom root exists, then we must rewrite some of our
    // parameters; parameters are relative to the root (default or custom),
    // but the queries require absolute numbers, so adjust correspondingly.
    if (isset($parameters->minDepth)) {
      $parameters->minDepth += $root['depth'];
    }
    else {
      $parameters->minDepth = $root['depth'];
    }
    if (isset($parameters->maxDepth)) {
      $parameters->maxDepth += $root['depth'];
    }
  }

  // If no minimum depth is specified, then set the actual minimum depth,
  // depending on the root.
  if (!isset($parameters->minDepth)) {
    if ($parameters->root !== '' && $root) {
      $parameters->minDepth = $root['depth'];
    }
    else {
      $parameters->minDepth = 1;
    }
  }
  for ($i = 1; $i <= $this
    ->maxDepth(); $i++) {
    $query
      ->orderBy('p' . $i, 'ASC');
  }
  $query
    ->condition('menu_name', $menu_name);
  if (!empty($parameters->expandedParents)) {
    $query
      ->condition('parent', $parameters->expandedParents, 'IN');
  }
  if (isset($parameters->minDepth) && $parameters->minDepth > 1) {
    $query
      ->condition('depth', $parameters->minDepth, '>=');
  }
  if (isset($parameters->maxDepth)) {
    $query
      ->condition('depth', $parameters->maxDepth, '<=');
  }

  // Add custom query conditions, if any were passed.
  if (!empty($parameters->conditions)) {

    // Only allow conditions that are testing definition fields.
    $parameters->conditions = array_intersect_key($parameters->conditions, array_flip($this
      ->definitionFields()));
    $serialized_fields = $this
      ->serializedFields();
    foreach ($parameters->conditions as $column => $value) {
      if (is_array($value)) {
        $operator = $value[1];
        $value = $value[0];
      }
      else {
        $operator = '=';
      }
      if (in_array($column, $serialized_fields)) {
        $value = serialize($value);
      }
      $query
        ->condition($column, $value, $operator);
    }
  }
  $links = $this
    ->safeExecuteSelect($query)
    ->fetchAllAssoc('id', \PDO::FETCH_ASSOC);
  return $links;
}