3 namespace Drupal\outside_in\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 outside_in_block_alter()
23 class SystemMenuOffCanvasForm extends PluginFormBase implements ContainerInjectionInterface {
25 use StringTranslationTrait;
26 use RedirectDestinationTrait;
31 * @var \Drupal\Core\Block\BlockPluginInterface
36 * The menu entity that the block uses and that will be edited in this form.
38 * @var \Drupal\system\MenuInterface
43 * @var \Drupal\Core\Entity\EntityStorageInterface
45 protected $menuStorage;
48 * @var \Drupal\Core\Entity\EntityTypeManagerInterface
50 protected $entityTypeManager;
53 * SystemMenuOffCanvasForm constructor.
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
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;
68 public static function create(ContainerInterface $container) {
70 $container->get('entity_type.manager')->getStorage('menu'),
71 $container->get('entity_type.manager'),
72 $container->get('string_translation')
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;
84 $form['entity_form'] = [
86 '#title' => $this->t('Edit menu %label', ['%label' => $this->menu->label()]),
89 $form['entity_form'] += $this->getEntityForm($this->menu)->buildForm([], $form_state);
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];
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');
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']);
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);
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);
129 * Gets the entity form for this menu.
131 * @param \Drupal\system\MenuInterface $menu
134 * @return \Drupal\Core\Entity\EntityFormInterface
137 protected function getEntityForm(MenuInterface $menu) {
138 $entity_form = $this->entityTypeManager->getFormObject('menu', 'edit');
139 $entity_form->setEntity($menu);
146 public function setPlugin(PluginInspectionInterface $plugin) {
147 $this->plugin = $plugin;
148 $this->menu = $this->menuStorage->load($this->plugin->getDerivativeId());