View source
<?php
namespace Drupal\password_policy\Form;
use Drupal\Component\Plugin\PluginManagerInterface;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\CloseModalDialogCommand;
use Drupal\Core\Ajax\RedirectCommand;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\Core\TempStore\SharedTempStoreFactory;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ConstraintEdit extends FormBase {
protected $tempstore;
protected $manager;
protected $tempstoreId = 'password_policy.password_policy';
protected $machineName;
public static function create(ContainerInterface $container) {
return new static($container
->get('tempstore.shared'), $container
->get('plugin.manager.password_policy.password_constraint'));
}
public function __construct(SharedTempStoreFactory $tempstore, PluginManagerInterface $manager) {
$this->tempstore = $tempstore;
$this->manager = $manager;
}
public function getFormId() {
return 'password_policy_constraint_edit_form';
}
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);
$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, []);
}
$form = $instance
->buildConfigurationForm($form, $form_state);
if (isset($id)) {
$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;
}
public function validateForm(array &$form, FormStateInterface $form_state) {
$instance = $form_state
->getValue('instance');
$instance
->validateConfigurationForm($form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$cached_values = $this->tempstore
->get($this->tempstoreId)
->get($this->machineName);
$policy = $cached_values['password_policy'];
$constraints = $policy
->getConstraints();
$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',
]);
}
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;
}
}