class MenuTreeStorage implements MenuTreeStorageInterface (View source)

Provides a menu tree storage using the database.

Constants

MAX_DEPTH

The maximum depth of a menu links tree.

Properties

protected Connection $connection

The database connection.

protected CacheBackendInterface $menuCacheBackend

Cache backend instance for the extracted tree data.

protected CacheTagsInvalidatorInterface $cacheTagsInvalidator

The cache tags invalidator.

protected string $table

The database table name.

protected array $options

Additional database connection options to use in queries.

protected array $definitions

Stores definitions that have already been loaded for better performance.

protected array $serializedFields

List of serialized fields.

protected array $definitionFields

List of plugin definition fields.

Methods

__construct(Connection $connection, CacheBackendInterface $menu_cache_backend, CacheTagsInvalidatorInterface $cache_tags_invalidator, string $table, array $options = [])

Constructs a new \Drupal\Core\Menu\MenuTreeStorage.

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.

purgeMultiple(array $ids)

Purges multiple menu links that no longer exist.

safeExecuteSelect(SelectInterface $query)

Executes a select query while making sure the database table exists.

array
save(array $link)

Saves a plugin definition to the storage.

array
doSave(array $link)

Saves a link without clearing caches.

array
preSave(array $link, array $original)

Fills in all the fields the database save needs, using the link definition.

delete(string $id)

Deletes a menu link definition from the storage.

int
getSubtreeHeight(string $id)

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

int
doFindChildrenRelativeDepth(array $original)

Finds the relative depth of this link's deepest child.

setParents(array $fields, array|false $parent, array $original)

Sets the materialized path field values based on the parent.

moveChildren(array $fields, array $original)

Re-parents a link's children when the link itself is moved.

array|false
findParent(array $link, array|false $original)

Loads the parent definition if it exists.

updateParentalStatus(array $link)

Sets has_children for the link's parent if it has visible children.

array
prepareLink(array $link, bool $intersect = FALSE)

Prepares a link by unserializing values and saving the definition.

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
loadMultiple(array $ids)

Loads multiple plugin definitions from the storage.

array|false
load(string $id)

Loads a menu link plugin definition from the storage.

array
loadFull(string $id)

Loads all table fields, not just those that are in the plugin definition.

array
loadFullMultiple(array $ids)

Loads all table fields for multiple menu link definitions by 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.

saveRecursive(string $id, array $children, array $links)

Saves menu links recursively.

array
loadTreeData(string $menu_name, MenuTreeParameters $parameters)

Loads a menu link tree from the storage.

array
loadLinks(string $menu_name, MenuTreeParameters $parameters)

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

array
collectRoutesAndDefinitions(array $tree, array $definitions)

Traverses the menu tree and collects all the route names and definitions.

array
doCollectRoutesAndDefinitions(array $tree, array $definitions)

Collects all the route names and definitions.

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

Loads 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.

array
getAllChildIds(string $id)

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

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

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

doBuildTreeData(array $links, array $parents = [], $depth = 1)

Prepares the data for calling $this->treeDataRecursive().

array
treeDataRecursive(array $links, array $parents, int $depth)

Builds the data representing a menu tree.

bool
ensureTableExists()

Checks if the tree table exists and create it if not.

array
serializedFields()

Determines serialized fields in the storage.

array
definitionFields()

Determines fields that are part of the plugin definition.

static array
schemaDefinition()

Defines the schema for the tree table.

array
findNoLongerExistingLinks(array $definitions)

Find any previously discovered menu links that no longer exist.

doDeleteMultiple(array $ids)

Purge menu links from the database.

Details

__construct(Connection $connection, CacheBackendInterface $menu_cache_backend, CacheTagsInvalidatorInterface $cache_tags_invalidator, string $table, array $options = [])

Constructs a new \Drupal\Core\Menu\MenuTreeStorage.

Parameters

Connection $connection

A Database connection to use for reading and writing configuration data.

CacheBackendInterface $menu_cache_backend

Cache backend instance for the extracted tree data.

CacheTagsInvalidatorInterface $cache_tags_invalidator

The cache tags invalidator.

string $table

A database table name to store configuration data in.

array $options

(optional) Any additional database connection options to use in queries.

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.

protected purgeMultiple(array $ids)

Purges multiple menu links that no longer exist.

Parameters

array $ids

An array of menu link IDs.

protected StatementInterface|null safeExecuteSelect(SelectInterface $query)

Executes a select query while making sure the database table exists.

Parameters

SelectInterface $query

The select object to be executed.

Return Value

StatementInterface|null

A prepared statement, or NULL if the query is not valid.

Exceptions

Exception

array save(array $link)

Saves a plugin definition to the storage.

