"Fossies" - the Fresh Open Source Software Archive

Member "drupal-8.9.10/core/lib/Drupal/Core/Menu/Form/MenuLinkDefaultForm.php" (26 Nov 2020, 6574 Bytes) of package /linux/www/drupal-8.9.10.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "MenuLinkDefaultForm.php" see the Fossies "Dox" file reference documentation.

    1 <?php
    2 
    3 namespace Drupal\Core\Menu\Form;
    4 
    5 use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
    6 use Drupal\Core\Form\FormStateInterface;
    7 use Drupal\Core\Menu\MenuLinkInterface;
    8 use Drupal\Core\Menu\MenuLinkManagerInterface;
    9 use Drupal\Core\Menu\MenuParentFormSelectorInterface;
   10 use Drupal\Core\Extension\ModuleHandlerInterface;
   11 use Drupal\Core\StringTranslation\StringTranslationTrait;
   12 use Drupal\Core\StringTranslation\TranslationInterface;
   13 use Symfony\Component\DependencyInjection\ContainerInterface;
   14 
   15 /**
   16  * Provides an edit form for static menu links.
   17  *
   18  * @see \Drupal\Core\Menu\MenuLinkDefault
   19  */
   20 class MenuLinkDefaultForm implements MenuLinkFormInterface, ContainerInjectionInterface {
   21 
   22   use StringTranslationTrait;
   23 
   24   /**
   25    * The edited menu link.
   26    *
   27    * @var \Drupal\Core\Menu\MenuLinkInterface
   28    */
   29   protected $menuLink;
   30 
   31   /**
   32    * The menu link manager.
   33    *
   34    * @var \Drupal\Core\Menu\MenuLinkManagerInterface
   35    */
   36   protected $menuLinkManager;
   37 
   38   /**
   39    * The parent form selector service.
   40    *
   41    * @var \Drupal\Core\Menu\MenuParentFormSelectorInterface
   42    */
   43   protected $menuParentSelector;
   44 
   45   /**
   46    * The module handler service.
   47    *
   48    * @var \Drupal\Core\Extension\ModuleHandlerInterface
   49    */
   50   protected $moduleHandler;
   51 
   52   /**
   53    * Constructs a new \Drupal\Core\Menu\Form\MenuLinkDefaultForm.
   54    *
   55    * @param \Drupal\Core\Menu\MenuLinkManagerInterface $menu_link_manager
   56    *   The menu link manager.
   57    * @param \Drupal\Core\Menu\MenuParentFormSelectorInterface $menu_parent_selector
   58    *   The menu parent form selector service.
   59    * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
   60    *   The string translation.
   61    * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   62    *   The module handler;
   63    */
   64   public function __construct(MenuLinkManagerInterface $menu_link_manager, MenuParentFormSelectorInterface $menu_parent_selector, TranslationInterface $string_translation, ModuleHandlerInterface $module_handler) {
   65     $this->menuLinkManager = $menu_link_manager;
   66     $this->menuParentSelector = $menu_parent_selector;
   67     $this->stringTranslation = $string_translation;
   68     $this->moduleHandler = $module_handler;
   69   }
   70 
   71   /**
   72    * {@inheritdoc}
   73    */
   74   public static function create(ContainerInterface $container) {
   75     return new static(
   76       $container->get('plugin.manager.menu.link'),
   77       $container->get('menu.parent_form_selector'),
   78       $container->get('string_translation'),
   79       $container->get('module_handler')
   80     );
   81   }
   82 
   83   /**
   84    * {@inheritdoc}
   85    */
   86   public function setMenuLinkInstance(MenuLinkInterface $menu_link) {
   87     $this->menuLink = $menu_link;
   88   }
   89 
   90   /**
   91    * {@inheritdoc}
   92    */
   93   public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
   94     $form['#title'] = $this->t('Edit menu link %title', ['%title' => $this->menuLink->getTitle()]);
   95 
   96     $provider = $this->menuLink->getProvider();
   97     $form['info'] = [
   98       '#type' => 'item',
   99       '#title' => $this->t('This link is provided by the @name module. The title and path cannot be edited.', ['@name' => $this->moduleHandler->getName($provider)]),
  100     ];
  101     $form['id'] = [
  102       '#type' => 'value',
  103       '#value' => $this->menuLink->getPluginId(),
  104     ];
  105     $link = [
  106       '#type' => 'link',
  107       '#title' => $this->menuLink->getTitle(),
  108       '#url' => $this->menuLink->getUrlObject(),
  109     ];
  110     $form['path'] = [
  111       'link' => $link,
  112       '#type' => 'item',
  113       '#title' => $this->t('Link'),
  114     ];
  115 
  116     $form['enabled'] = [
  117       '#type' => 'checkbox',
  118       '#title' => $this->t('Enable menu link'),
  119       '#description' => $this->t('Menu links that are not enabled will not be listed in any menu.'),
  120       '#default_value' => $this->menuLink->isEnabled(),
  121     ];
  122 
  123     $form['expanded'] = [
  124       '#type' => 'checkbox',
  125       '#title' => t('Show as expanded'),
  126       '#description' => $this->t('If selected and this menu link has children, the menu will always appear expanded. This option may be overridden for the entire menu tree when placing a menu block.'),
  127       '#default_value' => $this->menuLink->isExpanded(),
  128     ];
  129 
  130     $menu_parent = $this->menuLink->getMenuName() . ':' . $this->menuLink->getParent();
  131     $form['menu_parent'] = $this->menuParentSelector->parentSelectElement($menu_parent, $this->menuLink->getPluginId());
  132     $form['menu_parent']['#title'] = $this->t('Parent link');
  133     $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.');
  134     $form['menu_parent']['#attributes']['class'][] = 'menu-title-select';
  135 
  136     $delta = max(abs($this->menuLink->getWeight()), 50);
  137     $form['weight'] = [
  138       '#type' => 'number',
  139       '#min' => -$delta,
  140       '#max' => $delta,
  141       '#default_value' => $this->menuLink->getWeight(),
  142       '#title' => $this->t('Weight'),
  143       '#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.'),
  144     ];
  145 
  146     return $form;
  147   }
  148 
  149   /**
  150    * {@inheritdoc}
  151    */
  152   public function extractFormValues(array &$form, FormStateInterface $form_state) {
  153     // Start from the complete, original, definition.
  154     $new_definition = $this->menuLink->getPluginDefinition();
  155     // Since the ID may not be present in the definition used to construct the
  156     // plugin, add it here so it's available to any consumers of this method.
  157     $new_definition['id'] = $form_state->getValue('id');
  158     $new_definition['enabled'] = $form_state->getValue('enabled') ? 1 : 0;
  159     $new_definition['weight'] = (int) $form_state->getValue('weight');
  160     $new_definition['expanded'] = $form_state->getValue('expanded') ? 1 : 0;
  161     list($menu_name, $parent) = explode(':', $form_state->getValue('menu_parent'), 2);
  162     if (!empty($menu_name)) {
  163       $new_definition['menu_name'] = $menu_name;
  164     }
  165     if (isset($parent)) {
  166       $new_definition['parent'] = $parent;
  167     }
  168     return $new_definition;
  169   }
  170 
  171   /**
  172    * {@inheritdoc}
  173    */
  174   public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
  175   }
  176 
  177   /**
  178    * {@inheritdoc}
  179    */
  180   public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
  181     $new_definition = $this->extractFormValues($form, $form_state);
  182 
  183     return $this->menuLinkManager->updateDefinition($this->menuLink->getPluginId(), $new_definition);
  184   }
  185 
  186 }