MenuLinkDefaultForm.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. namespace Drupal\Core\Menu\Form;
  3. use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
  4. use Drupal\Core\Form\FormStateInterface;
  5. use Drupal\Core\Menu\MenuLinkInterface;
  6. use Drupal\Core\Menu\MenuLinkManagerInterface;
  7. use Drupal\Core\Menu\MenuParentFormSelectorInterface;
  8. use Drupal\Core\Extension\ModuleHandlerInterface;
  9. use Drupal\Core\StringTranslation\StringTranslationTrait;
  10. use Drupal\Core\StringTranslation\TranslationInterface;
  11. use Symfony\Component\DependencyInjection\ContainerInterface;
  12. /**
  13. * Provides an edit form for static menu links.
  14. *
  15. * @see \Drupal\Core\Menu\MenuLinkDefault
  16. */
  17. class MenuLinkDefaultForm implements MenuLinkFormInterface, ContainerInjectionInterface {
  18. use StringTranslationTrait;
  19. /**
  20. * The edited menu link.
  21. *
  22. * @var \Drupal\Core\Menu\MenuLinkInterface
  23. */
  24. protected $menuLink;
  25. /**
  26. * The menu link manager.
  27. *
  28. * @var \Drupal\Core\Menu\MenuLinkManagerInterface
  29. */
  30. protected $menuLinkManager;
  31. /**
  32. * The parent form selector service.
  33. *
  34. * @var \Drupal\Core\Menu\MenuParentFormSelectorInterface
  35. */
  36. protected $menuParentSelector;
  37. /**
  38. * The module handler service.
  39. *
  40. * @var \Drupal\Core\Extension\ModuleHandlerInterface
  41. */
  42. protected $moduleHandler;
  43. /**
  44. * Constructs a new \Drupal\Core\Menu\Form\MenuLinkDefaultForm.
  45. *
  46. * @param \Drupal\Core\Menu\MenuLinkManagerInterface $menu_link_manager
  47. * The menu link manager.
  48. * @param \Drupal\Core\Menu\MenuParentFormSelectorInterface $menu_parent_selector
  49. * The menu parent form selector service.
  50. * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
  51. * The string translation.
  52. * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
  53. * The module handler;
  54. */
  55. public function __construct(MenuLinkManagerInterface $menu_link_manager, MenuParentFormSelectorInterface $menu_parent_selector, TranslationInterface $string_translation, ModuleHandlerInterface $module_handler) {
  56. $this->menuLinkManager = $menu_link_manager;
  57. $this->menuParentSelector = $menu_parent_selector;
  58. $this->stringTranslation = $string_translation;
  59. $this->moduleHandler = $module_handler;
  60. }
  61. /**
  62. * {@inheritdoc}
  63. */
  64. public static function create(ContainerInterface $container) {
  65. return new static(
  66. $container->get('plugin.manager.menu.link'),
  67. $container->get('menu.parent_form_selector'),
  68. $container->get('string_translation'),
  69. $container->get('module_handler')
  70. );
  71. }
  72. /**
  73. * {@inheritdoc}
  74. */
  75. public function setMenuLinkInstance(MenuLinkInterface $menu_link) {
  76. $this->menuLink = $menu_link;
  77. }
  78. /**
  79. * {@inheritdoc}
  80. */
  81. public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
  82. $form['#title'] = $this->t('Edit menu link %title', ['%title' => $this->menuLink->getTitle()]);
  83. $provider = $this->menuLink->getProvider();
  84. $form['info'] = [
  85. '#type' => 'item',
  86. '#title' => $this->t('This link is provided by the @name module. The title and path cannot be edited.', ['@name' => $this->moduleHandler->getName($provider)]),
  87. ];
  88. $form['id'] = [
  89. '#type' => 'value',
  90. '#value' => $this->menuLink->getPluginId(),
  91. ];
  92. $link = [
  93. '#type' => 'link',
  94. '#title' => $this->menuLink->getTitle(),
  95. '#url' => $this->menuLink->getUrlObject(),
  96. ];
  97. $form['path'] = [
  98. 'link' => $link,
  99. '#type' => 'item',
  100. '#title' => $this->t('Link'),
  101. ];
  102. $form['enabled'] = [
  103. '#type' => 'checkbox',
  104. '#title' => $this->t('Enable menu link'),
  105. '#description' => $this->t('Menu links that are not enabled will not be listed in any menu.'),
  106. '#default_value' => $this->menuLink->isEnabled(),
  107. ];
  108. $form['expanded'] = [
  109. '#type' => 'checkbox',
  110. '#title' => t('Show as expanded'),
  111. '#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.'),
  112. '#default_value' => $this->menuLink->isExpanded(),
  113. ];
  114. $menu_parent = $this->menuLink->getMenuName() . ':' . $this->menuLink->getParent();
  115. $form['menu_parent'] = $this->menuParentSelector->parentSelectElement($menu_parent, $this->menuLink->getPluginId());
  116. $form['menu_parent']['#title'] = $this->t('Parent link');
  117. $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.');
  118. $form['menu_parent']['#attributes']['class'][] = 'menu-title-select';
  119. $delta = max(abs($this->menuLink->getWeight()), 50);
  120. $form['weight'] = [
  121. '#type' => 'number',
  122. '#min' => -$delta,
  123. '#max' => $delta,
  124. '#default_value' => $this->menuLink->getWeight(),
  125. '#title' => $this->t('Weight'),
  126. '#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.'),
  127. ];
  128. return $form;
  129. }
  130. /**
  131. * {@inheritdoc}
  132. */
  133. public function extractFormValues(array &$form, FormStateInterface $form_state) {
  134. // Start from the complete, original, definition.
  135. $new_definition = $this->menuLink->getPluginDefinition();
  136. // Since the ID may not be present in the definition used to construct the
  137. // plugin, add it here so it's available to any consumers of this method.
  138. $new_definition['id'] = $form_state->getValue('id');
  139. $new_definition['enabled'] = $form_state->getValue('enabled') ? 1 : 0;
  140. $new_definition['weight'] = (int) $form_state->getValue('weight');
  141. $new_definition['expanded'] = $form_state->getValue('expanded') ? 1 : 0;
  142. list($menu_name, $parent) = explode(':', $form_state->getValue('menu_parent'), 2);
  143. if (!empty($menu_name)) {
  144. $new_definition['menu_name'] = $menu_name;
  145. }
  146. if (isset($parent)) {
  147. $new_definition['parent'] = $parent;
  148. }
  149. return $new_definition;
  150. }
  151. /**
  152. * {@inheritdoc}
  153. */
  154. public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
  155. }
  156. /**
  157. * {@inheritdoc}
  158. */
  159. public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
  160. $new_definition = $this->extractFormValues($form, $form_state);
  161. return $this->menuLinkManager->updateDefinition($this->menuLink->getPluginId(), $new_definition);
  162. }
  163. }