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
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\key\Plugin\KeyPluginBase implements KeyPluginInterface
- class \Drupal\key\Plugin\KeyInputBase implements KeyInputInterface, KeyPluginFormInterface
- class \Drupal\key\Plugin\KeyInput\GenerateKeyInput
- class \Drupal\key\Plugin\KeyInputBase implements KeyInputInterface, KeyPluginFormInterface
- class \Drupal\key\Plugin\KeyPluginBase implements KeyPluginInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of GenerateKeyInput
File
- src/
Plugin/ KeyInput/ GenerateKeyInput.php, line 17
Namespace
Drupal\key\Plugin\KeyInputView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
GenerateKeyInput:: |
public | function |
Form constructor. Overrides PluginFormInterface:: |
|
GenerateKeyInput:: |
public | function |
Gets default configuration for this plugin. Overrides KeyPluginBase:: |
|
GenerateKeyInput:: |
public | function |
Process a submitted key value. Overrides KeyInputBase:: |
|
KeyInputBase:: |
public | function |
Process an existing key value. Overrides KeyInputInterface:: |
1 |
KeyInputBase:: |
public | function |
Form submission handler. Overrides PluginFormInterface:: |
|
KeyInputBase:: |
public | function |
Form validation handler. Overrides PluginFormInterface:: |
|
KeyPluginBase:: |
public | function |
Calculates dependencies for the configured plugin. Overrides DependentPluginInterface:: |
|
KeyPluginBase:: |
public static | function |
Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface:: |
1 |
KeyPluginBase:: |
public | function |
Gets this plugin's configuration. Overrides ConfigurableInterface:: |
|
KeyPluginBase:: |
public | function |
Returns the type of plugin. Overrides KeyPluginInterface:: |
|
KeyPluginBase:: |
public | function |
Sets the configuration for this plugin instance. Overrides ConfigurableInterface:: |
|
KeyPluginBase:: |
public | function |
Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides PluginBase:: |
|
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |