You are here

function cshs_menu_link_form_node_form_alter in Client-side Hierarchical Select 8.2

Same name and namespace in other branches
  1. 8 modules/cshs_menu_link/cshs_menu_link.module \cshs_menu_link_form_node_form_alter()

Implements hook_form_BASE_FORM_ID_alter().

@internal

See also

\menu_ui_form_node_form_alter()

File

modules/cshs_menu_link/cshs_menu_link.module, line 16
Client-side Hierarchical Select: Menu Link.

Code

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);
      }
    }
  }
}