3 namespace Drupal\system\Form;
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;
20 * The setting_tray form handler for the SystemMenuBlock.
24 class SystemMenuOffCanvasForm extends PluginFormBase implements ContainerInjectionInterface {
26 use StringTranslationTrait;
27 use RedirectDestinationTrait;
32 * @var \Drupal\Core\Block\BlockPluginInterface
37 * The menu entity that the block uses and that will be edited in this form.
39 * @var \Drupal\system\MenuInterface
44 * @var \Drupal\Core\Entity\EntityStorageInterface
46 protected $menuStorage;
49 * @var \Drupal\Core\Entity\EntityTypeManagerInterface
51 protected $entityTypeManager;
56 * @var \Drupal\Core\Config\ConfigFactoryInterface
58 protected $configFactory;
61 * SystemMenuOffCanvasForm constructor.
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
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;
79 public static function create(ContainerInterface $container) {
81 $container->get('entity_type.manager')->getStorage('menu'),
82 $container->get('entity_type.manager'),
83 $container->get('string_translation'),
84 $container->get('config.factory')
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;
96 $form['entity_form'] = [
98 '#title' => $this->t('Edit menu %label', ['%label' => $this->menu->label()]),
100 '#access' => !$this->hasMenuOverrides() && $this->menu->access('edit'),
102 $form['entity_form'] += $this->getEntityForm($this->menu)->buildForm([], $form_state);
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];
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');
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']);
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);
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);
146 * Gets the entity form for this menu.
148 * @param \Drupal\system\MenuInterface $menu
151 * @return \Drupal\Core\Entity\EntityFormInterface
154 protected function getEntityForm(MenuInterface $menu) {
155 $entity_form = $this->entityTypeManager->getFormObject('menu', 'edit');
156 $entity_form->setEntity($menu);
163 public function setPlugin(PluginInspectionInterface $plugin) {
164 $this->plugin = $plugin;
165 $this->menu = $this->menuStorage->loadOverrideFree($this->plugin->getDerivativeId());
169 * Determines if the menu has configuration overrides.
172 * TRUE if the menu has configuration overrides, otherwise FALSE.
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();