71e15236109356c58b0385c84e30a815734c1971
[yaffs-website] / web / core / modules / settings_tray / src / Form / SystemMenuOffCanvasForm.php
1 <?php
2
3 namespace Drupal\settings_tray\Form;
4
5 use Drupal\Component\Plugin\PluginInspectionInterface;
6 use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
7 use Drupal\Core\Entity\EntityStorageInterface;
8 use Drupal\Core\Entity\EntityTypeManagerInterface;
9 use Drupal\Core\Form\FormStateInterface;
10 use Drupal\Core\Plugin\PluginFormBase;
11 use Drupal\Core\Render\Element;
12 use Drupal\Core\Routing\RedirectDestinationTrait;
13 use Drupal\Core\StringTranslation\StringTranslationTrait;
14 use Drupal\Core\StringTranslation\TranslationInterface;
15 use Drupal\system\MenuInterface;
16 use Symfony\Component\DependencyInjection\ContainerInterface;
17
18 /**
19  * The off-canvas form handler for the SystemMenuBlock.
20  *
21  * @see settings_tray_block_alter()
22  *
23  * @internal
24  */
25 class SystemMenuOffCanvasForm extends PluginFormBase implements ContainerInjectionInterface {
26
27   use StringTranslationTrait;
28   use RedirectDestinationTrait;
29
30   /**
31    * The plugin.
32    *
33    * @var \Drupal\Core\Block\BlockPluginInterface
34    */
35   protected $plugin;
36
37   /**
38    * The menu entity that the block uses and that will be edited in this form.
39    *
40    * @var \Drupal\system\MenuInterface
41    */
42   protected $menu;
43
44   /**
45    * @var \Drupal\Core\Entity\EntityStorageInterface
46    */
47   protected $menuStorage;
48
49   /**
50    * @var \Drupal\Core\Entity\EntityTypeManagerInterface
51    */
52   protected $entityTypeManager;
53
54   /**
55    * SystemMenuOffCanvasForm constructor.
56    *
57    * @param \Drupal\Core\Entity\EntityStorageInterface $menu_storage
58    * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
59    * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
60    */
61   public function __construct(EntityStorageInterface $menu_storage, EntityTypeManagerInterface $entity_type_manager, TranslationInterface $string_translation) {
62     $this->menuStorage = $menu_storage;
63     $this->entityTypeManager = $entity_type_manager;
64     $this->stringTranslation = $string_translation;
65   }
66
67   /**
68    * {@inheritdoc}
69    */
70   public static function create(ContainerInterface $container) {
71     return new static(
72       $container->get('entity_type.manager')->getStorage('menu'),
73       $container->get('entity_type.manager'),
74       $container->get('string_translation')
75     );
76   }
77
78   /**
79    * {@inheritdoc}
80    */
81   public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
82     $form = $this->plugin->buildConfigurationForm([], $form_state);
83     // Move the menu levels section to the bottom.
84     $form['menu_levels']['#weight'] = 100;
85
86     $form['entity_form'] = [
87       '#type' => 'details',
88       '#title' => $this->t('Edit menu %label', ['%label' => $this->menu->label()]),
89       '#open' => TRUE,
90       '#access' => $this->menu->access('edit'),
91     ];
92     $form['entity_form'] += $this->getEntityForm($this->menu)->buildForm([], $form_state);
93
94     // Print the menu link titles as text instead of a link.
95     if (!empty($form['entity_form']['links']['links'])) {
96       foreach (Element::children($form['entity_form']['links']['links']) as $child) {
97         $title = $form['entity_form']['links']['links'][$child]['title'][1]['#title'];
98         $form['entity_form']['links']['links'][$child]['title'][1] = ['#markup' => $title];
99       }
100     }
101     // Change the header text.
102     $form['entity_form']['links']['links']['#header'][0] = $this->t('Link');
103     $form['entity_form']['links']['links']['#header'][1]['data'] = $this->t('On');
104
105     // Remove the label, ID, description, and buttons from the entity form.
106     unset($form['entity_form']['label'], $form['entity_form']['id'], $form['entity_form']['description'], $form['entity_form']['actions']);
107     // Since the overview form is further nested than expected, update the
108     // #parents. See \Drupal\menu_ui\MenuForm::form().
109     $form_state->set('menu_overview_form_parents', ['settings', 'entity_form', 'links']);
110
111     return $form;
112   }
113
114   /**
115    * {@inheritdoc}
116    */
117   public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
118     $this->plugin->validateConfigurationForm($form, $form_state);
119     $this->getEntityForm($this->menu)->validateForm($form, $form_state);
120   }
121
122   /**
123    * {@inheritdoc}
124    */
125   public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
126     $this->plugin->submitConfigurationForm($form, $form_state);
127     $this->getEntityForm($this->menu)->submitForm($form, $form_state);
128     $this->menu->save();
129   }
130
131   /**
132    * Gets the entity form for this menu.
133    *
134    * @param \Drupal\system\MenuInterface $menu
135    *   The menu entity.
136    *
137    * @return \Drupal\Core\Entity\EntityFormInterface
138    *   The entity form.
139    */
140   protected function getEntityForm(MenuInterface $menu) {
141     $entity_form = $this->entityTypeManager->getFormObject('menu', 'edit');
142     $entity_form->setEntity($menu);
143     return $entity_form;
144   }
145
146   /**
147    * {@inheritdoc}
148    */
149   public function setPlugin(PluginInspectionInterface $plugin) {
150     $this->plugin = $plugin;
151     $this->menu = $this->menuStorage->load($this->plugin->getDerivativeId());
152   }
153
154 }