class ConstraintEdit in Password Policy 8.3
Editing a constraint within the policy wizard form.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\password_policy\Form\ConstraintEdit
Expanded class hierarchy of ConstraintEdit
1 string reference to 'ConstraintEdit'
File
- src/
Form/ ConstraintEdit.php, line 18
Namespace
Drupal\password_policy\FormView source
class ConstraintEdit extends FormBase {
/**
* Adding a tempstore for the multiple steps of the wizard form.
*
* @var \Drupal\Core\TempStore\SharedTempStoreFactory
*/
protected $tempstore;
/**
* Plugin manager of the password constraints.
*
* @var \Drupal\Component\Plugin\PluginManagerInterface
*/
protected $manager;
/**
* Identifier of the wizard's tempstore.
*
* @var string
*/
protected $tempstoreId = 'password_policy.password_policy';
/**
* Machine name of the form step.
*
* @var string
*/
protected $machineName;
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('tempstore.shared'), $container
->get('plugin.manager.password_policy.password_constraint'));
}
/**
* Overriding the constructor to load in the plugin manager and tempstore.
*
* @param \Drupal\Core\TempStore\SharedTempStoreFactory $tempstore
* The tempstore of the wizard form.
* @param \Drupal\Component\Plugin\PluginManagerInterface $manager
* The plugin manager for the password constraints.
*/
public function __construct(SharedTempStoreFactory $tempstore, PluginManagerInterface $manager) {
$this->tempstore = $tempstore;
$this->manager = $manager;
}
/**
* Returns a unique string identifying the form.
*
* @return string
* The unique string identifying the form.
*/
public function getFormId() {
return 'password_policy_constraint_edit_form';
}
/**
* Form constructor.
*
* @param array $form
* An associative array containing the structure of the form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
* @param string $constraint_id
* Plugin ID of the constraint.
* @param string $machine_name
* Machine name of this form step.
*
* @return array
* The form structure.
*/
public function buildForm(array $form, FormStateInterface $form_state, $constraint_id = NULL, $machine_name = NULL) {
$this->machineName = $machine_name;
$cached_values = $this->tempstore
->get($this->tempstoreId)
->get($this->machineName);
/** @var \Drupal\password_policy\Entity\PasswordPolicy $policy */
$policy = $cached_values['password_policy'];
if (is_numeric($constraint_id)) {
$id = $constraint_id;
$constraint_id = $policy
->getConstraint($id);
$instance = $this->manager
->createInstance($constraint_id['id'], $constraint_id);
}
else {
$instance = $this->manager
->createInstance($constraint_id, []);
}
/** @var \Drupal\password_policy\PasswordConstraintInterface $instance */
$form = $instance
->buildConfigurationForm($form, $form_state);
if (isset($id)) {
// Conditionally set this form element so that we can update or add.
$form['id'] = [
'#type' => 'value',
'#value' => $id,
];
}
$form['instance'] = [
'#type' => 'value',
'#value' => $instance,
];
$form['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Save'),
'#ajax' => [
'callback' => [
$this,
'ajaxSave',
],
],
];
return $form;
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
$instance = $form_state
->getValue('instance');
$instance
->validateConfigurationForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$cached_values = $this->tempstore
->get($this->tempstoreId)
->get($this->machineName);
/** @var \Drupal\password_policy\Entity\PasswordPolicy $policy */
$policy = $cached_values['password_policy'];
$constraints = $policy
->getConstraints();
/** @var \Drupal\password_policy\PasswordConstraintInterface $instance */
$instance = $form_state
->getValue('instance');
$instance
->submitConfigurationForm($form, $form_state);
if ($form_state
->hasValue('id')) {
$constraints[$form_state
->getValue('id')] = $instance
->getConfiguration();
}
else {
$constraints[] = $instance
->getConfiguration();
}
$policy
->set('policy_constraints', $constraints);
$this->tempstore
->get($this->tempstoreId)
->set($this->machineName, $cached_values);
$form_state
->setRedirect('entity.password_policy.wizard.edit', [
'machine_name' => $this->machineName,
'step' => 'constraint',
]);
}
/**
* {@inheritdoc}
*/
public function ajaxSave(array &$form, FormStateInterface $form_state) {
$response = new AjaxResponse();
$url = Url::fromRoute('entity.password_policy.wizard.edit', [
'machine_name' => $this->machineName,
'step' => 'constraint',
]);
$response
->addCommand(new RedirectCommand($url
->toString()));
$response
->addCommand(new CloseModalDialogCommand());
return $response;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ConstraintEdit:: |
protected | property | Machine name of the form step. | |
ConstraintEdit:: |
protected | property | Plugin manager of the password constraints. | |
ConstraintEdit:: |
protected | property | Adding a tempstore for the multiple steps of the wizard form. | |
ConstraintEdit:: |
protected | property | Identifier of the wizard's tempstore. | |
ConstraintEdit:: |
public | function | ||
ConstraintEdit:: |
public | function |
Form constructor. Overrides FormInterface:: |
|
ConstraintEdit:: |
public static | function |
Instantiates a new instance of this class. Overrides FormBase:: |
|
ConstraintEdit:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
ConstraintEdit:: |
public | function |
Form submission handler. Overrides FormInterface:: |
|
ConstraintEdit:: |
public | function |
Form validation handler. Overrides FormBase:: |
|
ConstraintEdit:: |
public | function | Overriding the constructor to load in the plugin manager and tempstore. | |
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 | |
FormBase:: |
protected | property | The config factory. | 1 |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Retrieves a configuration object. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 1 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
protected | function | Gets the current user. | |
FormBase:: |
protected | function | Gets the request object. | |
FormBase:: |
protected | function | Gets the route match. | |
FormBase:: |
protected | function | Gets the logger for a specific channel. | |
FormBase:: |
protected | function |
Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: |
|
FormBase:: |
public | function | Resets the configuration factory. | |
FormBase:: |
public | function | Sets the config factory for this form. | |
FormBase:: |
public | function | Sets the request stack object to use. | |
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
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. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. |