diff --git a/core/modules/block/src/BlockForm.php b/core/modules/block/src/BlockForm.php index c96b4e8..e9d03fc 100644 --- a/core/modules/block/src/BlockForm.php +++ b/core/modules/block/src/BlockForm.php @@ -93,7 +93,7 @@ class BlockForm extends EntityForm { * @param \Drupal\Core\Plugin\PluginFormManagerInterface $plugin_form_manager * The plugin form manager. */ - public function __construct(EntityManagerInterface $entity_manager, ExecutableManagerInterface $manager, ContextRepositoryInterface $context_repository, LanguageManagerInterface $language, ThemeHandlerInterface $theme_handler, \Drupal\Core\Plugin\PluginFormManagerInterface $plugin_form_manager) { + public function __construct(EntityManagerInterface $entity_manager, ExecutableManagerInterface $manager, ContextRepositoryInterface $context_repository, LanguageManagerInterface $language, ThemeHandlerInterface $theme_handler, PluginFormManagerInterface $plugin_form_manager) { $this->storage = $entity_manager->getStorage('block'); $this->manager = $manager; $this->contextRepository = $context_repository; diff --git a/core/modules/menu_ui/src/MenuForm.php b/core/modules/menu_ui/src/MenuForm.php index c23e514..7e09fbf 100644 --- a/core/modules/menu_ui/src/MenuForm.php +++ b/core/modules/menu_ui/src/MenuForm.php @@ -173,12 +173,7 @@ public function menuNameExists($value) { */ public function save(array $form, FormStateInterface $form_state) { $menu = $this->entity; - if (!$menu->isNew() || $menu->isLocked()) { - $this->submitOverviewForm($form, $form_state); - } - $status = $menu->save(); - $edit_link = $this->entity->link($this->t('Edit')); if ($status == SAVED_UPDATED) { drupal_set_message($this->t('Menu %label has been updated.', array('%label' => $menu->label()))); @@ -193,6 +188,17 @@ public function save(array $form, FormStateInterface $form_state) { } /** + * {@inheritdoc} + */ + public function submitForm(array &$form, FormStateInterface $form_state) { + parent::submitForm($form, $form_state); + + if (!$this->entity->isNew() || $this->entity->isLocked()) { + $this->submitOverviewForm($form, $form_state); + } + } + + /** * Form constructor to edit an entire menu tree at once. * * Shows for one menu the menu links accessible to the current user and diff --git a/core/modules/outside_in/outside_in.module b/core/modules/outside_in/outside_in.module index 719c253..7132499 100644 --- a/core/modules/outside_in/outside_in.module +++ b/core/modules/outside_in/outside_in.module @@ -8,6 +8,7 @@ use Drupal\Core\Routing\RouteMatchInterface; use Drupal\outside_in\Block\BlockEntityOffCanvasForm; use Drupal\outside_in\Form\SystemBrandingOffCanvasForm; +use Drupal\outside_in\Form\SystemMenuOffCanvasForm; /** * Implements hook_help(). @@ -114,7 +115,7 @@ function outside_in_toolbar_alter(&$items) { // @todo Create a dynamic method for modules to set their own items. $edit_mode_items = ['contextual', 'block_place']; foreach ($items as $key => $item) { - if (!in_array($key, $edit_mode_items) && (!is_array($items[$key]['#wrapper_attributes']['class']) || !in_array('hidden', $items[$key]['#wrapper_attributes']['class']))) { + if (!in_array($key, $edit_mode_items) && (!isset($items[$key]['#wrapper_attributes']['class']) || !in_array('hidden', $items[$key]['#wrapper_attributes']['class']))) { $items[$key]['#wrapper_attributes']['class'][] = 'edit-mode-inactive'; } } @@ -128,4 +129,12 @@ function outside_in_block_alter(&$definitions) { if (!empty($definitions['system_branding_block'])) { $definitions['system_branding_block']['form']['offcanvas'] = SystemBrandingOffCanvasForm::class; } + + // Since menu blocks use derivatives, check the definition ID instead of + // relying on the plugin ID. + foreach ($definitions as &$definition) { + if ($definition['id'] === 'system_menu_block') { + $definition['form']['offcanvas'] = SystemMenuOffCanvasForm::class; + } + } } diff --git a/core/modules/outside_in/src/Form/SystemBrandingOffCanvasForm.php b/core/modules/outside_in/src/Form/SystemBrandingOffCanvasForm.php index 48278a8..b8254c1 100644 --- a/core/modules/outside_in/src/Form/SystemBrandingOffCanvasForm.php +++ b/core/modules/outside_in/src/Form/SystemBrandingOffCanvasForm.php @@ -20,7 +20,7 @@ class SystemBrandingOffCanvasForm implements ContainerInjectionInterface, Plugin /** * The plugin. * - * @var \Drupal\Core\Plugin\PluginFormInterface + * @var \Drupal\Core\Block\BlockPluginInterface */ protected $plugin; @@ -61,30 +61,30 @@ public function setPlugin($plugin) { * {@inheritdoc} */ public function buildConfigurationForm(array $form, FormStateInterface $form_state) { - $form = $this->plugin->buildConfigurationForm($form, $form_state); + $form['original'] = $this->plugin->buildConfigurationForm([], $form_state); + + // Unset links to Site Information form, we can make these changes here. + unset($form['original']['block_branding']['use_site_name']['#description'], $form['original']['block_branding']['use_site_slogan']['#description']); + $site_config = $this->configFactory->getEditable('system.site'); - $form['site_information'] = array( + $form['site_information'] = [ '#type' => 'details', '#title' => t('Site details'), '#open' => TRUE, '#weight' => -100, - ); - $form['site_information']['site_name'] = array( + ]; + $form['site_information']['site_name'] = [ '#type' => 'textfield', '#title' => t('Site name'), '#default_value' => $site_config->get('name'), '#required' => TRUE, - ); - $form['site_information']['site_slogan'] = array( + ]; + $form['site_information']['site_slogan'] = [ '#type' => 'textfield', '#title' => t('Slogan'), '#default_value' => $site_config->get('slogan'), '#description' => t("How this is used depends on your site's theme."), - ); - - // Unset links to Site Information form, we can make these changes here. - unset($form['block_branding']['use_site_name']['#description']); - unset($form['block_branding']['use_site_slogan']['#description']); + ]; return $form; } diff --git a/core/modules/outside_in/src/Form/SystemMenuOffCanvasForm.php b/core/modules/outside_in/src/Form/SystemMenuOffCanvasForm.php new file mode 100644 index 0000000..997d7a8 --- /dev/null +++ b/core/modules/outside_in/src/Form/SystemMenuOffCanvasForm.php @@ -0,0 +1,158 @@ +menuStorage = $menu_storage; + $this->entityTypeManager = $entity_type_manager; + $this->stringTranslation = $string_translation; + } + + /** + * {@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') + ); + } + + /** + * {@inheritdoc} + */ + public function buildConfigurationForm(array $form, FormStateInterface $form_state) { + $this->ensureFormState($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', array('%label' => $this->entity->label())), + '#open' => TRUE, + ]; + $form['entity_form'] += $this->getEntityForm($this->entity)->buildForm([], $form_state); + unset($form['entity_form']['label'], $form['entity_form']['id'], $form['entity_form']['description'], $form['entity_form']['actions']); + + return $form; + } + + /** + * {@inheritdoc} + */ + public function validateConfigurationForm(array &$form, FormStateInterface $form_state) { + $this->ensureFormState($form_state); + + $this->plugin->validateConfigurationForm($form, $form_state); + $this->getEntityForm($this->entity)->validateForm($form, $form_state); + } + + /** + * {@inheritdoc} + */ + public function submitConfigurationForm(array &$form, FormStateInterface $form_state) { + $this->ensureFormState($form_state); + + $this->plugin->submitConfigurationForm($form, $form_state); + $this->getEntityForm($this->entity)->submitForm($form, $form_state); + $this->entity->save(); + } + + /** + * Gets the entity form for this menu. + * + * @return \Drupal\Core\Form\FormInterface + */ + protected function getEntityForm(MenuInterface $entity) { + $entity_form = $this->entityTypeManager->getFormObject('menu', 'edit'); + $entity_form->setEntity($entity); + return $entity_form; + } + + /** + * Ensures the form state is set up correctly. + * + * @todo Remove this once https://www.drupal.org/node/2537732 is fixed. + * + * @param \Drupal\Core\Form\FormStateInterface $form_state + * The current state of the form. + */ + protected function ensureFormState(FormStateInterface $form_state) { + // Prepare $form_state for \Drupal\menu_ui\MenuForm::submitOverviewForm(). + $input = &$form_state->getUserInput(); + if (is_null($input)) { + $input = []; + } + + if (!$this->entity->isNew() || $this->entity->isLocked()) { + $form_state->set('menu_overview_form_parents', ['settings', 'entity_form', 'links']); + } + } + + /** + * Sets the plugin for this object. + * + * @param object $plugin + * The plugin. + */ + public function setPlugin($plugin) { + $this->plugin = $plugin; + $this->entity = $this->menuStorage->load($this->plugin->getDerivativeId()); + } + +}