PasswordPolicyRolesForm.php in Password Policy 8.3
File
src/Form/PasswordPolicyRolesForm.php
View source
<?php
namespace Drupal\password_policy\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\user\RoleStorageInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class PasswordPolicyRolesForm extends FormBase {
protected $storage;
public static function create(ContainerInterface $container) {
$entity_type_manager = $container
->get('entity_type.manager');
return new static($entity_type_manager
->getStorage('user_role'));
}
public function __construct(RoleStorageInterface $storage) {
$this->storage = $storage;
}
public function getFormId() {
return 'password_policy_roles_form';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$cached_values = $form_state
->getTemporaryValue('wizard');
$policy = $cached_values['password_policy'];
$options = [];
foreach ($this->storage
->loadMultiple() as $role) {
$options[$role
->id()] = $role
->label();
}
unset($options[AccountInterface::ANONYMOUS_ROLE]);
$form['roles'] = [
'#type' => 'checkboxes',
'#title' => $this
->t('Apply to Roles'),
'#description' => $this
->t('Select Roles to which this policy applies.'),
'#options' => $options,
'#default_value' => $policy
->getRoles(),
];
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$cached_values = $form_state
->getTemporaryValue('wizard');
$policy = $cached_values['password_policy'];
$policy
->set('roles', array_filter($form_state
->getValue('roles')));
}
}