You are here

public function KeyConfigOverrideAddForm::buildForm in Key 8

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/KeyConfigOverrideAddForm.php, line 105

Class

KeyConfigOverrideAddForm
KeyConfigOverrideAddForm class.

Namespace

Drupal\key\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $config_type = $form_state
    ->getValue('config_type');
  $config_name = $form_state
    ->getValue('config_name');
  $form['label'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Key configuration override name'),
    '#description' => $this
      ->t('A human readable name for this override.'),
    '#size' => 30,
    '#maxlength' => 64,
    '#required' => TRUE,
  ];
  $form['id'] = [
    '#type' => 'machine_name',
    '#required' => TRUE,
    '#size' => 30,
    '#maxlength' => 64,
    '#machine_name' => [
      'exists' => [
        $this->storage,
        'load',
      ],
    ],
  ];
  $entity_types = array_map(function (EntityTypeInterface $definition) {
    return $definition
      ->getLabel();
  }, $this
    ->getConfigEntityTypeDefinitions());

  // Sort the entity types by label.
  uasort($entity_types, 'strnatcasecmp');

  // Add the simple configuration type to the top of the list.
  $config_types = [
    'system.simple' => $this
      ->t('Simple configuration'),
  ] + $entity_types;
  $form['config_type'] = [
    '#title' => $this
      ->t('Configuration type'),
    '#type' => 'select',
    '#options' => $config_types,
    '#required' => TRUE,
    '#ajax' => [
      'callback' => '::changeConfigObject',
      'wrapper' => 'edit-config-object-wrapper',
    ],
  ];
  $form['config_object'] = [
    '#type' => 'container',
    '#prefix' => '<div id="edit-config-object-wrapper">',
    '#suffix' => '</div>',
  ];
  $form['config_object']['config_name'] = [
    '#title' => $this
      ->t('Configuration name'),
    '#type' => 'select',
    '#options' => $this
      ->getConfigNames($config_type),
    '#required' => TRUE,
    '#ajax' => [
      'callback' => '::changeConfigObject',
      'wrapper' => 'edit-config-object-wrapper',
    ],
  ];
  $form['config_object']['config_item'] = [
    '#title' => $this
      ->t('Configuration item'),
    '#type' => 'select',
    '#options' => $this
      ->getConfigItems($config_type, $config_name),
    '#required' => TRUE,
  ];
  $request = $this->requestStack
    ->getCurrentRequest();
  $query_key = $request->query
    ->get('key');
  $form['key_id'] = [
    '#title' => $this
      ->t('Key'),
    '#type' => 'key_select',
    '#default_value' => $query_key,
    '#required' => TRUE,
  ];
  $form['clear_overridden'] = [
    '#title' => $this
      ->t('Clear overridden value'),
    '#type' => 'checkbox',
    '#description' => $this
      ->t('Check this field to clear any existing value for the overridden configuration item. This is important to make sure potentially sensitive data is removed from the configuration.'),
    '#default_value' => TRUE,
  ];
  return parent::buildForm($form, $form_state);
}