Parameters

array $link

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

protected array doSave(array $link)

Saves a link without clearing caches.

Parameters

array $link

A definition, according to $definitionFields, 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

protected array preSave(array $link, array $original)

Fills in all the fields the database save needs, using the link definition.

Parameters

array $link

The link definition to be updated.

array $original

The link definition before the changes. May be empty if not found.

Return Value

array

The values which will be stored.

Exceptions

PluginException

delete(string $id)

Deletes a menu link definition from the storage.

Parameters

string $id

The menu link plugin ID.

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.

protected int doFindChildrenRelativeDepth(array $original)

Finds the relative depth of this link's deepest child.

Parameters

array $original

The parent definition used to find the depth.

Return Value

int

Returns the relative depth.

protected setParents(array $fields, array|false $parent, array $original)

Sets the materialized path field values based on the parent.

Parameters

array $fields

The menu link.

array|false $parent

The parent menu link.

array $original

The original menu link.

protected moveChildren(array $fields, array $original)

Re-parents a link's children when the link itself is moved.

Parameters

array $fields

The changed menu link.

array $original

The original menu link.

protected array|false findParent(array $link, array|false $original)

Loads the parent definition if it exists.

Parameters

array $link

The link definition to find the parent of.

array|false $original

The original link that might be used to find the parent if the parent is not set on the $link, or FALSE if the original could not be loaded.

Return Value

array|false

Returns a definition array, or FALSE if no parent was found.

protected updateParentalStatus(array $link)

Sets has_children for the link's parent if it has visible children.

Parameters

array $link

The link to get a parent ID from.

Prepares a link by unserializing values and saving the definition.

Parameters

array $link

The data loaded in the query.

bool $intersect

If TRUE, filter out values that are not part of the actual definition.

Return Value

array

The prepared link data.

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 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|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.

protected array loadFull(string $id)

Loads all table fields, not just those that are in the plugin definition.

Parameters

string $id

The menu link ID.

Return Value

array

The loaded menu link definition or an empty array if not be found.

protected array loadFullMultiple(array $ids)

Loads all table fields for multiple menu link definitions by ID.

Parameters

array $ids

The IDs to load.

Return Value

array

The loaded menu link definitions.

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.

protected saveRecursive(string $id, array $children, array $links)

Saves menu links recursively.

Parameters

string $id

The definition ID.

array $children

An array of IDs of child links collected by parent ID.

array $links

An array of all definitions keyed by 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.

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

Parameters

string $menu_name

A menu name.

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.

protected array collectRoutesAndDefinitions(array $tree, array $definitions)

Traverses the menu tree and collects all the route names and definitions.

Parameters

array $tree

The menu tree you wish to operate on.

array $definitions

An array to accumulate definitions by reference.

Return Value

array

Array of route names, with all values being unique.

protected array doCollectRoutesAndDefinitions(array $tree, array $definitions)

Collects all the route names and definitions.

Parameters

array $tree

A menu link tree from MenuTreeStorage::doBuildTreeData()

array $definitions

The collected definitions which are populated by reference.

Return Value

array

The collected route names.

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.

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.

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 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.

protected doBuildTreeData(array $links, array $parents = [], $depth = 1)

Prepares the data for calling $this->treeDataRecursive().

Parameters

array $links
array $parents
$depth

protected array treeDataRecursive(array $links, array $parents, int $depth)

Builds the data representing a menu tree.

The function is a bit complex because the rendering of a link depends on the next menu link.

Parameters

array $links

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 $this->table. This array must be ordered depth-first. MenuTreeStorage::loadTreeData() includes a sample query.

array $parents

An array of the menu link ID values that are in the path from the current page to the root of the menu tree.

int $depth

The minimum depth to include in the returned menu tree.

Return Value

array

The fully built tree.

See also

MenuTreeStorage::loadTreeData

protected bool ensureTableExists()

Checks if the tree table exists and create it if not.

Return Value

bool

TRUE if the table was created, FALSE otherwise.

protected array serializedFields()

Determines serialized fields in the storage.

Return Value

array

A list of fields that are serialized in the database.

protected array definitionFields()

Determines fields that are part of the plugin definition.

Return Value

array

The list of the subset of fields that are part of the plugin definition.

static protected array schemaDefinition()

internal  
 

Defines the schema for the tree table.

Return Value

array

The schema API definition for the SQL storage table.

Find any previously discovered menu links that no longer exist.

Parameters

array $definitions

The new menu link definitions.

Return Value

array

A list of menu link IDs that no longer exist.

protected doDeleteMultiple(array $ids)

Purge menu links from the database.

Parameters

array $ids

A list of menu link IDs to be purged.