You are here

public function MenuLinkConfigForm::form in Config menu link 8

Gets the actual form array to be built.

Overrides EntityForm::form

See also

\Drupal\Core\Entity\EntityForm::processForm()

\Drupal\Core\Entity\EntityForm::afterBuild()

1 call to MenuLinkConfigForm::form()
MenuLinkConfigForm::buildConfigurationForm in src/Plugin/Menu/Form/MenuLinkConfigForm.php
Form constructor.

File

src/Plugin/Menu/Form/MenuLinkConfigForm.php, line 137
Contains \Drupal\menu_link_config\Plugin\Menu\Form\MenuLinkConfigForm.

Class

MenuLinkConfigForm

Namespace

Drupal\menu_link_config\Plugin\Menu\Form

Code

public function form(array $form, FormStateInterface $form_state) {
  parent::form($form, $form_state);
  $form['#title'] = $this
    ->t('Edit menu link %title', [
    '%title' => $this->entity
      ->getTitle(),
  ]);

  // Put the title field first.
  $form['title'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Title'),
    '#default_value' => $this->entity
      ->getTitle(),
    '#weight' => -10,
  ];
  $form['id'] = [
    '#type' => 'machine_name',
    '#maxlength' => 128,
    '#machine_name' => [
      'source' => [
        'title',
      ],
      'exists' => '\\Drupal\\menu_link_config\\Controller\\MenuController::getMenuLink',
    ],
    '#disabled' => !$this->entity
      ->isNew(),
    '#weight' => -9,
  ];
  $form['description'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Description'),
    '#description' => $this
      ->t('Shown when hovering over the menu link.'),
    '#default_value' => $this->entity
      ->getDescription(),
    '#weight' => -5,
  ];
  $link = [
    '#type' => 'link',
    '#title' => $this->entity
      ->getTitle(),
  ] + $this->entity
    ->getUrlObject()
    ->toRenderArray();
  $form['info'] = [
    'link' => $link,
    '#type' => 'item',
    '#title' => $this
      ->t('Link'),
  ];

  // We always show the internal path here.

  /** @var \Drupal\Core\Url $url */
  $url = $this->entity
    ->getUrlObject();
  if ($url
    ->isExternal()) {
    $default_value = $url
      ->toString();
  }
  elseif ($url
    ->getRouteName() == '<front>') {

    // The default route for new entities is <front>, but we just want an
    // empty form field.
    $default_value = $this->entity
      ->isNew() ? '' : '<front>';
  }
  else {

    // @todo Url::getInternalPath() calls UrlGenerator::getPathFromRoute()
    // which need a replacement since it is deprecated.
    // https://www.drupal.org/node/2307061
    try {
      $default_value = $url
        ->getInternalPath();
    } catch (\Exception $e) {
      $default_value = 'broken path';
    }

    // @todo Add a helper method to Url to render just the query string and
    // fragment. https://www.drupal.org/node/2305013
    $options = $url
      ->getOptions();
    if (isset($options['query'])) {
      $default_value .= $options['query'] ? '?' . UrlHelper::buildQuery($options['query']) : '';
    }
    if (isset($options['fragment']) && $options['fragment'] !== '') {
      $default_value .= '#' . $options['fragment'];
    }
  }
  $form['url'] = [
    '#title' => $this
      ->t('Link path'),
    '#type' => 'textfield',
    '#description' => $this
      ->t('The path for this menu link. This can be an internal Drupal path such as %add-node or an external URL such as %drupal. Enter %front to link to the front page.', [
      '%front' => '<front>',
      '%add-node' => '/node/add',
      '%drupal' => 'http://drupal.org',
    ]),
    '#default_value' => $default_value,
    '#required' => TRUE,
    '#weight' => -2,
  ];
  $form['enabled'] = [
    '#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->entity
      ->status(),
  ];
  $form['expanded'] = [
    '#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->entity
      ->isExpanded(),
  ];
  $menu_parent = $this->entity
    ->getMenuName() . ':' . $this->entity
    ->getParent();
  $form['menu_parent'] = $this->menuParentSelector
    ->parentSelectElement($menu_parent, $this->entity
    ->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->entity
    ->getWeight()), 50);
  $form['weight'] = [
    '#type' => 'number',
    '#min' => -$delta,
    '#max' => $delta,
    '#default_value' => $this->entity
      ->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;
}