You are here

public static function MenuSelectTree::processElement in Menu Select 8

Same name and namespace in other branches
  1. 2.0.x src/Element/MenuSelectTree.php \Drupal\menu_select\Element\MenuSelectTree::processElement()

Process the element.

File

src/Element/MenuSelectTree.php, line 53

Class

MenuSelectTree
A form element for the select tree.

Namespace

Drupal\menu_select\Element

Code

public static function processElement(&$element, FormStateInterface $form_state, &$complete_form) {
  $element['tree']['menu_parent_id']['#default_value'] = $element['#menu_parent'];
  $element['tree']['parent_menu_position_preview'] = [
    '#type' => 'item',
    '#title' => t('Menu link position preview'),
    '#description' => t("Preview of the menu item's hierarchical position."),
  ];
  $element['tree']['parent_menu_position_preview']['children'] = [
    '#type' => 'container',
    '#attributes' => [
      'class' => [
        'menu-select-parent-position-preview',
        'js-menu-select-parent-position-preview',
      ],
    ],
  ];
  $config = \Drupal::config('menu_select.settings');
  $search_enabled = $config
    ->get('search_enabled');
  if ($search_enabled && \Drupal::currentUser()
    ->hasPermission('use menu select search')) {
    $element['tree']['parent_menu_item_search'] = [
      '#type' => 'textfield',
      '#title' => t('Search'),
      '#autocomplete_route_name' => 'menu_select.menu_select_autocomplete',
      '#autocomplete_route_parameters' => [
        'menus' => implode(':', array_keys($element['#menus'])),
        'max_depth' => $element['#max_depth'],
      ],
      '#description' => t("Alternatively, use this autocomplete search to find the menu item's parent and select it."),
      '#attributes' => [
        'class' => [
          'menu-select-parent-menu-item-search',
          'js-menu-select-parent-menu-item-search',
        ],
      ],
    ];
  }

  /** @var \Drupal\menu_select\MenuSelectTreeBuilder $tree_builder */
  $tree_builder = \Drupal::service('menu_select.tree_builder');
  $element['tree']['parent_menu_item'] = [];
  foreach ($element['#menus'] as $menu_machine_name => $menu_label) {
    $menu_tree = $tree_builder
      ->loadMenuTree($menu_machine_name, $element['#max_depth']);
    $element['tree']['parent_menu_item'][] = $tree_builder
      ->buildRenderedMenu($menu_tree, $menu_machine_name, $menu_label, $element['#current_link_id']);
  }
  return $element;
}