class MenuLinkManager implements MenuLinkManagerInterface (View source)

Manages discovery, instantiation, and tree building of menu link plugins.

This manager finds plugins that are rendered as menu links.

Properties

protected array $defaults

Provides some default values for the definition of all menu link plugins.

protected DiscoveryInterface $discovery

The object that discovers plugins managed by this manager.

protected FactoryInterface $factory

The object that instantiates plugins managed by this manager.

protected MenuTreeStorageInterface $treeStorage

The menu link tree storage.

protected StaticMenuLinkOverridesInterface $overrides

Service providing overrides for static links.

protected ModuleHandlerInterface $moduleHandler

The module handler.

Methods

__construct(MenuTreeStorageInterface $tree_storage, StaticMenuLinkOverridesInterface $overrides, ModuleHandlerInterface $module_handler)

Constructs a \Drupal\Core\Menu\MenuLinkManager object.

processDefinition(array $definition, $plugin_id)

Performs extra processing on plugin definitions.

getDiscovery()

Gets the plugin discovery.

getFactory()

Gets the plugin factory.

array
getDefinitions()

Gets the definition of all plugins for this type.

rebuild()

Triggers discovery, save, and cleanup of discovered links.

mixed
getDefinition(string $plugin_id, bool $exception_on_invalid = TRUE)

Gets a specific plugin definition.

bool
hasDefinition(string $plugin_id)

Indicates if a specific plugin definition exists.

object
createInstance(string $plugin_id, array $configuration = [])

Returns a pre-configured menu link plugin instance.

object|false
getInstance(array $options)

Gets a preconfigured instance of a plugin.

deleteLinksInMenu(string $menu_name)

Deletes all links having a certain menu name.

deleteInstance(MenuLinkInterface $instance, bool $persist)

Deletes a specific instance.

removeDefinition(string $id, bool $persist = TRUE)

Removes a single link definition from the menu tree storage.

bool
menuNameInUse(string $menu_name)

Determines if any links use a given menu name.

int
countMenuLinks(string $menu_name = NULL)

Counts the total number of menu links.

array
getParentIds(string $id)

Loads all parent link IDs of a given menu link.

array
getChildIds(string $id)

Loads all child link IDs of a given menu link, regardless of visibility.

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

Loads multiple plugin instances based on route.

addDefinition(string $id, array $definition)

Adds a new menu link definition to the menu tree storage.

updateDefinition(string $id, array $new_definition_values, bool $persist = TRUE)

Updates the values for a menu link definition in the menu tree storage.

resetLink(string $id)

Resets the values for a menu link based on the values found by discovery.

resetInstance(MenuLinkInterface $instance)

Resets the menu link to its default settings.

resetDefinitions()

Resets any local definition cache. Used for testing.

Details

__construct(MenuTreeStorageInterface $tree_storage, StaticMenuLinkOverridesInterface $overrides, ModuleHandlerInterface $module_handler)

Constructs a \Drupal\Core\Menu\MenuLinkManager object.

Parameters

MenuTreeStorageInterface $tree_storage

The menu link tree storage.

StaticMenuLinkOverridesInterface $overrides

The service providing overrides for static links.

ModuleHandlerInterface $module_handler

The module handler.

protected processDefinition(array $definition, $plugin_id)

Performs extra processing on plugin definitions.

By default we add defaults for the type to the definition. If a type has additional processing logic, the logic can be added by replacing or extending this method.

Parameters

array $definition

The definition to be processed and modified by reference.

$plugin_id

The ID of the plugin this definition is being used for.

protected DiscoveryInterface getDiscovery()

Gets the plugin discovery.

Return Value

DiscoveryInterface

protected FactoryInterface getFactory()

Gets the plugin factory.

Return Value

FactoryInterface

array getDefinitions()

Gets the definition of all plugins for this type.

Return Value

array

An array of plugin definitions (empty array if no definitions were found). Keys are plugin IDs.

rebuild()

Triggers discovery, save, and cleanup of discovered links.

mixed getDefinition(string $plugin_id, bool $exception_on_invalid = TRUE)

Gets a specific plugin definition.

Parameters

string $plugin_id

A plugin id.

bool $exception_on_invalid

(optional) If TRUE, an invalid plugin ID will throw an exception.

Return Value

mixed

A plugin definition, or NULL if the plugin ID is invalid and $exception_on_invalid is FALSE.

Exceptions

PluginNotFoundException

bool hasDefinition(string $plugin_id)

Indicates if a specific plugin definition exists.

Parameters

