You are here

public function ToolbarMenuElementEditForm::buildForm in Toolbar Menu 8

Same name and namespace in other branches
  1. 8.2 src/Form/ToolbarMenuElementEditForm.php \Drupal\toolbar_menu\Form\ToolbarMenuElementEditForm::buildForm()

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides EntityForm::buildForm

File

src/Form/ToolbarMenuElementEditForm.php, line 23

Class

ToolbarMenuElementEditForm
Edit form for toolbar menu elements.

Namespace

Drupal\toolbar_menu\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $form['label'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Label'),
    '#maxlength' => 255,
    '#default_value' => $this->entity
      ->label(),
    '#required' => TRUE,
  ];
  $form['id'] = [
    '#type' => 'machine_name',
    '#title' => $this
      ->t('ID'),
    '#maxlength' => 255,
    '#default_value' => $this->entity
      ->id(),
    '#required' => TRUE,
    '#disabled' => !$this->entity
      ->isNew(),
    '#machine_name' => [
      'exists' => 'Drupal\\toolbar_menu\\Entity\\ToolbarMenuElement::load',
    ],
  ];
  $existing_menu = [];
  foreach ($this->entityTypeManager
    ->getStorage('menu')
    ->loadByProperties([
    'status' => TRUE,
  ]) as $menu) {
    $existing_menu[$menu
      ->id()] = $menu
      ->label();
  }
  $form['menu'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Menu'),
    '#options' => $existing_menu,
    '#target_type' => 'menu',
    '#validate_reference' => TRUE,
    '#default_value' => $this->entity
      ->menu(),
    '#required' => TRUE,
  ];
  $form['rewrite_label'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Display the menu label in toolbar instead of this entity label'),
    '#validate_reference' => TRUE,
    '#default_value' => $this->entity
      ->rewriteLabel(),
  ];
  return parent::buildForm($form, $form_state);
}