cshs_menu_link.module in Client-side Hierarchical Select 8.2
Same filename and directory in other branches
Client-side Hierarchical Select: Menu Link.
File
modules/cshs_menu_link/cshs_menu_link.moduleView source
<?php
/**
* @file
* Client-side Hierarchical Select: Menu Link.
*/
use Drupal\cshs\Element\CshsElement;
/**
* Implements hook_form_BASE_FORM_ID_alter().
*
* @internal
* @see \menu_ui_form_node_form_alter()
*/
function cshs_menu_link_form_node_form_alter(array &$form) : void {
if (!empty($form['menu']['link']['menu_parent'])) {
$element =& $form['menu']['link']['menu_parent'];
$element['#type'] = 'cshs';
$element['#attached']['library'][] = 'cshs_menu_link/cshs_menu_link';
$element['#none_label'] = \t('- Select -');
$element['#description'] = \t('The menu link is created below the selected menu item. Optionally, select additional levels to place the link deeper in the menu structure.');
$element['#no_first_level_none'] = TRUE;
// The first item is always a menu name so the below processing
// is relevant only if there is at least one link additionally.
if (\count($element['#options']) > 1) {
/* @var \Drupal\Core\Menu\MenuLinkManagerInterface $link_manager */
$link_manager = \Drupal::service('plugin.manager.menu.link');
foreach ($element['#options'] as $id => $label) {
// Example `$id` values:
// - `main:`;
// - `main:standard.front_page`;
// - `main:menu_link_content:d5517ae1-925f-4385-96a3-ceca5ad7bb61`.
[
$menu_name,
$link_id,
] = \explode(':', $id, 2);
// Assume there is no parent until we know that.
$parent = '';
if ($link_id !== '') {
if ($link = $link_manager
->getDefinition($link_id, FALSE)) {
// The `parent` is either an empty string or link ID.
// It is ok to have a parent like `$menu_name:` as it means
// the link is top-level and its parent is the menu itself.
$parent = $menu_name . ':' . $link['parent'];
}
else {
\assert(FALSE, \sprintf('The link with ID "%s" must exist!', $link_id));
}
}
$element['#options'][$id] = CshsElement::option($label, $parent);
}
}
}
}
/**
* Implements hook_form_BASE_FORM_ID_alter().
*
* @internal
* @see \taxonomy_menu_ui_form_taxonomy_term_form_alter()
*/
function cshs_menu_link_form_taxonomy_term_form_alter(array &$form) : void {
if (\Drupal::moduleHandler()
->moduleExists('taxonomy_menu_ui')) {
\cshs_menu_link_form_node_form_alter($form);
}
}
Functions
Name![]() |
Description |
---|---|
cshs_menu_link_form_node_form_alter | Implements hook_form_BASE_FORM_ID_alter(). |
cshs_menu_link_form_taxonomy_term_form_alter | Implements hook_form_BASE_FORM_ID_alter(). |