string $plugin_id

A plugin ID.

Return Value

bool

TRUE if the definition exists, FALSE otherwise.

object createInstance(string $plugin_id, array $configuration = [])

Returns a pre-configured menu link plugin instance.

Parameters

string $plugin_id

The ID of the plugin being instantiated.

array $configuration

An array of configuration relevant to the plugin instance.

Return Value

object

A fully configured plugin instance.

Exceptions

PluginException

object|false getInstance(array $options)

Gets a preconfigured instance of a plugin.

Parameters

array $options

An array of options that can be used to determine a suitable plugin to instantiate and how to configure it.

Return Value

object|false

A fully configured plugin instance. The interface of the plugin instance will depend on the plugin type. If no instance can be retrieved, FALSE will be returned.

deleteLinksInMenu(string $menu_name)

Deletes all links having a certain menu name.

If a link is not deletable but is resettable, the link will be reset to have its original menu name, under the assumption that the original menu is not the one we are deleting it from. Note that when resetting, if the original menu name is the same as the menu name passed to this method, the link will not be moved or deleted.

Parameters

string $menu_name

The name of the menu whose links will be deleted or reset.

protected deleteInstance(MenuLinkInterface $instance, bool $persist)

Deletes a specific instance.

Parameters

MenuLinkInterface $instance

The plugin instance to be deleted.

bool $persist

If TRUE, calls MenuLinkInterface::deleteLink() on the instance.

Exceptions

PluginException

removeDefinition(string $id, bool $persist = TRUE)

Removes a single link definition from the menu tree storage.

This is used for plugins not found through discovery to remove definitions.

Parameters

string $id

The menu link plugin ID.

bool $persist

If TRUE, this method will attempt to persist the deletion from any external storage by invoking MenuLinkInterface::deleteLink() on the plugin that is being deleted.

Exceptions

PluginException

bool menuNameInUse(string $menu_name)

Determines if any links use a given menu name.

Parameters

string $menu_name

The menu name.

Return Value

bool

TRUE if any links are present in the named menu, FALSE otherwise.

Counts the total number of menu links.

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 getParentIds(string $id)

Loads all parent link IDs of a given menu link.

This method is very similar to getActiveTrailIds() but allows the link to be specified rather than being discovered based on the menu name and request. This method is mostly useful for testing.

Parameters

string $id

The menu link plugin ID.

Return Value

array

An ordered array of IDs representing the path to the root of the tree. The first element of the array will be equal to $id, unless $id is not valid, in which case the return value will be NULL.

array getChildIds(string $id)

Loads all child link IDs of a given menu link, regardless of visibility.

This method is mostly useful for testing.

Parameters

string $id

The menu link plugin ID.

Return Value

array

An unordered array of IDs representing the IDs of all children, or NULL if the ID is invalid.

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

Loads multiple plugin instances 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

MenuLinkInterface[]

An array of instances keyed by plugin ID.

MenuLinkInterface addDefinition(string $id, array $definition)

Adds a new menu link definition to the menu tree storage.

Use this function when you know there is no entry in the tree. This is used for plugins not found through discovery to add new definitions.

Parameters

string $id

The plugin ID for the new menu link definition that is being added.

array $definition

The values of the link definition.

Return Value

MenuLinkInterface

A plugin instance created using the newly added definition.

Exceptions

PluginException

MenuLinkInterface updateDefinition(string $id, array $new_definition_values, bool $persist = TRUE)

Updates the values for a menu link definition in the menu tree storage.

This will update the definition for a discovered menu link without the need for a full rebuild. It is also used for plugins not found through discovery to update definitions.

Parameters

string $id

The menu link plugin ID.

array $new_definition_values

The new values for the link definition. This will usually be just a subset of the plugin definition.

bool $persist

TRUE to also have the link instance itself persist the changed values to any additional storage by invoking MenuLinkInterface::updateDefinition() on the plugin that is being updated.

Return Value

MenuLinkInterface

A plugin instance created using the updated definition.

Exceptions

PluginException

Resets the values for a menu link based on the values found by discovery.

Parameters

string $id

The menu link plugin ID.

Return Value

MenuLinkInterface

The menu link instance after being reset.

Exceptions

PluginException

protected MenuLinkInterface resetInstance(MenuLinkInterface $instance)

Resets the menu link to its default settings.

Parameters

MenuLinkInterface $instance

The menu link which should be reset.

Return Value

MenuLinkInterface

The reset menu link.

Exceptions

PluginException

resetDefinitions()

Resets any local definition cache. Used for testing.