You are here

class ProtectedPagesSettingForm in Protected Pages 8

Provides protected pages settings configuration form.

Hierarchy

Expanded class hierarchy of ProtectedPagesSettingForm

1 string reference to 'ProtectedPagesSettingForm'
protected_pages.routing.yml in ./protected_pages.routing.yml
protected_pages.routing.yml

File

src/Form/ProtectedPagesSettingForm.php, line 17

Namespace

Drupal\protected_pages\Form
View source
class ProtectedPagesSettingForm extends ConfigFormBase {

  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames() {
    return [
      'protected_pages.settings',
    ];
  }

  /**
   * The mail manager.
   *
   * @var \Drupal\Core\Mail\MailManagerInterface
   */
  protected $mailManager;

  /**
   * The email validator.
   *
   * @var \Egulias\EmailValidator\EmailValidator
   */
  protected $emailValidator;

  /**
   * The renderer service.
   *
   * @var \Drupal\Core\Render\RendererInterface
   */
  protected $renderer;

  /**
   * Provides the password hashing service object.
   *
   * @var \Drupal\Core\Password\PasswordInterface
   */
  protected $password;

  /**
   * Constructs a new ProtectedPagesSettingForm.
   *
   * @param \Drupal\Core\Mail\MailManagerInterface $mail_manager
   *   The mail manager.
   * @param \Egulias\EmailValidator\EmailValidator $email_validator
   *   The email validator.
   * @param \Drupal\Core\Render\RendererInterface $renderer
   *   The renderer service.
   * @param \Drupal\Core\Password\PasswordInterface $password
   *   The password hashing service.
   */
  public function __construct(MailManagerInterface $mail_manager, EmailValidator $email_validator, RendererInterface $renderer, PasswordInterface $password) {
    $this->mailManager = $mail_manager;
    $this->emailValidator = $email_validator;
    $this->renderer = $renderer;
    $this->password = $password;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('plugin.manager.mail'), $container
      ->get('email.validator'), $container
      ->get('renderer'), $container
      ->get('password'));
  }

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

  /**
   * Form element validation handler to validate session expire time value..
   */
  public function protectedPagesValidateIntegerPositive($element, FormStateInterface $form_state) {
    $value = $element['#value'];
    if ($value !== '' && (!is_numeric($value) || intval($value) != $value || $value < 0)) {
      $form_state
        ->setError($element, $this
        ->t('%name must be a positive integer.', [
        '%name' => $element['#title'],
      ]));
    }
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $form = parent::buildForm($form, $form_state);
    $config = $this
      ->config('protected_pages.settings');
    $form['protected_pages_password_fieldset'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Protected Pages password settings'),
      '#description' => $this
        ->t('Configure password related settings.'),
      '#open' => TRUE,
    ];
    $global_password_help_text = [];
    $global_password_help_text[] = $this
      ->t('Allow per page password = Only per page password will be accepted. Global password will not be accepted.');
    $global_password_help_text[] = $this
      ->t('Allow per page password or Global password = Per page  password and global password both will be accepted.');
    $global_password_help_text[] = $this
      ->t('Allow Only Global = Only global password will be accepted for each protected page.');
    $global_password_help_text_list = [
      '#theme' => 'item_list',
      '#items' => $global_password_help_text,
      '#title' => $this
        ->t('Please select the appropriate option for protected pages handling.'),
    ];
    $form['protected_pages_password_fieldset']['protected_pages_user_global_password'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Global password setting'),
      '#default_value' => $config
        ->get('password.per_page_or_global'),
      '#options' => [
        'per_page_password' => $this
          ->t('Allow per page password'),
        'per_page_or_global' => $this
          ->t('Allow per page password or Global password'),
        'only_global' => $this
          ->t('Allow only global'),
      ],
      '#description' => $this->renderer
        ->render($global_password_help_text_list),
    ];
    $form['protected_pages_password_fieldset']['protected_pages_global_password_field'] = [
      '#type' => 'password_confirm',
      '#title' => $this
        ->t('Global password'),
      '#description' => $this
        ->t('The default password for all protected pages. This
                                password is necessary if you select the previous checkbox "Allow per page
                                password or Global password" or "Allow Only Global" options above.'),
    ];
    $form['protected_pages_password_fieldset']['protected_pages_session_expire_time'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Session expire time'),
      '#description' => $this
        ->t('When user enters password a session is created.
      The node will be accessible until session expire. Once session expires,
      user will need to enter password again. The default session expire time
      is 0 (unlimited).'),
      '#default_value' => $config
        ->get('password.protected_pages_session_expire_time'),
      '#required' => TRUE,
      '#size' => 10,
      '#element_validate' => [
        [
          $this,
          'protectedPagesValidateIntegerPositive',
        ],
      ],
      '#field_suffix' => $this
        ->t('minutes'),
    ];
    $form['protected_pages_email_fieldset'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Protected Pages email settings'),
      '#description' => $this
        ->t('The following settings allows admin to send emails to multiple users about protected pages details to access protected pages.'),
      '#open' => TRUE,
    ];
    $form['protected_pages_email_fieldset']['protected_pages_email_subject'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Email subject'),
      '#default_value' => $config
        ->get('email.subject'),
      '#description' => $this
        ->t('Enter the subject of the email.'),
    ];
    $form['protected_pages_email_fieldset']['protected_pages_email_body'] = [
      '#type' => 'textarea',
      '#title' => $this
        ->t('Email content'),
      '#rows' => 15,
      '#default_value' => $config
        ->get('email.body'),
      '#description' => $this
        ->t('Enter the body of the email. Only [protected-page-url] and [site-name] tokens are available.
      Since password is encrypted, therefore we can not provide it by token.'),
    ];
    $form['protected_pages_other_fieldset'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Protected Pages other settings'),
      '#description' => $this
        ->t('Configure other settings.'),
      '#open' => TRUE,
    ];
    $form['protected_pages_other_fieldset']['protected_pages_title'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Password page title'),
      '#default_value' => $config
        ->get('others.protected_pages_title'),
      '#description' => $this
        ->t('Enter the title of the protected page.'),
    ];
    $form['protected_pages_other_fieldset']['protected_pages_description'] = [
      '#type' => 'textarea',
      '#title' => $this
        ->t('Password page description (inside the field set)'),
      '#default_value' => $config
        ->get('others.protected_pages_description'),
      '#description' => $this
        ->t('Enter specific description for the protected page. This description is displayed inside the fieldset. HTML is accepted.'),
    ];
    $form['protected_pages_other_fieldset']['protected_pages_password_fieldset_legend'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Password fieldset legend'),
      '#default_value' => $config
        ->get('others.protected_pages_password_fieldset_legend'),
      '#description' => $this
        ->t('Enter the text for the password fieldset legend.'),
    ];
    $form['protected_pages_other_fieldset']['protected_pages_password_label'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Password field label'),
      '#default_value' => $config
        ->get('others.protected_pages_password_label'),
      '#description' => $this
        ->t('Enter the text for the password field label.'),
    ];
    $form['protected_pages_other_fieldset']['protected_pages_submit_button_text'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Submit button text'),
      '#default_value' => $config
        ->get('others.protected_pages_submit_button_text'),
      '#description' => $this
        ->t('Enter the text for the submit button of enter password form.'),
    ];
    $form['protected_pages_other_fieldset']['protected_pages_incorrect_password_msg'] = [
      '#type' => 'textarea',
      '#title' => $this
        ->t('Incorrect password error text'),
      '#default_value' => $config
        ->get('others.protected_pages_incorrect_password_msg'),
      '#description' => $this
        ->t('This error text will appear if someone enters wrong password in "Enter password screen".'),
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $config = $this
      ->config('protected_pages.settings');
    $config
      ->set('password.per_page_or_global', $form_state
      ->getValue('protected_pages_user_global_password'));
    $config
      ->set('password.protected_pages_session_expire_time', $form_state
      ->getValue('protected_pages_session_expire_time'));
    $config
      ->set('email.subject', $form_state
      ->getValue('protected_pages_email_subject'));
    $config
      ->set('email.body', $form_state
      ->getValue('protected_pages_email_body'));
    $config
      ->set('others.protected_pages_title', $form_state
      ->getValue('protected_pages_title'));
    $config
      ->set('others.protected_pages_description', $form_state
      ->getValue('protected_pages_description'));
    $config
      ->set('others.protected_pages_password_fieldset_legend', $form_state
      ->getValue('protected_pages_password_fieldset_legend'));
    $config
      ->set('others.protected_pages_password_label', $form_state
      ->getValue('protected_pages_password_label'));
    $config
      ->set('others.protected_pages_submit_button_text', $form_state
      ->getValue('protected_pages_submit_button_text'));
    $config
      ->set('others.protected_pages_incorrect_password_msg', $form_state
      ->getValue('protected_pages_incorrect_password_msg'));
    $password = $form_state
      ->getValue('protected_pages_global_password_field');
    if ($password) {
      $password_hash = $this->password
        ->hash(Html::escape($password));
      $config
        ->set('password.protected_pages_global_password', $password_hash);
    }
    $config
      ->save();
    drupal_flush_all_caches();
    return parent::submitForm($form, $form_state);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigFormBaseTrait::config protected function Retrieves a configuration object.
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::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.
ProtectedPagesSettingForm::$emailValidator protected property The email validator.
ProtectedPagesSettingForm::$mailManager protected property The mail manager.
ProtectedPagesSettingForm::$password protected property Provides the password hashing service object.
ProtectedPagesSettingForm::$renderer protected property The renderer service.
ProtectedPagesSettingForm::buildForm public function Form constructor. Overrides ConfigFormBase::buildForm
ProtectedPagesSettingForm::create public static function Instantiates a new instance of this class. Overrides ConfigFormBase::create
ProtectedPagesSettingForm::getEditableConfigNames protected function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
ProtectedPagesSettingForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
ProtectedPagesSettingForm::protectedPagesValidateIntegerPositive public function Form element validation handler to validate session expire time value..
ProtectedPagesSettingForm::submitForm public function Form submission handler. Overrides ConfigFormBase::submitForm
ProtectedPagesSettingForm::__construct public function Constructs a new ProtectedPagesSettingForm. Overrides ConfigFormBase::__construct
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.