You are here

public function GenerateKeyInput::buildConfigurationForm in Key 8

Form constructor.

Plugin forms are embedded in other forms. In order to know where the plugin form is located in the parent form, #parents and #array_parents must be known, but these are not available during the initial build phase. In order to have these properties available when building the plugin form's elements, let this method return a form element that has a #process callback and build the rest of the form in the callback. By the time the callback is executed, the element's #parents and #array_parents properties will have been set by the form API. For more documentation on #parents and #array_parents, see \Drupal\Core\Render\Element\FormElement.

Parameters

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

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form. Calling code should pass on a subform state created through \Drupal\Core\Form\SubformState::createForSubform().

Return value

array The form structure.

Overrides PluginFormInterface::buildConfigurationForm

File

src/Plugin/KeyInput/GenerateKeyInput.php, line 32

Class

GenerateKeyInput
Defines a key input that generates a key value.

Namespace

Drupal\key\Plugin\KeyInput

Code

public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
  $config = $this
    ->getConfiguration();

  // If the key value has already been generated.
  if ($config['generated']) {
    $form['key_value_message'] = [
      '#markup' => t('The key value has already been generated and will not be changed.'),
    ];
    $form['display_once'] = [
      '#type' => 'value',
      '#value' => $config['display_once'],
    ];
  }
  else {
    $form['key_value_message'] = [
      '#markup' => t('The key value will be automatically generated using the selected key type settings.'),
    ];

    // Allow the user to choose to display the key value once.
    $form['display_once'] = [
      '#type' => 'checkbox',
      '#title' => t('Display value'),
      '#description' => t('Check this to display the generated value once.'),
      '#default_value' => $config['display_once'],
    ];
  }
  $form['generated'] = [
    '#type' => 'value',
    '#value' => $config['generated'],
  ];
  return $form;
}