You are here

public function KeyFormBase::form in Key 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 KeyFormBase::form()
KeyEditForm::form in src/Form/KeyEditForm.php
Gets the actual form array to be built.
1 method overrides KeyFormBase::form()
KeyEditForm::form in src/Form/KeyEditForm.php
Gets the actual form array to be built.

File

src/Form/KeyFormBase.php, line 85

Class

KeyFormBase
Base form for key add and edit forms.

Namespace

Drupal\key\Form

Code

public function form(array $form, FormStateInterface $form_state) {

  /* @var $key \Drupal\key\Entity\Key */
  $key = $this->entity;
  $form['label'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Key name'),
    '#maxlength' => 255,
    '#default_value' => $key
      ->label(),
    '#required' => TRUE,
  ];
  $form['id'] = [
    '#type' => 'machine_name',
    '#default_value' => $key
      ->id(),
    '#machine_name' => [
      'exists' => [
        $this->storage,
        'load',
      ],
    ],
    '#disabled' => !$key
      ->isNew(),
  ];
  $form['description'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Description'),
    '#default_value' => $key
      ->getDescription(),
    '#description' => $this
      ->t('A short description of the key.'),
  ];

  // This is the element that contains all of the dynamic parts of the form.
  $form['settings'] = [
    '#type' => 'container',
    '#prefix' => '<div id="key-settings">',
    '#suffix' => '</div>',
  ];

  // Key type section.
  $form['settings']['type_section'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Type settings'),
    '#open' => TRUE,
  ];
  $form['settings']['type_section']['key_type'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Key type'),
    '#options' => $key
      ->getPluginsAsOptions('key_type'),
    '#required' => TRUE,
    '#default_value' => $key
      ->getKeyType()
      ->getPluginId(),
    '#ajax' => [
      'callback' => [
        $this,
        'ajaxUpdateSettings',
      ],
      'event' => 'change',
      'wrapper' => 'key-settings',
    ],
  ];
  $form['settings']['type_section']['key_type_description'] = [
    '#markup' => $key
      ->getKeyType()
      ->getPluginDefinition()['description'],
  ];
  $form['settings']['type_section']['key_type_settings'] = [
    '#type' => 'container',
    '#title' => $this
      ->t('Key type settings'),
    '#title_display' => FALSE,
    '#tree' => TRUE,
  ];
  if ($key
    ->getKeyType() instanceof KeyPluginFormInterface) {
    $plugin_form_state = $this
      ->createPluginFormState('key_type', $form_state);
    $form['settings']['type_section']['key_type_settings'] += $key
      ->getKeyType()
      ->buildConfigurationForm([], $plugin_form_state);
    $form_state
      ->setValue('key_type_settings', $plugin_form_state
      ->getValues());
  }

  // Key provider section.
  $form['settings']['provider_section'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Provider settings'),
    '#open' => TRUE,
  ];
  $form['settings']['provider_section']['key_provider'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Key provider'),
    '#options' => $key
      ->getPluginsAsOptions('key_provider'),
    '#required' => TRUE,
    '#default_value' => $key
      ->getKeyProvider()
      ->getPluginId(),
    '#ajax' => [
      'callback' => [
        $this,
        'ajaxUpdateSettings',
      ],
      'event' => 'change',
      'wrapper' => 'key-settings',
    ],
  ];
  $form['settings']['provider_section']['key_provider_description'] = [
    '#markup' => $key
      ->getKeyProvider()
      ->getPluginDefinition()['description'],
  ];
  $form['settings']['provider_section']['key_provider_settings'] = [
    '#type' => 'container',
    '#title' => $this
      ->t('Key provider settings'),
    '#title_display' => FALSE,
    '#tree' => TRUE,
  ];
  if ($key
    ->getKeyProvider() instanceof KeyPluginFormInterface) {
    $plugin_form_state = $this
      ->createPluginFormState('key_provider', $form_state);
    $form['settings']['provider_section']['key_provider_settings'] += $key
      ->getKeyProvider()
      ->buildConfigurationForm([], $plugin_form_state);
    $form_state
      ->setValue('key_provider_settings', $plugin_form_state
      ->getValues());
  }

  // Key input section.
  $form['settings']['input_section'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Value'),
    '#open' => TRUE,
  ];
  $form['settings']['input_section']['key_input'] = [
    '#type' => 'value',
    '#value' => $key
      ->getKeyInput()
      ->getPluginId(),
  ];
  $form['settings']['input_section']['key_input_settings'] = [
    '#type' => 'container',
    '#title' => $this
      ->t('Key value settings'),
    '#title_display' => FALSE,
    '#tree' => TRUE,
  ];
  if ($key
    ->getKeyInput() instanceof KeyPluginFormInterface) {
    $plugin_form_state = $this
      ->createPluginFormState('key_input', $form_state);
    $form['settings']['input_section']['key_input_settings'] += $key
      ->getKeyInput()
      ->buildConfigurationForm([], $plugin_form_state);
    $form_state
      ->setValue('key_input_settings', $plugin_form_state
      ->getValues());
  }
  return parent::form($form, $form_state);
}