Pull merge.
[yaffs-website] / web / core / lib / Drupal / Core / Menu / Form / MenuLinkDefaultForm.php
1 <?php
2
3 namespace Drupal\Core\Menu\Form;
4
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;
14
15 /**
16  * Provides an edit form for static menu links.
17  *
18  * @see \Drupal\Core\Menu\MenuLinkDefault
19  */
20 class MenuLinkDefaultForm implements MenuLinkFormInterface, ContainerInjectionInterface {
21
22   use StringTranslationTrait;
23
24   /**
25    * The edited menu link.
26    *
27    * @var \Drupal\Core\Menu\MenuLinkInterface
28    */
29   protected $menuLink;
30
31   /**
32    * The menu link manager.
33    *
34    * @var \Drupal\Core\Menu\MenuLinkManagerInterface
35    */
36   protected $menuLinkManager;
37
38   /**
39    * The parent form selector service.
40    *
41    * @var \Drupal\Core\Menu\MenuParentFormSelectorInterface
42    */
43   protected $menuParentSelector;
44
45   /**
46    * The module handler service.
47    *
48    * @var \Drupal\Core\Extension\ModuleHandlerInterface
49    */
50   protected $moduleHandler;
51
52   /**
53    * The module data from system_get_info().
54    *
55    * @var array
56    */
57   protected $moduleData;
58
59   /**
60    * Constructs a new \Drupal\Core\Menu\Form\MenuLinkDefaultForm.
61    *
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
69    *   The module handler;
70    */
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;
76   }
77
78   /**
79    * {@inheritdoc}
80    */
81   public static function create(ContainerInterface $container) {
82     return new static(
83       $container->get('plugin.manager.menu.link'),
84       $container->get('menu.parent_form_selector'),
85       $container->get('string_translation'),
86       $container->get('module_handler')
87     );
88   }
89
90   /**
91    * {@inheritdoc}
92    */
93   public function setMenuLinkInstance(MenuLinkInterface $menu_link) {
94     $this->menuLink = $menu_link;
95   }
96
97   /**
98    * {@inheritdoc}
99    */
100   public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
101     $form['#title'] = $this->t('Edit menu link %title', ['%title' => $this->menuLink->getTitle()]);
102
103     $provider = $this->menuLink->getProvider();
104     $form['info'] = [
105       '#type' => 'item',
106       '#title' => $this->t('This link is provided by the @name module. The title and path cannot be edited.', ['@name' => $this->moduleHandler->getName($provider)]),
107     ];
108     $form['id'] = [
109       '#type' => 'value',
110       '#value' => $this->menuLink->getPluginId(),
111     ];
112     $link = [
113       '#type' => 'link',
114       '#title' => $this->menuLink->getTitle(),
115       '#url' => $this->menuLink->getUrlObject(),
116     ];
117     $form['path'] = [
118       'link' => $link,
119       '#type' => 'item',
120       '#title' => $this->t('Link'),
121     ];
122
123     $form['enabled'] = [
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(),
128     ];
129
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(),
135     ];
136
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';
142
143     $delta = max(abs($this->menuLink->getWeight()), 50);
144     $form['weight'] = [
145       '#type' => 'number',
146       '#min' => -$delta,
147       '#max' => $delta,
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.'),
151     ];
152
153     return $form;
154   }
155
156   /**
157    * {@inheritdoc}
158    */
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;
171     }
172     if (isset($parent)) {
173       $new_definition['parent'] = $parent;
174     }
175     return $new_definition;
176   }
177
178   /**
179    * {@inheritdoc}
180    */
181   public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
182   }
183
184   /**
185    * {@inheritdoc}
186    */
187   public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
188     $new_definition = $this->extractFormValues($form, $form_state);
189
190     return $this->menuLinkManager->updateDefinition($this->menuLink->getPluginId(), $new_definition);
191   }
192
193 }