You are here

public function GenerateKeyInput::processSubmittedKeyValue in Key 8

Process a submitted key value.

Parameters

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

Return value

array The submitted value (with index "submitted") and the processed value (with index "processed_submitted").

Overrides KeyInputBase::processSubmittedKeyValue

File

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

Class

GenerateKeyInput
Defines a key input that generates a key value.

Namespace

Drupal\key\Plugin\KeyInput

Code

public function processSubmittedKeyValue(FormStateInterface $form_state) {
  $key_input_settings = $form_state
    ->getValues();
  $key_value_data = $form_state
    ->get('key_value');

  // If the key value has already been generated, use the existing value.
  // Otherwise, generate a key.
  if ($key_input_settings['generated']) {
    $processed_values = [
      'submitted' => $key_value_data['current'],
      'processed_submitted' => $key_value_data['current'],
    ];
  }
  else {

    /** @var \Drupal\key\Entity\Key $key */
    $key = $form_state
      ->getFormObject()
      ->getEntity();
    $key_type = $key
      ->getKeyType();

    // Generate the key value using the key type configuration.
    $key_value = $key_type::generateKeyValue($form_state
      ->getUserInput()['key_type_settings']);
    $processed_values = [
      'submitted' => $key_value,
      'processed_submitted' => $key_value,
    ];
    $form_state
      ->setValue('generated', TRUE);

    // If the user requested to display the generated password.
    if ($key_input_settings['display_once']) {
      $this
        ->messenger()
        ->addMessage(t('A key value of the requested type has been generated and is displayed below as a Base64-encoded string. You will need to decode it to get the actual key value, which may or may not be human-readable. The key value will not be displayed again, so take note of it now, if necessary.<br>%key_value', [
        '%key_value' => base64_encode($key_value),
      ]));
    }
  }
  return $processed_values;
}