MenuLinkDefaultForm.php 6.4 KB

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