View source
<?php
namespace Drupal\Core\Menu\Form;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Menu\MenuLinkInterface;
use Drupal\Core\Menu\MenuLinkManagerInterface;
use Drupal\Core\Menu\MenuParentFormSelectorInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\StringTranslation\TranslationInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class MenuLinkDefaultForm implements MenuLinkFormInterface, ContainerInjectionInterface {
use StringTranslationTrait;
protected $menuLink;
protected $menuLinkManager;
protected $menuParentSelector;
protected $moduleHandler;
protected $moduleData;
public function __construct(MenuLinkManagerInterface $menu_link_manager, MenuParentFormSelectorInterface $menu_parent_selector, TranslationInterface $string_translation, ModuleHandlerInterface $module_handler) {
$this->menuLinkManager = $menu_link_manager;
$this->menuParentSelector = $menu_parent_selector;
$this->stringTranslation = $string_translation;
$this->moduleHandler = $module_handler;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('plugin.manager.menu.link'), $container
->get('menu.parent_form_selector'), $container
->get('string_translation'), $container
->get('module_handler'));
}
public function setMenuLinkInstance(MenuLinkInterface $menu_link) {
$this->menuLink = $menu_link;
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form['#title'] = $this
->t('Edit menu link %title', array(
'%title' => $this->menuLink
->getTitle(),
));
$provider = $this->menuLink
->getProvider();
$form['info'] = array(
'#type' => 'item',
'#title' => $this
->t('This link is provided by the @name module. The title and path cannot be edited.', array(
'@name' => $this->moduleHandler
->getName($provider),
)),
);
$link = array(
'#type' => 'link',
'#title' => $this->menuLink
->getTitle(),
'#url' => $this->menuLink
->getUrlObject(),
);
$form['path'] = array(
'link' => $link,
'#type' => 'item',
'#title' => $this
->t('Link'),
);
$form['enabled'] = array(
'#type' => 'checkbox',
'#title' => $this
->t('Enable menu link'),
'#description' => $this
->t('Menu links that are not enabled will not be listed in any menu.'),
'#default_value' => $this->menuLink
->isEnabled(),
);
$form['expanded'] = array(
'#type' => 'checkbox',
'#title' => t('Show as expanded'),
'#description' => $this
->t('If selected and this menu link has children, the menu will always appear expanded.'),
'#default_value' => $this->menuLink
->isExpanded(),
);
$menu_parent = $this->menuLink
->getMenuName() . ':' . $this->menuLink
->getParent();
$form['menu_parent'] = $this->menuParentSelector
->parentSelectElement($menu_parent, $this->menuLink
->getPluginId());
$form['menu_parent']['#title'] = $this
->t('Parent link');
$form['menu_parent']['#description'] = $this
->t('The maximum depth for a link and all its children is fixed. Some menu links may not be available as parents if selecting them would exceed this limit.');
$form['menu_parent']['#attributes']['class'][] = 'menu-title-select';
$delta = max(abs($this->menuLink
->getWeight()), 50);
$form['weight'] = array(
'#type' => 'number',
'#min' => -$delta,
'#max' => $delta,
'#default_value' => $this->menuLink
->getWeight(),
'#title' => $this
->t('Weight'),
'#description' => $this
->t('Link weight among links in the same menu at the same depth. In the menu, the links with high weight will sink and links with a low weight will be positioned nearer the top.'),
);
return $form;
}
public function extractFormValues(array &$form, FormStateInterface $form_state) {
$new_definition = array();
$new_definition['enabled'] = $form_state
->getValue('enabled') ? 1 : 0;
$new_definition['weight'] = (int) $form_state
->getValue('weight');
$new_definition['expanded'] = $form_state
->getValue('expanded') ? 1 : 0;
list($menu_name, $parent) = explode(':', $form_state
->getValue('menu_parent'), 2);
if (!empty($menu_name)) {
$new_definition['menu_name'] = $menu_name;
}
if (isset($parent)) {
$new_definition['parent'] = $parent;
}
return $new_definition;
}
public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
$new_definition = $this
->extractFormValues($form, $form_state);
return $this->menuLinkManager
->updateDefinition($this->menuLink
->getPluginId(), $new_definition);
}
}