View source
<?php
namespace Drupal\system\Form;
use Drupal\Component\Plugin\PluginInspectionInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\PluginFormBase;
use Drupal\Core\Render\Element;
use Drupal\Core\Routing\RedirectDestinationTrait;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\system\MenuInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class SystemMenuOffCanvasForm extends PluginFormBase implements ContainerInjectionInterface {
use StringTranslationTrait;
use RedirectDestinationTrait;
protected $plugin;
protected $menu;
protected $menuStorage;
protected $entityTypeManager;
protected $configFactory;
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;
}
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'));
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = $this->plugin
->buildConfigurationForm([], $form_state);
$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);
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,
];
}
}
$form['entity_form']['links']['links']['#header'][0] = $this
->t('Link');
$form['entity_form']['links']['links']['#header'][1]['data'] = $this
->t('On');
unset($form['entity_form']['label'], $form['entity_form']['id'], $form['entity_form']['description'], $form['entity_form']['actions']);
$form_state
->set('menu_overview_form_parents', [
'settings',
'entity_form',
'links',
]);
return $form;
}
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);
}
}
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();
}
}
protected function getEntityForm(MenuInterface $menu) {
$entity_form = $this->entityTypeManager
->getFormObject('menu', 'edit');
$entity_form
->setEntity($menu);
return $entity_form;
}
public function setPlugin(PluginInspectionInterface $plugin) {
$this->plugin = $plugin;
$this->menu = $this->menuStorage
->loadOverrideFree($this->plugin
->getDerivativeId());
}
protected function hasMenuOverrides() {
return $this->configFactory
->get($this->menu
->getEntityType()
->getConfigPrefix() . '.' . $this->menu
->id())
->hasOverrides();
}
}