class SystemMenuOffCanvasForm

Same name in other branches
  1. 9 core/modules/system/src/Form/SystemMenuOffCanvasForm.php \Drupal\system\Form\SystemMenuOffCanvasForm
  2. 8.9.x core/modules/system/src/Form/SystemMenuOffCanvasForm.php \Drupal\system\Form\SystemMenuOffCanvasForm
  3. 11.x core/modules/system/src/Form/SystemMenuOffCanvasForm.php \Drupal\system\Form\SystemMenuOffCanvasForm

The setting_tray form handler for the SystemMenuBlock.

@internal

Hierarchy

Expanded class hierarchy of SystemMenuOffCanvasForm

1 file declares its use of SystemMenuOffCanvasForm
SystemMenuBlock.php in core/modules/system/src/Plugin/Block/SystemMenuBlock.php

File

core/modules/system/src/Form/SystemMenuOffCanvasForm.php, line 24

Namespace

Drupal\system\Form
View source
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
     *   The menu storage handler.
     * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
     *   The entity type manager.
     * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
     *   The string translation handler.
     * @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' => !$this->hasMenuOverrides() && $this->menu
                ->access('edit'),
        ];
        $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();
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 2
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 3
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
SystemMenuOffCanvasForm::$configFactory protected property The config factory.
SystemMenuOffCanvasForm::$entityTypeManager protected property
SystemMenuOffCanvasForm::$menu protected property The menu entity that the block uses and that will be edited in this form.
SystemMenuOffCanvasForm::$menuStorage protected property
SystemMenuOffCanvasForm::$plugin protected property The plugin. Overrides PluginFormBase::$plugin
SystemMenuOffCanvasForm::buildConfigurationForm public function Form constructor. Overrides PluginFormInterface::buildConfigurationForm
SystemMenuOffCanvasForm::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create
SystemMenuOffCanvasForm::getEntityForm protected function Gets the entity form for this menu.
SystemMenuOffCanvasForm::hasMenuOverrides protected function Determines if the menu has configuration overrides.
SystemMenuOffCanvasForm::setPlugin public function Sets the plugin for this object. Overrides PluginFormBase::setPlugin
SystemMenuOffCanvasForm::submitConfigurationForm public function Form submission handler. Overrides PluginFormInterface::submitConfigurationForm
SystemMenuOffCanvasForm::validateConfigurationForm public function Form validation handler. Overrides PluginFormBase::validateConfigurationForm
SystemMenuOffCanvasForm::__construct public function SystemMenuOffCanvasForm constructor.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.