class MenuLinkDefaultForm

Same name in other branches
  1. 9 core/lib/Drupal/Core/Menu/Form/MenuLinkDefaultForm.php \Drupal\Core\Menu\Form\MenuLinkDefaultForm
  2. 8.9.x core/lib/Drupal/Core/Menu/Form/MenuLinkDefaultForm.php \Drupal\Core\Menu\Form\MenuLinkDefaultForm
  3. 10 core/lib/Drupal/Core/Menu/Form/MenuLinkDefaultForm.php \Drupal\Core\Menu\Form\MenuLinkDefaultForm

Provides an edit form for static menu links.

Hierarchy

Expanded class hierarchy of MenuLinkDefaultForm

See also

\Drupal\Core\Menu\MenuLinkDefault

2 files declare their use of MenuLinkDefaultForm
MenuLinkDefaultFormTest.php in core/tests/Drupal/Tests/Core/Menu/MenuLinkDefaultFormTest.php
ViewsMenuLinkForm.php in core/modules/views/src/Plugin/Menu/Form/ViewsMenuLinkForm.php

File

core/lib/Drupal/Core/Menu/Form/MenuLinkDefaultForm.php, line 21

Namespace

Drupal\Core\Menu\Form
View source
class MenuLinkDefaultForm implements MenuLinkFormInterface, ContainerInjectionInterface {
    use StringTranslationTrait;
    
    /**
     * The edited menu link.
     *
     * @var \Drupal\Core\Menu\MenuLinkInterface
     */
    protected $menuLink;
    
    /**
     * The menu link manager.
     *
     * @var \Drupal\Core\Menu\MenuLinkManagerInterface
     */
    protected $menuLinkManager;
    
    /**
     * The parent form selector service.
     *
     * @var \Drupal\Core\Menu\MenuParentFormSelectorInterface
     */
    protected $menuParentSelector;
    
    /**
     * The module handler service.
     *
     * @var \Drupal\Core\Extension\ModuleHandlerInterface
     */
    protected $moduleHandler;
    
    /**
     * Constructs a new \Drupal\Core\Menu\Form\MenuLinkDefaultForm.
     *
     * @param \Drupal\Core\Menu\MenuLinkManagerInterface $menu_link_manager
     *   The menu link manager.
     * @param \Drupal\Core\Menu\MenuParentFormSelectorInterface $menu_parent_selector
     *   The menu parent form selector service.
     * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
     *   The string translation.
     * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
     *   The module handler;
     * @param \Drupal\Core\Extension\ModuleExtensionList|null $moduleExtensionList
     *   The module extension list.
     */
    public function __construct(MenuLinkManagerInterface $menu_link_manager, MenuParentFormSelectorInterface $menu_parent_selector, TranslationInterface $string_translation, ModuleHandlerInterface $module_handler, ?ModuleExtensionList $moduleExtensionList = NULL) {
        $this->menuLinkManager = $menu_link_manager;
        $this->menuParentSelector = $menu_parent_selector;
        $this->stringTranslation = $string_translation;
        $this->moduleHandler = $module_handler;
        if ($this->moduleExtensionList === NULL) {
            @trigger_error('Calling ' . __METHOD__ . '() without the $moduleExtensionList argument is deprecated in drupal:10.3.0 and will be required in drupal:12.0.0. See https://www.drupal.org/node/3310017', E_USER_DEPRECATED);
            $this->moduleExtensionList = \Drupal::service('extension.list.module');
        }
    }
    
    /**
     * {@inheritdoc}
     */
    public static function create(ContainerInterface $container) {
        return new static($container->get('plugin.manager.menu.link'), $container->get('menu.parent_form_selector'), $container->get('string_translation'), $container->get('module_handler'), $container->get('extension.list.module'));
    }
    
    /**
     * {@inheritdoc}
     */
    public function setMenuLinkInstance(MenuLinkInterface $menu_link) {
        $this->menuLink = $menu_link;
    }
    
