3 namespace Drupal\settings_tray\Form;
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;
19 * The off-canvas form handler for the SystemMenuBlock.
21 * @see settings_tray_block_alter()
25 class SystemMenuOffCanvasForm extends PluginFormBase implements ContainerInjectionInterface {
27 use StringTranslationTrait;
28 use RedirectDestinationTrait;
33 * @var \Drupal\Core\Block\BlockPluginInterface
38 * The menu entity that the block uses and that will be edited in this form.
40 * @var \Drupal\system\MenuInterface
45 * @var \Drupal\Core\Entity\EntityStorageInterface
47 protected $menuStorage;
50 * @var \Drupal\Core\Entity\EntityTypeManagerInterface
52 protected $entityTypeManager;
55 * SystemMenuOffCanvasForm constructor.
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
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;
70 public static function create(ContainerInterface $container) {
72 $container->get('entity_type.manager')->getStorage('menu'),
73 $container->get('entity_type.manager'),
74 $container->get('string_translation')
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;
86 $form['entity_form'] = [
88 '#title' => $this->t('Edit menu %label', ['%label' => $this->menu->label()]),
90 '#access' => $this->menu->access('edit'),
92 $form['entity_form'] += $this->getEntityForm($this->menu)->buildForm([], $form_state);
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];
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');
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']);
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);
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);
132 * Gets the entity form for this menu.
134 * @param \Drupal\system\MenuInterface $menu
137 * @return \Drupal\Core\Entity\EntityFormInterface
140 protected function getEntityForm(MenuInterface $menu) {
141 $entity_form = $this->entityTypeManager->getFormObject('menu', 'edit');
142 $entity_form->setEntity($menu);
149 public function setPlugin(PluginInspectionInterface $plugin) {
150 $this->plugin = $plugin;
151 $this->menu = $this->menuStorage->load($this->plugin->getDerivativeId());