You are here

class SecurepagesSettingsForm in Secure Pages 8

Defines a form that configures forms module settings.

Hierarchy

Expanded class hierarchy of SecurepagesSettingsForm

1 string reference to 'SecurepagesSettingsForm'
securepages.routing.yml in ./securepages.routing.yml
securepages.routing.yml

File

src/Form/SecurepagesSettingsForm.php, line 18
Contains \Drupal\securepages\Form\SecurepagesSettingsForm.

Namespace

Drupal\securepages\Form
View source
class SecurepagesSettingsForm extends ConfigFormBase {

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

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

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state, Request $request = NULL) {
    $config = $this
      ->config('securepages.settings');
    $secure = 0;
    if ($config
      ->get('secure')) {
      $secure = 1;
    }
    $form['enable'] = array(
      '#type' => 'checkbox',
      '#title' => t('Enable Secure Pages'),
      '#default_value' => $config
        ->get('enable'),
      '#disabled' => !Securepages::isHTTPSSupported(),
      '#description' => $this
        ->t('To start using secure pages this setting must be enabled. This setting will only be possible to change when the web server has been configured for HTTPS. You may need to set the secure base URL below in case of a custom port. <a href=":url">You can manually visit the site in HTTPS too</a>.', array(
        ':url' => Securepages::getUrl('<front>')
          ->toString(),
      )),
    );
    $form['switch'] = array(
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Switch back to HTTP pages when there are no matches'),
      '#default_value' => $config
        ->get('switch'),
    );
    $form['basepath'] = array(
      '#type' => 'textfield',
      '#title' => $this
        ->t('Non-secure base URL'),
      '#default_value' => $config
        ->get('basepath'),
      '#size' => 100,
    );
    $form['basepath_ssl'] = array(
      '#type' => 'textfield',
      '#title' => $this
        ->t('Secure base URL'),
      '#default_value' => $config
        ->get('basepath_ssl'),
      '#size' => 100,
    );
    $active_options = [
      0 => $this
        ->t('Pages not matching the patterns should be secure'),
      1 => $this
        ->t('Pages matching the patterns should be secure'),
    ];
    $form['secure'] = array(
      '#type' => 'radios',
      '#title' => $this
        ->t('Which pages will be secure'),
      '#default_value' => $secure,
      '#options' => $active_options,
    );
    $form['pages'] = array(
      '#title' => $this
        ->t('Pages'),
      '#type' => 'textarea',
      '#default_value' => implode("\n", $config
        ->get('pages')),
      '#cols' => 40,
      '#rows' => 5,
      '#description' => $this
        ->t("Enter one page per line as Drupal paths. The '*' character is a wildcard. Example paths are '<em>blog</em>' for the main blog page and '<em>blog/*</em>' for every personal blog. '<em>&lt;front&gt;</em>' is the front page."),
    );
    $form['ignore'] = array(
      '#type' => 'textarea',
      '#title' => $this
        ->t('Ignore pages'),
      '#default_value' => implode("\n", $config
        ->get('ignore')),
      '#cols' => 40,
      '#rows' => 5,
      '#description' => $this
        ->t("The pages listed here will be ignored and be either returned in HTTP or HTTPS. Enter one page per line as Drupal paths. The '*' character is a wildcard. Example paths are '<em>blog</em>' for the blog page and '<em>blog/*</em>' for every personal blog. '<em>&lt;front&gt;</em>' is the front page."),
    );
    $form['roles'] = array(
      '#type' => 'checkboxes',
      '#title' => $this
        ->t('User roles'),
      '#description' => $this
        ->t('Users with the chosen role(s) are always redirected to HTTPS, regardless of path rules.'),
      '#options' => user_role_names(),
      '#default_value' => $config
        ->get('roles'),
    );
    $form['forms'] = array(
      '#type' => 'textarea',
      '#title' => $this
        ->t('Secure forms'),
      '#default_value' => implode("\n", $config
        ->get('forms')),
      '#cols' => 40,
      '#rows' => 5,
      '#description' => $this
        ->t('List of form ids which will have the HTTPS flag set to TRUE.'),
    );
    $form['debug'] = array(
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Enable debugging'),
      '#default_value' => $config
        ->get('debug'),
      '#description' => $this
        ->t('Turn on debugging to allow easier testing of settings.'),
    );
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $this
      ->config('securepages.settings')
      ->set('enable', $form_state
      ->getValue('enable'))
      ->set('switch', $form_state
      ->getValue('switch'))
      ->set('basepath', $form_state
      ->getValue('basepath'))
      ->set('basepath_ssl', $form_state
      ->getValue('basepath_ssl'))
      ->set('secure', $form_state
      ->getValue('secure'))
      ->set('pages', $this::explodeValues($form_state
      ->getValue('pages')))
      ->set('ignore', $this::explodeValues($form_state
      ->getValue('ignore')))
      ->set('roles', array_filter($form_state
      ->getValue('roles')))
      ->set('forms', $this::explodeValues($form_state
      ->getValue('forms')))
      ->set('debug', $form_state
      ->getValue('debug'))
      ->save();
    parent::submitForm($form, $form_state);
  }

  /**
   * Explode $values and returned a clean array with options.
   *
   * @param string $values
   *   Values as entered in the form, separated by newlines.
   *
   * @return array
   *   Array formatted trimmed values with empty items removed.
   */
  private static function explodeValues($values) {

    // Convert string to an array, trim whitespace on each item, remove
    // empty items and reindex array for clean export.
    return array_values(array_filter(array_map('trim', explode("\n", $values))));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigFormBase::create public static function Instantiates a new instance of this class. Overrides FormBase::create 13
ConfigFormBase::__construct public function Constructs a \Drupal\system\ConfigFormBase object. 11
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.
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.
SecurepagesSettingsForm::buildForm public function Form constructor. Overrides ConfigFormBase::buildForm
SecurepagesSettingsForm::explodeValues private static function Explode $values and returned a clean array with options.
SecurepagesSettingsForm::getEditableConfigNames protected function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
SecurepagesSettingsForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
SecurepagesSettingsForm::submitForm public function Form submission handler. Overrides ConfigFormBase::submitForm
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.