You are here

public function MenuParentFormSelector::parentSelectElement in Drupal 10

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Menu/MenuParentFormSelector.php \Drupal\Core\Menu\MenuParentFormSelector::parentSelectElement()
  2. 9 core/lib/Drupal/Core/Menu/MenuParentFormSelector.php \Drupal\Core\Menu\MenuParentFormSelector::parentSelectElement()

File

core/lib/Drupal/Core/Menu/MenuParentFormSelector.php, line 79

Class

MenuParentFormSelector
Default implementation of the menu parent form selector service.

Namespace

Drupal\Core\Menu

Code

public function parentSelectElement($menu_parent, $id = '', array $menus = NULL) {
  $options_cacheability = new CacheableMetadata();
  $options = $this
    ->getParentSelectOptions($id, $menus, $options_cacheability);

  // If no options were found, there is nothing to select.
  if ($options) {
    $element = [
      '#type' => 'select',
      '#options' => $options,
    ];
    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.
      [
        $menu_name,
      ] = 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 [];
}