class CshsMenuParentFormSelector in Menu Link Weight 8
Same name and namespace in other branches
- 8.2 src/MenuParentFormSelector/CshsMenuParentFormSelector.php \Drupal\menu_link_weight\MenuParentFormSelector\CshsMenuParentFormSelector
Implements Client-side hierarchical select (CSHS) as the menu parent form selector.
Hierarchy
- class \Drupal\Core\Menu\MenuParentFormSelector implements MenuParentFormSelectorInterface uses DeprecatedServicePropertyTrait, StringTranslationTrait
- class \Drupal\menu_link_weight\MenuParentFormSelector\CshsMenuParentFormSelector
Expanded class hierarchy of CshsMenuParentFormSelector
1 file declares its use of CshsMenuParentFormSelector
File
- src/
MenuParentFormSelector/ CshsMenuParentFormSelector.php, line 14
Namespace
Drupal\menu_link_weight\MenuParentFormSelectorView source
class CshsMenuParentFormSelector extends MenuParentFormSelector {
/**
* {@inheritdoc}
*/
public function getParentSelectOptionsCshs($id = '', array $menus = NULL, CacheableMetadata &$cacheability = NULL) {
if (!isset($menus)) {
$menus = $this
->getMenuOptions();
}
$options = [];
$depth_limit = $this
->getParentDepthLimit($id);
foreach ($menus as $menu_name => $menu_title) {
$options[$menu_name . ':'] = [
'name' => '<' . $menu_title . '>',
'parent_tid' => 0,
];
$parameters = new MenuTreeParameters();
$parameters
->setMaxDepth($depth_limit);
$tree = $this->menuLinkTree
->load($menu_name, $parameters);
$manipulators = [
[
'callable' => 'menu.default_tree_manipulators:checkNodeAccess',
],
[
'callable' => 'menu.default_tree_manipulators:checkAccess',
],
[
'callable' => 'menu.default_tree_manipulators:generateIndexAndSort',
],
];
$tree = $this->menuLinkTree
->transform($tree, $manipulators);
$this
->parentSelectOptionsTreeWalkCshs($tree, $menu_name, $menu_name . ':', $options, $id, $depth_limit, $cacheability);
}
return $options;
}
/**
* {@inheritdoc}
*/
public function parentSelectElement($menu_parent, $id = '', array $menus = NULL) {
$options_cacheability = new CacheableMetadata();
$options = $this
->getParentSelectOptionsCshs($id, $menus, $options_cacheability);
// If no options were found, there is nothing to select.
if ($options) {
$element = [
'#type' => 'cshs',
'#options' => $options,
'#attached' => [
'library' => [
'menu_link_weight/menu_parent_selector.cshs',
],
],
];
if (!isset($options[$menu_parent])) {
// The requested menu parent cannot be found in the menu anymore. Try
// setting it to the top level in the current menu.
list($menu_name, $parent) = explode(':', $menu_parent, 2);
$menu_parent = $menu_name . ':';
}
if (isset($options[$menu_parent])) {
// Only provide the default value if it is valid among the options.
$element += [
'#default_value' => $menu_parent,
];
}
$options_cacheability
->applyTo($element);
return $element;
}
return [];
}
/**
* {@inheritdoc}
*/
protected function parentSelectOptionsTreeWalkCshs(array $tree, $menu_name, $indent, array &$options, $exclude, $depth_limit, CacheableMetadata &$cacheability = NULL) {
/** @var \Drupal\Core\Menu\MenuLinkTreeElement[] $tree */
foreach ($tree as $element) {
if ($element->depth > $depth_limit) {
// Don't iterate through any links on this level.
break;
}
// Collect the cacheability metadata of the access result, as well as the
// link.
if ($cacheability) {
$cacheability = $cacheability
->merge(CacheableMetadata::createFromObject($element->access))
->merge(CacheableMetadata::createFromObject($element->link));
}
// Only show accessible links.
if (!$element->access
->isAllowed()) {
continue;
}
$link = $element->link;
if ($link
->getPluginId() != $exclude) {
$title = Unicode::truncate($link
->getTitle(), 30, TRUE, FALSE);
if (!$link
->isEnabled()) {
$title .= ' (' . $this
->t('disabled') . ')';
}
$options[$menu_name . ':' . $link
->getPluginId()] = [
'name' => $title,
'parent_tid' => $indent,
];
if (!empty($element->subtree)) {
$this
->parentSelectOptionsTreeWalkCshs($element->subtree, $menu_name, $menu_name . ':' . $link
->getPluginId(), $options, $exclude, $depth_limit, $cacheability);
}
}
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
CshsMenuParentFormSelector:: |
public | function | ||
CshsMenuParentFormSelector:: |
public | function |
Gets a form element to choose a menu and parent. Overrides MenuParentFormSelector:: |
|
CshsMenuParentFormSelector:: |
protected | function | ||
DeprecatedServicePropertyTrait:: |
public | function | Allows to access deprecated/removed properties. | |
MenuParentFormSelector:: |
protected | property | ||
MenuParentFormSelector:: |
protected | property | The entity type manager service. | |
MenuParentFormSelector:: |
protected | property | The menu link tree service. | |
MenuParentFormSelector:: |
protected | function | Gets a list of menu names for use as options. | |
MenuParentFormSelector:: |
protected | function | Returns the maximum depth of the possible parents of the menu link. | |
MenuParentFormSelector:: |
public | function |
Gets the options for a select element to choose a menu and parent. Overrides MenuParentFormSelectorInterface:: |
|
MenuParentFormSelector:: |
protected | function | Iterates over all items in the tree to prepare the parents select options. | |
MenuParentFormSelector:: |
public | function | Constructs a \Drupal\Core\Menu\MenuParentFormSelector | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |