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