You are here

public function MenuTreeStorage::getRootPathIds in Drupal 9

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

Returns all the IDs that represent the path to the root of the tree.

array(
  'p1' => 1,
  'p2' => 6,
  'p3' => 8,
  'p4' => 0,
  'p5' => 0,
  'p6' => 0,
  'p7' => 0,
  'p8' => 0,
  'p9' => 0,
);

Parameters

string $id: A menu link ID.

Return value

array An associative array of IDs with keys equal to values that represents the path from the given ID to the root of the tree. If $id is an ID that exists, the returned array will at least include it. An empty array is returned if the ID does not exist in the storage. An example $id (8) with two parents (1, 6) looks like the following:

Overrides MenuTreeStorageInterface::getRootPathIds

File

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

Class

MenuTreeStorage
Provides a menu tree storage using the database.

Namespace

Drupal\Core\Menu

Code

public function getRootPathIds($id) {
  $subquery = $this->connection
    ->select($this->table, NULL, $this->options);

  // @todo Consider making this dynamic based on static::MAX_DEPTH or from the
  //   schema if that is generated using static::MAX_DEPTH.
  //   https://www.drupal.org/node/2302043
  $subquery
    ->fields($this->table, [
    'p1',
    'p2',
    'p3',
    'p4',
    'p5',
    'p6',
    'p7',
    'p8',
    'p9',
  ]);
  $subquery
    ->condition('id', $id);
  $result = current($subquery
    ->execute()
    ->fetchAll(\PDO::FETCH_ASSOC));
  $ids = array_filter($result);
  if ($ids) {
    $query = $this->connection
      ->select($this->table, NULL, $this->options);
    $query
      ->fields($this->table, [
      'id',
    ]);
    $query
      ->orderBy('depth', 'DESC');
    $query
      ->condition('mlid', $ids, 'IN');

    // @todo Cache this result in memory if we find it is being used more
    //   than once per page load. https://www.drupal.org/node/2302185
    return $this
      ->safeExecuteSelect($query)
      ->fetchAllKeyed(0, 0);
  }
  return [];
}