You are here

protected function MongodbMenuTreeStorage::loadLinks in MongoDB 8

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.

Overrides MenuTreeStorage::loadLinks

File

src/MongodbMenuTreeStorage.php, line 57
Contains \Drupal\mongodb\MongodbMenuTreeStorage .

Class

MongodbMenuTreeStorage

Namespace

Drupal\mongodb

Code

protected function loadLinks($menu_name, MenuTreeParameters $parameters) {
  $query = [];

  // 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 array();
    }

    // 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.
    $query['value.p'] = new \MongoRegex('/^' . preg_quote($root['p'], '/') . '/');

    // 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 !== '' && !empty($root)) {
      $parameters->minDepth = $root['depth'];
    }
    else {
      $parameters->minDepth = 1;
    }
  }
  $query['value.menu_name'] = $menu_name;
  if (!empty($parameters->expandedParents)) {
    $query['value.parent']['$in'] = array_values($parameters->expandedParents);
  }
  if (isset($parameters->minDepth) && $parameters->minDepth > 1) {
    $query['value.depth']['$gte'] = $parameters->minDepth;
  }
  if (isset($parameters->maxDepth)) {
    $query['value.depth']['$lte'] = $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()));
    foreach ($parameters->conditions as $column => $value) {
      $query["value.{$column}"] = $value;
    }
  }
  $links = [];
  foreach ($this
    ->mongoCollection()
    ->find($query)
    ->sort([
    'value.p' => 1,
  ]) as $link) {
    $links[$link['value']['id']] = $link['value'];
  }
  return $links;
}