function MenuParentFormSelector::parentSelectElement

Same name in other branches
  1. 8.9.x core/lib/Drupal/Core/Menu/MenuParentFormSelector.php \Drupal\Core\Menu\MenuParentFormSelector::parentSelectElement()
  2. 10 core/lib/Drupal/Core/Menu/MenuParentFormSelector.php \Drupal\Core\Menu\MenuParentFormSelector::parentSelectElement()
  3. 11.x core/lib/Drupal/Core/Menu/MenuParentFormSelector.php \Drupal\Core\Menu\MenuParentFormSelector::parentSelectElement()

Overrides MenuParentFormSelectorInterface::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 [];
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.