You are here

class GenerateKeyInput in Key 8

Defines a key input that generates a key value.

Plugin annotation


@KeyInput(
  id = "generate",
  label = @Translation("Generate"),
  description = @Translation("A key input that generates a key value.")
)

Hierarchy

Expanded class hierarchy of GenerateKeyInput

File

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

Namespace

Drupal\key\Plugin\KeyInput
View source
class GenerateKeyInput extends KeyInputBase {

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    return [
      'generated' => FALSE,
      'display_once' => TRUE,
    ];
  }

  /**
   * {@inheritdoc}
   */
  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;
  }

  /**
   * {@inheritdoc}
   */
  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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
GenerateKeyInput::buildConfigurationForm public function Form constructor. Overrides PluginFormInterface::buildConfigurationForm
GenerateKeyInput::defaultConfiguration public function Gets default configuration for this plugin. Overrides KeyPluginBase::defaultConfiguration
GenerateKeyInput::processSubmittedKeyValue public function Process a submitted key value. Overrides KeyInputBase::processSubmittedKeyValue
KeyInputBase::processExistingKeyValue public function Process an existing key value. Overrides KeyInputInterface::processExistingKeyValue 1
KeyInputBase::submitConfigurationForm public function Form submission handler. Overrides PluginFormInterface::submitConfigurationForm
KeyInputBase::validateConfigurationForm public function Form validation handler. Overrides PluginFormInterface::validateConfigurationForm
KeyPluginBase::calculateDependencies public function Calculates dependencies for the configured plugin. Overrides DependentPluginInterface::calculateDependencies
KeyPluginBase::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create 1
KeyPluginBase::getConfiguration public function Gets this plugin's configuration. Overrides ConfigurableInterface::getConfiguration
KeyPluginBase::getPluginType public function Returns the type of plugin. Overrides KeyPluginInterface::getPluginType
KeyPluginBase::setConfiguration public function Sets the configuration for this plugin instance. Overrides ConfigurableInterface::setConfiguration
KeyPluginBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides PluginBase::__construct
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.