class MenuParentFormSelector implements MenuParentFormSelectorInterface (View source)

Default implementation of the menu parent form selector service.

The form selector is a list of all appropriate menu links.

Traits

Wrapper methods for \Drupal\Core\StringTranslation\TranslationInterface.

Properties

protected TranslationInterface $stringTranslation

The string translation service.

from  StringTranslationTrait
protected MenuLinkTreeInterface $menuLinkTree

The menu link tree service.

protected EntityTypeManagerInterface $entityTypeManager

The entity type manager service.

Methods

t(string $string, array $args = [], array $options = [])

Translates a string to the current language or to a given language.

formatPlural($count, $singular, $plural, array $args = [], array $options = [])

Formats a string containing a count of items.

getNumberOfPlurals($langcode = NULL)

Returns the number of plurals supported by a given language.

getStringTranslation()

Gets the string translation service.

$this
setStringTranslation(TranslationInterface $translation)

Sets the string translation service to use.

__construct(MenuLinkTreeInterface $menu_link_tree, EntityTypeManagerInterface $entity_type_manager, TranslationInterface $string_translation)

Constructs a \Drupal\Core\Menu\MenuParentFormSelector.

array
getParentSelectOptions(string $id = '', array $menus = NULL, CacheableMetadata $cacheability = NULL)

Gets the options for a select element to choose a menu and parent.

array
parentSelectElement(string $menu_parent, string $id = '', array $menus = NULL)

Gets a form element to choose a menu and parent.

int
getParentDepthLimit(string $id)

Returns the maximum depth of the possible parents of the menu link.

parentSelectOptionsTreeWalk(array $tree, string $menu_name, string $indent, array $options, string $exclude, int $depth_limit, CacheableMetadata $cacheability = NULL)

Iterates over all items in the tree to prepare the parents select options.

array
getMenuOptions(array $menu_names = NULL)

Gets a list of menu names for use as options.

Details

protected TranslatableMarkup t(string $string, array $args = [], array $options = [])

Translates a string to the current language or to a given language.

See \Drupal\Core\StringTranslation\TranslatableMarkup::__construct() for important security information and usage guidelines.

In order for strings to be localized, make them available in one of the ways supported by the

Parameters

string $string

A string containing the English text to translate.

array $args

(optional) An associative array of replacements to make after translation. Based on the first character of the key, the value is escaped and/or themed. See \Drupal\Component\Render\FormattableMarkup::placeholderFormat() for details.

array $options

(optional) An associative array of additional options, with the following elements:

  • 'langcode' (defaults to the current language): A language code, to translate to a language other than what is used to display the page.
  • 'context' (defaults to the empty context): The context the source string belongs to. See the @link i18n Internationalization topic @endlink for more information about string contexts.

Return Value

TranslatableMarkup

An object that, when cast to a string, returns the translated string.

See also

FormattableMarkup::placeholderFormat
TranslatableMarkup::__construct

protected formatPlural($count, $singular, $plural, array $args = [], array $options = [])

Formats a string containing a count of items.

Parameters

$count
$singular
$plural
array $args
array $options

See also

TranslationInterface::formatPlural

protected getNumberOfPlurals($langcode = NULL)

Returns the number of plurals supported by a given language.

Parameters

$langcode

See also

PluralFormulaInterface::getNumberOfPlurals

protected TranslationInterface getStringTranslation()

Gets the string translation service.

Return Value

TranslationInterface

The string translation service.

$this setStringTranslation(TranslationInterface $translation)

Sets the string translation service to use.

Parameters

TranslationInterface $translation

The string translation service.

Return Value

$this

__construct(MenuLinkTreeInterface $menu_link_tree, EntityTypeManagerInterface $entity_type_manager, TranslationInterface $string_translation)

Constructs a \Drupal\Core\Menu\MenuParentFormSelector.

Parameters

MenuLinkTreeInterface $menu_link_tree

The menu link tree service.

EntityTypeManagerInterface $entity_type_manager

The entity type manager service.

TranslationInterface $string_translation

The string translation service.

array getParentSelectOptions(string $id = '', array $menus = NULL, CacheableMetadata $cacheability = NULL)

Gets the options for a select element to choose a menu and parent.

Parameters

string $id

Optional ID of a link plugin. This will exclude the link and its children from the select options.

array $menus

Optional array of menu names as keys and titles as values to limit the select options. If NULL, all menus will be included.

CacheableMetadata $cacheability

Optional cacheability metadata object, which will be populated based on the accessibility of the links and the cacheability of the links.

Return Value

array

Keyed array where the keys are contain a menu name and parent ID and the values are a menu name or link title indented by depth.

array parentSelectElement(string $menu_parent, string $id = '', array $menus = NULL)

Gets a form element to choose a menu and parent.

The specific type of form element will vary depending on the implementation, but callers will normally need to set the #title for the element.

Parameters

string $menu_parent

A menu name and parent ID concatenated with a ':' character to use as the default value.

string $id

(optional) ID of a link plugin. This will exclude the link and its children from being selected.

array $menus

(optional) Array of menu names as keys and titles as values to limit the values that may be selected. If NULL, all menus will be included.

Return Value

array

A form element to choose a parent, or an empty array if no possible parents exist for the given parameters. The resulting form value will be a single string containing the chosen menu name and parent ID separated by a ':' character.

protected int getParentDepthLimit(string $id)

Returns the maximum depth of the possible parents of the menu link.

Parameters

string $id

The menu link plugin ID or an empty value for a new link.

Return Value

int

The depth related to the depth of the given menu link.

protected parentSelectOptionsTreeWalk(array $tree, string $menu_name, string $indent, array $options, string $exclude, int $depth_limit, CacheableMetadata $cacheability = NULL)

Iterates over all items in the tree to prepare the parents select options.

Parameters

array $tree

The menu tree.

string $menu_name

The menu name.

string $indent

The indentation string used for the label.

array $options

The select options.

string $exclude

An excluded menu link.

int $depth_limit

The maximum depth of menu links considered for the select options.

CacheableMetadata $cacheability

The object to add cacheability metadata to, if not NULL.

protected array getMenuOptions(array $menu_names = NULL)

Gets a list of menu names for use as options.

Parameters

array $menu_names

(optional) Array of menu names to limit the options, or NULL to load all.

Return Value

array

Keys are menu names (ids) values are the menu labels.