    /**
     * {@inheritdoc}
     */
    public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
        $form['#title'] = $this->t('Edit menu link %title', [
            '%title' => $this->menuLink
                ->getTitle(),
        ]);
        $provider = $this->menuLink
            ->getProvider();
        $form['info'] = [
            '#type' => 'item',
            '#title' => $this->t('This link is provided by the @name module. The title and path cannot be edited.', [
                '@name' => $this->moduleExtensionList
                    ->getName($provider),
            ]),
        ];
        $form['id'] = [
            '#type' => 'value',
            '#value' => $this->menuLink
                ->getPluginId(),
        ];
        $link = [
            '#type' => 'link',
            '#title' => $this->menuLink
                ->getTitle(),
            '#url' => $this->menuLink
                ->getUrlObject(),
        ];
        $form['path'] = [
            'link' => $link,
            '#type' => 'item',
            '#title' => $this->t('Link'),
        ];
        $form['enabled'] = [
            '#type' => 'checkbox',
            '#title' => $this->t('Enable menu link'),
            '#description' => $this->t('Menu links that are not enabled will not be listed in any menu.'),
            '#default_value' => $this->menuLink
                ->isEnabled(),
        ];
        $form['expanded'] = [
            '#type' => 'checkbox',
            '#title' => $this->t('Show as expanded'),
            '#description' => $this->t('If selected and this menu link has children, the menu will always appear expanded. This option may be overridden for the entire menu tree when placing a menu block.'),
            '#default_value' => $this->menuLink
                ->isExpanded(),
        ];
        $menu_parent = $this->menuLink
            ->getMenuName() . ':' . $this->menuLink
            ->getParent();
        $form['menu_parent'] = $this->menuParentSelector
            ->parentSelectElement($menu_parent, $this->menuLink
            ->getPluginId());
        $form['menu_parent']['#title'] = $this->t('Parent link');
        $form['menu_parent']['#description'] = $this->t('The maximum depth for a link and all its children is fixed. Some menu links may not be available as parents if selecting them would exceed this limit.');
        $form['menu_parent']['#attributes']['class'][] = 'menu-title-select';
        $delta = max(abs($this->menuLink
            ->getWeight()), 50);
        $form['weight'] = [
            '#type' => 'number',
            '#min' => -$delta,
            '#max' => $delta,
            '#default_value' => $this->menuLink
                ->getWeight(),
            '#title' => $this->t('Weight'),
            '#description' => $this->t('Link weight among links in the same menu at the same depth. In the menu, the links with high weight will sink and links with a low weight will be positioned nearer the top.'),
        ];
        return $form;
    }
    
    /**
     * {@inheritdoc}
     */
    public function extractFormValues(array &$form, FormStateInterface $form_state) {
        // Start from the complete, original, definition.
        $new_definition = $this->menuLink
            ->getPluginDefinition();
        // Since the ID may not be present in the definition used to construct the
        // plugin, add it here so it's available to any consumers of this method.
        $new_definition['id'] = $form_state->getValue('id');
        $new_definition['enabled'] = $form_state->getValue('enabled') ? 1 : 0;
        $new_definition['weight'] = (int) $form_state->getValue('weight');
        $new_definition['expanded'] = $form_state->getValue('expanded') ? 1 : 0;
        [
            $menu_name,
            $parent,
        ] = explode(':', $form_state->getValue('menu_parent'), 2);
        if (!empty($menu_name)) {
            $new_definition['menu_name'] = $menu_name;
        }
        if (isset($parent)) {
            $new_definition['parent'] = $parent;
        }
        return $new_definition;
    }
    
    /**
     * {@inheritdoc}
     */
    public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
    }
    
    /**
     * {@inheritdoc}
     */
    public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
        $new_definition = $this->extractFormValues($form, $form_state);
        return $this->menuLinkManager
            ->updateDefinition($this->menuLink
            ->getPluginId(), $new_definition);
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
MenuLinkDefaultForm::$menuLink protected property The edited menu link. 1
MenuLinkDefaultForm::$menuLinkManager protected property The menu link manager.
MenuLinkDefaultForm::$menuParentSelector protected property The parent form selector service.
MenuLinkDefaultForm::$moduleHandler protected property The module handler service.
MenuLinkDefaultForm::buildConfigurationForm public function Form constructor. Overrides PluginFormInterface::buildConfigurationForm 1
MenuLinkDefaultForm::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create
MenuLinkDefaultForm::extractFormValues public function Extracts a plugin definition from form values. Overrides MenuLinkFormInterface::extractFormValues 1
MenuLinkDefaultForm::setMenuLinkInstance public function Injects the menu link plugin instance. Overrides MenuLinkFormInterface::setMenuLinkInstance
MenuLinkDefaultForm::submitConfigurationForm public function Form submission handler. Overrides PluginFormInterface::submitConfigurationForm
MenuLinkDefaultForm::validateConfigurationForm public function Form validation handler. Overrides PluginFormInterface::validateConfigurationForm
MenuLinkDefaultForm::__construct public function Constructs a new \Drupal\Core\Menu\Form\MenuLinkDefaultForm.
StringTranslationTrait::$stringTranslation protected property The string translation service. 3
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.

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