You are here

class PasswordPolicyConstraintForm in Password Policy 8.3

Form that lists out the constraints for the policy.

Hierarchy

Expanded class hierarchy of PasswordPolicyConstraintForm

File

src/Form/PasswordPolicyConstraintForm.php, line 18

Namespace

Drupal\password_policy\Form
View source
class PasswordPolicyConstraintForm extends FormBase {

  /**
   * The form builder.
   *
   * @var \Drupal\Core\Form\FormBuilderInterface
   */
  protected $formBuilder;

  /**
   * Plugin manager for constraints.
   *
   * @var \Drupal\Component\Plugin\PluginManagerInterface
   */
  protected $manager;

  /**
   * Machine name for the form step.
   *
   * @var string
   */
  protected $machineName;

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('plugin.manager.password_policy.password_constraint'), $container
      ->get('form_builder'));
  }

  /**
   * Overridden constructor to load the plugin.
   *
   * @param \Drupal\Component\Plugin\PluginManagerInterface $manager
   *   Plugin manager for constraints.
   * @param \Drupal\Core\Form\FormBuilderInterface $formBuilder
   *   The form builder.
   */
  public function __construct(PluginManagerInterface $manager, FormBuilderInterface $formBuilder) {
    $this->manager = $manager;
    $this->formBuilder = $formBuilder;
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'password_policy_constraint_form';
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $cached_values = $form_state
      ->getTemporaryValue('wizard');
    $this->machineName = $cached_values['id'];
    $form['#attached']['library'][] = 'core/drupal.dialog.ajax';
    $constraints = [];
    foreach ($this->manager
      ->getDefinitions() as $plugin_id => $definition) {
      $constraints[$plugin_id] = (string) $definition['title'];
    }
    $form['add_constraint_title'] = [
      '#markup' => '<h2>' . $this
        ->t('Add Constraint') . '</h2>',
    ];
    $form['constraint'] = [
      '#type' => 'select',
      '#options' => $constraints,
      '#prefix' => '<table style="width=100%"><tr><td>',
      '#suffix' => '</td>',
    ];
    $form['add'] = [
      '#type' => 'submit',
      '#name' => 'add',
      '#value' => $this
        ->t('Configure Constraint Settings'),
      '#ajax' => [
        'callback' => [
          $this,
          'add',
        ],
        'event' => 'click',
      ],
      '#prefix' => '<td>',
      '#suffix' => '</td></tr></table>',
    ];
    $form['constraint_list'] = [
      '#markup' => '<h2>' . $this
        ->t('Policy Constraints') . '</h2>',
    ];
    $form['items'] = [
      '#type' => 'markup',
      '#prefix' => '<div id="configured-constraints">',
      '#suffix' => '</div>',
      '#theme' => 'table',
      '#header' => [
        'plugin_id' => $this
          ->t('Plugin Id'),
        'summary' => $this
          ->t('Summary'),
        'operations' => $this
          ->t('Operations'),
      ],
      '#rows' => $this
        ->renderRows($cached_values),
      '#empty' => $this
        ->t('No constraints have been configured.'),
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {

    // This form has no explicit submit action since it just shows constraints.
  }

  /**
   * Ajax callback that manages adding a constraint.
   *
   * @param array $form
   *   Form definition of parent form.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   State of the form.
   *
   * @return \Drupal\Core\Ajax\AjaxResponse
   *   Returns the valid Ajax response from a modal window.
   */
  public function add(array &$form, FormStateInterface $form_state) {
    $constraint = $form_state
      ->getValue('constraint');
    $content = $this->formBuilder
      ->getForm(ConstraintEdit::class, $constraint, $this->machineName);
    $content['#attached']['library'][] = 'core/drupal.dialog.ajax';
    $url = Url::fromRoute('entity.password_policy.constraint.add', [
      'machine_name' => $this->machineName,
      'constraint_id' => $constraint,
    ], [
      'query' => [
        FormBuilderInterface::AJAX_FORM_REQUEST => TRUE,
      ],
    ]);
    $content['submit']['#attached']['drupalSettings']['ajax'][$content['submit']['#id']]['url'] = $url
      ->toString();
    $response = new AjaxResponse();
    $response
      ->addCommand(new OpenModalDialogCommand($this
      ->t('Configure Required Context'), $content, [
      'width' => '700',
    ]));
    return $response;
  }

  /**
   * Helper function to render the constraint rows for the policy.
   *
   * @param array $cached_values
   *   Loading the cached metadata for the form wizard.
   *
   * @return array
   *   Constraint rows rendered for the policy.
   */
  public function renderRows(array $cached_values) {

    /** @var \Drupal\password_policy\Entity\PasswordPolicy $policy */
    $policy = $cached_values['password_policy'];
    $configured_conditions = [];
    foreach ($policy
      ->getConstraints() as $row => $constraint) {

      /** @var \Drupal\password_policy\PasswordConstraintInterface $instance */
      $instance = $this->manager
        ->createInstance($constraint['id'], $constraint);
      $operations = $this
        ->getOperations('entity.password_policy.constraint', [
        'machine_name' => $cached_values['id'],
        'constraint_id' => $row,
      ]);
      $build = [
        '#type' => 'operations',
        '#links' => $operations,
      ];
      $configured_conditions[] = [
        'plugin_id' => $instance
          ->getPluginId(),
        'summary' => $instance
          ->getSummary(),
        'operations' => [
          'data' => $build,
        ],
      ];
    }
    return $configured_conditions;
  }

  /**
   * Helper function to load edit operations for a constraint.
   *
   * @param string $route_name_base
   *   String representing the base of the route name for the constraints.
   * @param array $route_parameters
   *   Passing route parameter context to the helper function.
   *
   * @return array
   *   Set of operations associated with a constraint.
   */
  protected function getOperations($route_name_base, array $route_parameters = []) {
    $edit_url = new Url($route_name_base . '.edit', $route_parameters);
    $route_parameters['id'] = $route_parameters['constraint_id'];
    unset($route_parameters['constraint_id']);
    $delete_url = new Url($route_name_base . '.delete', $route_parameters);
    $operations = [];
    $operations['edit'] = [
      'title' => $this
        ->t('Edit'),
      'url' => $edit_url,
      'weight' => 10,
      'attributes' => [
        'class' => [
          'use-ajax',
        ],
        'data-dialog-type' => 'modal',
        'data-dialog-options' => Json::encode([
          'width' => 700,
        ]),
      ],
    ];
    $operations['delete'] = [
      'title' => $this
        ->t('Delete'),
      'url' => $delete_url,
      'weight' => 100,
      'attributes' => [
        'class' => [
          'use-ajax',
        ],
        'data-dialog-type' => 'modal',
        'data-dialog-options' => Json::encode([
          'width' => 700,
        ]),
      ],
    ];
    return $operations;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
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.
FormBase::validateForm public function Form validation handler. Overrides FormInterface::validateForm 62
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.
PasswordPolicyConstraintForm::$formBuilder protected property The form builder.
PasswordPolicyConstraintForm::$machineName protected property Machine name for the form step.
PasswordPolicyConstraintForm::$manager protected property Plugin manager for constraints.
PasswordPolicyConstraintForm::add public function Ajax callback that manages adding a constraint.
PasswordPolicyConstraintForm::buildForm public function Form constructor. Overrides FormInterface::buildForm
PasswordPolicyConstraintForm::create public static function Instantiates a new instance of this class. Overrides FormBase::create
PasswordPolicyConstraintForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
PasswordPolicyConstraintForm::getOperations protected function Helper function to load edit operations for a constraint.
PasswordPolicyConstraintForm::renderRows public function Helper function to render the constraint rows for the policy.
PasswordPolicyConstraintForm::submitForm public function Form submission handler. Overrides FormInterface::submitForm
PasswordPolicyConstraintForm::__construct public function Overridden constructor to load the plugin.
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.