You are here

class ConstraintEdit in Password Policy 8.3

Editing a constraint within the policy wizard form.

Hierarchy

Expanded class hierarchy of ConstraintEdit

1 string reference to 'ConstraintEdit'
password_policy.routing.yml in ./password_policy.routing.yml
password_policy.routing.yml

File

src/Form/ConstraintEdit.php, line 18

Namespace

Drupal\password_policy\Form
View 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

Namesort descending Modifiers Type Description Overrides
ConstraintEdit::$machineName protected property Machine name of the form step.
ConstraintEdit::$manager protected property Plugin manager of the password constraints.
ConstraintEdit::$tempstore protected property Adding a tempstore for the multiple steps of the wizard form.
ConstraintEdit::$tempstoreId protected property Identifier of the wizard's tempstore.
ConstraintEdit::ajaxSave public function
ConstraintEdit::buildForm public function Form constructor. Overrides FormInterface::buildForm
ConstraintEdit::create public static function Instantiates a new instance of this class. Overrides FormBase::create
ConstraintEdit::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
ConstraintEdit::submitForm public function Form submission handler. Overrides FormInterface::submitForm
ConstraintEdit::validateForm public function Form validation handler. Overrides FormBase::validateForm
ConstraintEdit::__construct public function Overriding the constructor to load in the plugin manager and tempstore.
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
FormBase::$configFactory protected property The config factory. 1
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::config protected function Retrieves a configuration object.
FormBase::configFactory protected function Gets the config factory for this form. 1
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
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.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.