interface MenuTreeStorageInterface (View source)

Defines an interface for storing a menu tree containing menu link IDs.

The tree contains a hierarchy of menu links which have an ID as well as a route name or external URL.

Methods

int
maxDepth()

The maximum depth of tree the storage implementation supports.

resetDefinitions()

Clears all definitions cached in memory.

rebuild(array $definitions)

Rebuilds the stored menu link definitions.

array|false
load(string $id)

Loads a menu link plugin definition from the storage.

array
loadMultiple(array $ids)

Loads multiple plugin definitions from the storage.

array
loadByProperties(array $properties)

Loads multiple plugin definitions from the storage based on properties.

array
loadByRoute(string $route_name, array $route_parameters = [], string $menu_name = NULL)

Loads multiple plugin definitions from the storage based on route.

array
save(array $definition)

Saves a plugin definition to the storage.

delete(string $id)

Deletes a menu link definition from the storage.

array
loadTreeData(string $menu_name, MenuTreeParameters $parameters)

Loads a menu link tree from the storage.

array
loadAllChildren(string $id, int $max_relative_depth = NULL)

Loads all the enabled menu links that are below the given ID.

array
getAllChildIds(string $id)

Loads all the IDs for menu links that are below the given ID.

array
loadSubtreeData(string $id, int $max_relative_depth = NULL)

Loads a subtree rooted by the given ID.

array
getRootPathIds(string $id)

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

array
getExpanded(string $menu_name, array $parents)

Finds expanded links in a menu given a set of possible parents.

int
getSubtreeHeight(string $id)

Finds the height of a subtree rooted by the given ID.

bool
menuNameInUse(string $menu_name)

Determines whether a specific menu name is used in the tree.

array
getMenuNames()

Returns the used menu names in the tree storage.

int
countMenuLinks(string $menu_name = NULL)

Counts the total number of menu links in one menu or all menus.

Details

int maxDepth()

The maximum depth of tree the storage implementation supports.

Return Value

int

The maximum depth.

resetDefinitions()

Clears all definitions cached in memory.

rebuild(array $definitions)

Rebuilds the stored menu link definitions.

Links that saved by passing definitions into this method must be included on all future calls, or they will be purged. This allows for automatic cleanup e.g. when modules are uninstalled.

Parameters

array $definitions

The new menu link definitions.

array|false load(string $id)

Loads a menu link plugin definition from the storage.

Parameters

string $id

The menu link plugin ID.

Return Value

array|false

The plugin definition, or FALSE if no definition was found for the ID.

array loadMultiple(array $ids)

Loads multiple plugin definitions from the storage.

Parameters

array $ids

An array of plugin IDs.

Return Value

array

An array of plugin definition arrays keyed by plugin ID, which are the actual definitions after the loadMultiple() including all those plugins from $ids.

array loadByProperties(array $properties)

Loads multiple plugin definitions from the storage based on properties.

Parameters

array $properties

The properties to filter by.

Return Value

array

An array of menu link definition arrays.

Exceptions

InvalidArgumentException

array loadByRoute(string $route_name, array $route_parameters = [], string $menu_name = NULL)

Loads multiple plugin definitions from the storage based on route.

Parameters

string $route_name

The route name.

array $route_parameters

(optional) The route parameters. Defaults to an empty array.

string $menu_name

(optional) Restricts the found links to just those in the named menu.

Return Value

array

An array of menu link definitions keyed by ID and ordered by depth.

array save(array $definition)

Saves a plugin definition to the storage.

Parameters

array $definition

A definition for a \Drupal\Core\Menu\MenuLinkInterface plugin.

Return Value

array

The menu names affected by the save operation. This will be one menu name if the link is saved to the sane menu, or two if it is saved to a new menu.

Exceptions

Exception
PluginException

delete(string $id)

Deletes a menu link definition from the storage.

Parameters

string $id

The menu link plugin ID.

array loadTreeData(string $menu_name, MenuTreeParameters $parameters)

Loads a menu link tree from the storage.

This function may be used build the data for a menu tree only, for example to further massage the data manually before further processing happens. MenuLinkTree::checkAccess() needs to be invoked afterwards.

The tree order is maintained using an optimized algorithm, for example by storing each parent in an individual field, see https://www.drupal.org/node/141866 for more details. However, any details of the storage should not be relied upon since it may be swapped with a different implementation.

Parameters

string $menu_name

The name of the menu.

MenuTreeParameters $parameters

The parameters to determine which menu links to be loaded into a tree.

Return Value

array

An array with 2 elements:

  • tree: A fully built menu tree containing an array. static::treeDataRecursive()
  • route_names: An array of all route names used in the tree.

array loadAllChildren(string $id, int $max_relative_depth = NULL)

Loads all the enabled menu links that are below the given ID.

The returned links are not ordered, and visible children will be included even if they have parent that is not enabled or ancestor so would not normally appear in a rendered tree.

Parameters

string $id

The parent menu link ID.

int $max_relative_depth

The maximum relative depth of the children relative to the passed parent.

Return Value

array

An array of enabled link definitions, keyed by ID.

array getAllChildIds(string $id)

Loads all the IDs for menu links that are below the given ID.

Parameters

string $id

The parent menu link ID.

Return Value

array

An unordered array of plugin IDs corresponding to all children.

array loadSubtreeData(string $id, int $max_relative_depth = NULL)

Loads a subtree rooted by the given ID.

The returned links are structured like those from loadTreeData().

Parameters

string $id

The menu link plugin ID.

int $max_relative_depth

(optional) The maximum depth of child menu links relative to the passed in. Defaults to NULL, in which case the full subtree will be returned.

Return Value

array

An array with 2 elements:

  • subtree: A fully built menu tree element or FALSE.
  • route_names: An array of all route names used in the subtree.

array getRootPathIds(string $id)

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

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:

array getExpanded(string $menu_name, array $parents)

Finds expanded links in a menu given a set of possible parents.

Parameters

string $menu_name

The menu name.

array $parents

One or more parent IDs to match.

Return Value

array

The menu link IDs that are flagged as expanded in this menu.

int getSubtreeHeight(string $id)

Finds the height of a subtree rooted by the given ID.

Parameters

string $id

The ID of an item in the storage.

Return Value

int

Returns the height of the subtree. This will be at least 1 if the ID exists, or 0 if the ID does not exist in the storage.

bool menuNameInUse(string $menu_name)

Determines whether a specific menu name is used in the tree.

Parameters

string $menu_name

The menu name.

Return Value

bool

Returns TRUE if the given menu name is used, otherwise FALSE.

array getMenuNames()

Returns the used menu names in the tree storage.

Return Value

array

The menu names.

Counts the total number of menu links in one menu or all menus.

Parameters

string $menu_name

(optional) The menu name to count by. Defaults to all menus.

Return Value

int

The number of menu links in the named menu, or in all menus if the menu name is NULL.