3 namespace Drupal\Core\Menu\Form;
5 use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
6 use Drupal\Core\Form\FormStateInterface;
7 use Drupal\Core\Menu\MenuLinkInterface;
8 use Drupal\Core\Menu\MenuLinkManagerInterface;
9 use Drupal\Core\Menu\MenuParentFormSelectorInterface;
10 use Drupal\Core\Extension\ModuleHandlerInterface;
11 use Drupal\Core\StringTranslation\StringTranslationTrait;
12 use Drupal\Core\StringTranslation\TranslationInterface;
13 use Symfony\Component\DependencyInjection\ContainerInterface;
16 * Provides an edit form for static menu links.
18 * @see \Drupal\Core\Menu\MenuLinkDefault
20 class MenuLinkDefaultForm implements MenuLinkFormInterface, ContainerInjectionInterface {
22 use StringTranslationTrait;
25 * The edited menu link.
27 * @var \Drupal\Core\Menu\MenuLinkInterface
32 * The menu link manager.
34 * @var \Drupal\Core\Menu\MenuLinkManagerInterface
36 protected $menuLinkManager;
39 * The parent form selector service.
41 * @var \Drupal\Core\Menu\MenuParentFormSelectorInterface
43 protected $menuParentSelector;
46 * The module handler service.
48 * @var \Drupal\Core\Extension\ModuleHandlerInterface
50 protected $moduleHandler;
53 * The module data from system_get_info().
57 protected $moduleData;
60 * Constructs a new \Drupal\Core\Menu\Form\MenuLinkDefaultForm.
62 * @param \Drupal\Core\Menu\MenuLinkManagerInterface $menu_link_manager
63 * The menu link manager.
64 * @param \Drupal\Core\Menu\MenuParentFormSelectorInterface $menu_parent_selector
65 * The menu parent form selector service.
66 * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
67 * The string translation.
68 * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
71 public function __construct(MenuLinkManagerInterface $menu_link_manager, MenuParentFormSelectorInterface $menu_parent_selector, TranslationInterface $string_translation, ModuleHandlerInterface $module_handler) {
72 $this->menuLinkManager = $menu_link_manager;
73 $this->menuParentSelector = $menu_parent_selector;
74 $this->stringTranslation = $string_translation;
75 $this->moduleHandler = $module_handler;
81 public static function create(ContainerInterface $container) {
83 $container->get('plugin.manager.menu.link'),
84 $container->get('menu.parent_form_selector'),
85 $container->get('string_translation'),
86 $container->get('module_handler')
93 public function setMenuLinkInstance(MenuLinkInterface $menu_link) {
94 $this->menuLink = $menu_link;
100 public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
101 $form['#title'] = $this->t('Edit menu link %title', ['%title' => $this->menuLink->getTitle()]);
103 $provider = $this->menuLink->getProvider();
106 '#title' => $this->t('This link is provided by the @name module. The title and path cannot be edited.', ['@name' => $this->moduleHandler->getName($provider)]),
110 '#value' => $this->menuLink->getPluginId(),
114 '#title' => $this->menuLink->getTitle(),
115 '#url' => $this->menuLink->getUrlObject(),
120 '#title' => $this->t('Link'),
124 '#type' => 'checkbox',
125 '#title' => $this->t('Enable menu link'),
126 '#description' => $this->t('Menu links that are not enabled will not be listed in any menu.'),
127 '#default_value' => $this->menuLink->isEnabled(),
130 $form['expanded'] = [
131 '#type' => 'checkbox',
132 '#title' => t('Show as expanded'),
133 '#description' => $this->t('If selected and this menu link has children, the menu will always appear expanded.'),
134 '#default_value' => $this->menuLink->isExpanded(),
137 $menu_parent = $this->menuLink->getMenuName() . ':' . $this->menuLink->getParent();
138 $form['menu_parent'] = $this->menuParentSelector->parentSelectElement($menu_parent, $this->menuLink->getPluginId());
139 $form['menu_parent']['#title'] = $this->t('Parent link');
140 $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.');
141 $form['menu_parent']['#attributes']['class'][] = 'menu-title-select';
143 $delta = max(abs($this->menuLink->getWeight()), 50);
148 '#default_value' => $this->menuLink->getWeight(),
149 '#title' => $this->t('Weight'),
150 '#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.'),
159 public function extractFormValues(array &$form, FormStateInterface $form_state) {
160 // Start from the complete, original, definition.
161 $new_definition = $this->menuLink->getPluginDefinition();
162 // Since the ID may not be present in the definition used to construct the
163 // plugin, add it here so it's available to any consumers of this method.
164 $new_definition['id'] = $form_state->getValue('id');
165 $new_definition['enabled'] = $form_state->getValue('enabled') ? 1 : 0;
166 $new_definition['weight'] = (int) $form_state->getValue('weight');
167 $new_definition['expanded'] = $form_state->getValue('expanded') ? 1 : 0;
168 list($menu_name, $parent) = explode(':', $form_state->getValue('menu_parent'), 2);
169 if (!empty($menu_name)) {
170 $new_definition['menu_name'] = $menu_name;
172 if (isset($parent)) {
173 $new_definition['parent'] = $parent;
175 return $new_definition;
181 public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
187 public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
188 $new_definition = $this->extractFormValues($form, $form_state);
190 return $this->menuLinkManager->updateDefinition($this->menuLink->getPluginId(), $new_definition);