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