You are here

public function TokenCustomForm::form in Custom Tokens 8

Gets the actual form array to be built.

Overrides ContentEntityForm::form

See also

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

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

File

src/Form/TokenCustomForm.php, line 71

Class

TokenCustomForm
Form handler for the custom token edit forms.

Namespace

Drupal\token_custom\Form

Code

public function form(array $form, FormStateInterface $form_state) {
  $token = $this->entity;
  $form = parent::form($form, $form_state);
  if ($this->operation == 'edit') {
    $form['#title'] = $this
      ->t('Edit custom token %label', [
      '%label' => $token
        ->label(),
    ]);
  }
  $types = TokenCustomType::loadMultiple();
  $options = [];
  foreach ($types as $type) {
    $options[$type
      ->id()] = $type
      ->label();
  }
  $form['type'] = [
    '#type' => 'select',
    '#title' => 'Token type',
    '#description' => $this
      ->t('The token type determines the availability of the token according to the data in the $data array (ex. a token of type <em>node</em> will need $data[node].'),
    '#options' => $options,
    '#maxlength' => 128,
    '#default_value' => $token
      ->bundle(),
    '#weight' => -1,
  ];
  $form['machine_name']['widget'][0]['value']['#type'] = 'machine_name';
  $form['machine_name']['widget'][0]['value']['#machine_name'] = [
    'exists' => '\\Drupal\\token_custom\\Entity\\TokenCustom::load',
  ];
  $account = $this
    ->currentUser();
  $form['machine_name']['#access'] = $account
    ->hasPermission('administer custom tokens');
  $form['machine_name']['#disabled'] = !$token
    ->isNew();
  $form['machine_name']['widget'][0]['value']['#required'] = TRUE;
  $form['machine_name']['#required'] = TRUE;
  return $form;
}