function MenuLinkContentForm::form

Same name and namespace in other branches
  1. 9 core/modules/menu_link_content/src/Form/MenuLinkContentForm.php \Drupal\menu_link_content\Form\MenuLinkContentForm::form()
  2. 8.9.x core/modules/menu_link_content/src/Form/MenuLinkContentForm.php \Drupal\menu_link_content\Form\MenuLinkContentForm::form()
  3. 10 core/modules/menu_link_content/src/Form/MenuLinkContentForm.php \Drupal\menu_link_content\Form\MenuLinkContentForm::form()

Overrides ContentEntityForm::form

File

core/modules/menu_link_content/src/Form/MenuLinkContentForm.php, line 69

Class

MenuLinkContentForm
Provides a form to add/update content menu links.

Namespace

Drupal\menu_link_content\Form

Code

public function form(array $form, FormStateInterface $form_state) {
    $form = parent::form($form, $form_state);
    $parent_id = $this->entity
        ->getParentId() ?: $this->getRequest()->query
        ->get('parent');
    $default = $this->entity
        ->getMenuName() . ':' . $parent_id;
    $id = $this->entity
        ->isNew() ? '' : $this->entity
        ->getPluginId();
    $menu_id = $this->entity
        ->getMenuName();
    $menu = $this->entityTypeManager
        ->getStorage('menu')
        ->load($menu_id);
    if ($menu instanceof MenuInterface && $this->entity
        ->isNew()) {
        $form['menu_parent'] = $this->menuParentSelector
            ->parentSelectElement($default, $id, [
            $menu_id => $menu->label(),
        ]);
    }
    else {
        $form['menu_parent'] = $this->menuParentSelector
            ->parentSelectElement($default, $id);
    }
    $form['menu_parent']['#weight'] = 10;
    $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';
    return $form;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.