You are here

public function MenuForm::form in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/menu_ui/src/MenuForm.php \Drupal\menu_ui\MenuForm::form()
  2. 10 core/modules/menu_ui/src/MenuForm.php \Drupal\menu_ui\MenuForm::form()

Gets the actual form array to be built.

Overrides EntityForm::form

See also

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

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

File

core/modules/menu_ui/src/MenuForm.php, line 99

Class

MenuForm
Base form for menu edit forms.

Namespace

Drupal\menu_ui

Code

public function form(array $form, FormStateInterface $form_state) {
  $menu = $this->entity;
  if ($this->operation == 'edit') {
    $form['#title'] = $this
      ->t('Edit menu %label', [
      '%label' => $menu
        ->label(),
    ]);
  }
  $form['label'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Title'),
    '#default_value' => $menu
      ->label(),
    '#required' => TRUE,
  ];
  $form['id'] = [
    '#type' => 'machine_name',
    '#title' => $this
      ->t('Menu name'),
    '#default_value' => $menu
      ->id(),
    '#maxlength' => MenuStorage::MAX_ID_LENGTH,
    '#description' => $this
      ->t('A unique name to construct the URL for the menu. It must only contain lowercase letters, numbers and hyphens.'),
    '#machine_name' => [
      'exists' => [
        $this,
        'menuNameExists',
      ],
      'source' => [
        'label',
      ],
      'replace_pattern' => '[^a-z0-9-]+',
      'replace' => '-',
    ],
    // A menu's machine name cannot be changed.
    '#disabled' => !$menu
      ->isNew() || $menu
      ->isLocked(),
  ];
  $form['description'] = [
    '#type' => 'textfield',
    '#title' => t('Administrative summary'),
    '#maxlength' => 512,
    '#default_value' => $menu
      ->getDescription(),
  ];
  $form['langcode'] = [
    '#type' => 'language_select',
    '#title' => t('Menu language'),
    '#languages' => LanguageInterface::STATE_ALL,
    '#default_value' => $menu
      ->language()
      ->getId(),
  ];

  // Add menu links administration form for existing menus.
  if (!$menu
    ->isNew() || $menu
    ->isLocked()) {

    // Form API supports constructing and validating self-contained sections
    // within forms, but does not allow handling the form section's submission
    // equally separated yet. Therefore, we use a $form_state key to point to
    // the parents of the form section.
    // @see self::submitOverviewForm()
    $form_state
      ->set('menu_overview_form_parents', [
      'links',
    ]);
    $form['links'] = [];
    $form['links'] = $this
      ->buildOverviewForm($form['links'], $form_state);
  }
  return parent::form($form, $form_state);
}