summaryrefslogtreecommitdiffstats
path: root/core/modules/settings_tray/src/Form/SystemMenuOffCanvasForm.php
blob: 1f8114f498ad7be057a512cfb8ee771feb48fce6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<?php

namespace Drupal\settings_tray\Form;

use Drupal\Component\Plugin\PluginInspectionInterface;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\PluginFormBase;
use Drupal\Core\Render\Element;
use Drupal\Core\Routing\RedirectDestinationTrait;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\system\MenuInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * The off-canvas form handler for the SystemMenuBlock.
 *
 * @see settings_tray_block_alter()
 *
 * @internal
 */
class SystemMenuOffCanvasForm extends PluginFormBase implements ContainerInjectionInterface {

  use StringTranslationTrait;
  use RedirectDestinationTrait;

  /**
   * The plugin.
   *
   * @var \Drupal\Core\Block\BlockPluginInterface
   */
  protected $plugin;

  /**
   * The menu entity that the block uses and that will be edited in this form.
   *
   * @var \Drupal\system\MenuInterface
   */
  protected $menu;

  /**
   * @var \Drupal\Core\Entity\EntityStorageInterface
   */
  protected $menuStorage;

  /**
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * The config factory.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * SystemMenuOffCanvasForm constructor.
   *
   * @param \Drupal\Core\Entity\EntityStorageInterface $menu_storage
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The config factory.
   */
  public function __construct(EntityStorageInterface $menu_storage, EntityTypeManagerInterface $entity_type_manager, TranslationInterface $string_translation, ConfigFactoryInterface $config_factory) {
    $this->menuStorage = $menu_storage;
    $this->entityTypeManager = $entity_type_manager;
    $this->stringTranslation = $string_translation;
    $this->configFactory = $config_factory;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('entity_type.manager')->getStorage('menu'),
      $container->get('entity_type.manager'),
      $container->get('string_translation'),
      $container->get('config.factory')
    );
  }

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $form = $this->plugin->buildConfigurationForm([], $form_state);
    // Move the menu levels section to the bottom.
    $form['menu_levels']['#weight'] = 100;

    $form['entity_form'] = [
      '#type' => 'details',
      '#title' => $this->t('Edit menu %label', ['%label' => $this->menu->label()]),
      '#open' => TRUE,
      '#access' => AccessResult::allowedIf(!$this->hasMenuOverrides()),
    ];
    $form['entity_form'] += $this->getEntityForm($this->menu)->buildForm([], $form_state);

    // Print the menu link titles as text instead of a link.
    if (!empty($form['entity_form']['links']['links'])) {
      foreach (Element::children($form['entity_form']['links']['links']) as $child) {
        $title = $form['entity_form']['links']['links'][$child]['title'][1]['#title'];
        $form['entity_form']['links']['links'][$child]['title'][1] = ['#markup' => $title];
      }
    }
    // Change the header text.
    $form['entity_form']['links']['links']['#header'][0] = $this->t('Link');
    $form['entity_form']['links']['links']['#header'][1]['data'] = $this->t('On');

    // Remove the label, ID, description, and buttons from the entity form.
    unset($form['entity_form']['label'], $form['entity_form']['id'], $form['entity_form']['description'], $form['entity_form']['actions']);
    // Since the overview form is further nested than expected, update the
    // #parents. See \Drupal\menu_ui\MenuForm::form().
    $form_state->set('menu_overview_form_parents', ['settings', 'entity_form', 'links']);

    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
    $this->plugin->validateConfigurationForm($form, $form_state);
    if (!$this->hasMenuOverrides()) {
      $this->getEntityForm($this->menu)->validateForm($form, $form_state);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
    $this->plugin->submitConfigurationForm($form, $form_state);
    if (!$this->hasMenuOverrides()) {
      $this->getEntityForm($this->menu)->submitForm($form, $form_state);
      $this->menu->save();
    }
  }

  /**
   * Gets the entity form for this menu.
   *
   * @param \Drupal\system\MenuInterface $menu
   *   The menu entity.
   *
   * @return \Drupal\Core\Entity\EntityFormInterface
   *   The entity form.
   */
  protected function getEntityForm(MenuInterface $menu) {
    $entity_form = $this->entityTypeManager->getFormObject('menu', 'edit');
    $entity_form->setEntity($menu);
    return $entity_form;
  }

  /**
   * {@inheritdoc}
   */
  public function setPlugin(PluginInspectionInterface $plugin) {
    $this->plugin = $plugin;
    $this->menu = $this->menuStorage->loadOverrideFree($this->plugin->getDerivativeId());
  }

  /**
   * Determines if the menu has configuration overrides.
   *
   * @return bool
   *   TRUE if the menu has configuration overrides, otherwise FALSE.
   */
  protected function hasMenuOverrides() {
    // @todo Replace the following with $this->menu->hasOverrides() in https://www.drupal.org/project/drupal/issues/2910353
    //   and remove this function.
    return $this->configFactory->get($this->menu->getEntityType()
      ->getConfigPrefix() . '.' . $this->menu->id())->hasOverrides();
  }

}