You are here

class GlobalredirectSettingsForm in Global Redirect 8

Defines a form to configure module settings.

Hierarchy

Expanded class hierarchy of GlobalredirectSettingsForm

1 string reference to 'GlobalredirectSettingsForm'
globalredirect.routing.yml in ./globalredirect.routing.yml
globalredirect.routing.yml

File

src/Form/GlobalredirectSettingsForm.php, line 17
This is the GlobalRedirect admin include which provides an interface to global redirect to change some of the default settings Contains \Drupal\globalredirect\Form\GlobalredirectSettingsForm.

Namespace

Drupal\globalredirect\Form
View source
class GlobalredirectSettingsForm extends ConfigFormBase {

  /**
   * {@inheritdoc}
   */
  public function getFormID() {
    return 'globalredirect_settings';
  }

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

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

    // Get all settings
    $config = $this
      ->config('globalredirect.settings');
    $settings = $config
      ->get();
    $form['settings'] = array(
      '#tree' => TRUE,
    );
    $form['settings']['deslash'] = array(
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Deslash'),
      '#description' => $this
        ->t('If enabled, this option will remove the trailing slash from requests. This stops requests such as <code>example.com/node/1/</code> failing to match the corresponding alias and can cause duplicate content. On the other hand, if you require certain requests to have a trailing slash, this feature can cause problems so may need to be disabled.'),
      '#default_value' => $settings['deslash'],
    );
    $form['settings']['nonclean_to_clean'] = array(
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Non-clean to Clean'),
      '#description' => $this
        ->t('If enabled, this option will redirect from non-clean to clean URL (if Clean URL\'s are enabled). This will stop, for example, node 1  existing on both <code>example.com/node/1</code> AND <code>example.com/index.php/node/1</code>.'),
      '#default_value' => $settings['nonclean_to_clean'],
    );
    $form['settings']['access_check'] = array(
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Check access to the redirected page'),
      '#description' => $this
        ->t('This helps to stop redirection on protected pages and avoids giving away <em>secret</em> URL\'s. <strong>By default this feature is disabled to avoid any unexpected behavior</strong>'),
      '#default_value' => $settings['access_check'],
    );
    $form['settings']['normalize_aliases'] = array(
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Normalize aliases'),
      '#description' => $this
        ->t('Will check if for the given path an alias exists or if the used alias is in correct case and will redirect to the appropriate alias form.'),
      '#default_value' => $settings['normalize_aliases'],
    );
    $form['settings']['canonical'] = array(
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Add Canonical Link'),
      '#description' => $this
        ->t('If enabled, will add a <a href="!canonical">canonical link</a> to each page.', array(
        '!canonical' => 'http://googlewebmastercentral.blogspot.com/2009/02/specify-your-canonical.html',
      )),
      '#default_value' => $settings['canonical'],
    );
    $form['settings']['content_location_header'] = array(
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Set Content Location Header'),
      '#description' => $this
        ->t('If enabled, will add a <a href="!canonical">Content-Location</a> header.', array(
        '!canonical' => 'http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.14',
      )),
      '#default_value' => $settings['content_location_header'],
    );
    $form['settings']['term_path_handler'] = array(
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Taxonomy Term Path Handler'),
      '#description' => $this
        ->t('If enabled, any request to a taxonomy/term/[tid] page will check that the correct path is being used for the term\'s vocabulary.'),
      '#default_value' => $settings['term_path_handler'],
    );
    $form['settings']['frontpage_redirect'] = array(
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Frontpage Redirect Handler'),
      '#description' => $this
        ->t('If enabled, any request to the frontpage path will redirect to the site root.<br />
                           Whatever you set as the path of the front page on the !link settings page will redirect to the site root (e.g. "node" or "node/1" and also its alias (e.g. in case you have set "node/1" as your home page but that page also has an alias "home")).', array(
        '!link' => $this
          ->l($this
          ->t('Site Information'), Url::fromRoute('system.site_information_settings')),
      )),
      '#default_value' => $settings['frontpage_redirect'],
    );
    $form['settings']['ignore_admin_path'] = array(
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Ignore Admin Path'),
      '#description' => $this
        ->t('If enabled, any request to the admin section of the site will be ignored by Global Redirect.<br />
                           This is useful if you are experiencing problems with Global Redirect and want to protect the admin section of your website. NOTE: This may not be desirable if you are using path aliases for certain admin URLs.'),
      '#default_value' => $settings['ignore_admin_path'],
    );
    $form['buttons']['reset'] = array(
      '#type' => 'submit',
      '#submit' => array(
        '::submitResetDefaults',
      ),
      '#value' => t('Reset to defaults'),
    );
    return parent::buildForm($form, $form_state);
  }

  /**
   * Compares the submitted settings to the defaults and unsets any that are equal. This was we only store overrides.
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {

    // Get config factory
    $config = $this
      ->config('globalredirect.settings');
    $form_values = $form_state
      ->getValue([
      'settings',
    ]);
    $config
      ->set('deslash', $form_values['deslash'])
      ->set('nonclean_to_clean', $form_values['nonclean_to_clean'])
      ->set('access_check', $form_values['access_check'])
      ->set('normalize_aliases', $form_values['normalize_aliases'])
      ->set('canonical', $form_values['canonical'])
      ->set('content_location_header', $form_values['content_location_header'])
      ->set('term_path_handler', $form_values['term_path_handler'])
      ->set('frontpage_redirect', $form_values['frontpage_redirect'])
      ->set('ignore_admin_path', $form_values['ignore_admin_path'])
      ->save();
    parent::submitForm($form, $form_state);
  }

  /**
   * Clears the caches.
   */
  public function submitResetDefaults(array &$form, FormStateInterface $form_state) {
    $config = $this
      ->config('globalredirect.settings');

    // Get config factory
    $settingsDefault = $this
      ->getDefaultSettings();
    $config
      ->set('deslash', $settingsDefault['deslash'])
      ->set('nonclean_to_clean', $settingsDefault['nonclean_to_clean'])
      ->set('access_check', $settingsDefault['access_check'])
      ->set('normalize_aliases', $settingsDefault['normalize_aliases'])
      ->set('canonical', $settingsDefault['canonical'])
      ->set('content_location_header', $settingsDefault['content_location_header'])
      ->set('term_path_handler', $settingsDefault['term_path_handler'])
      ->set('frontpage_redirect', $settingsDefault['frontpage_redirect'])
      ->set('ignore_admin_path', $settingsDefault['ignore_admin_path'])
      ->save();
    parent::submitForm($form, $form_state);
  }

  /**
   * Returns an associative array of default settings
   * @return array
   */
  public function getDefaultSettings() {
    $defaults = array(
      'deslash' => 1,
      'nonclean_to_clean' => 1,
      'access_check' => 0,
      'normalize_aliases' => 1,
      'canonical' => 0,
      'content_location_header' => 0,
      'term_path_handler' => 1,
      'frontpage_redirect' => 1,
      'ignore_admin_path' => 1,
    );
    return $defaults;
  }

}

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
FormInterface::getFormId public function Returns a unique string identifying the form. 236
GlobalredirectSettingsForm::buildForm public function Form constructor. Overrides ConfigFormBase::buildForm
GlobalredirectSettingsForm::getDefaultSettings public function Returns an associative array of default settings
GlobalredirectSettingsForm::getEditableConfigNames protected function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
GlobalredirectSettingsForm::getFormID public function
GlobalredirectSettingsForm::submitForm public function Compares the submitted settings to the defaults and unsets any that are equal. This was we only store overrides. Overrides ConfigFormBase::submitForm
GlobalredirectSettingsForm::submitResetDefaults public function Clears the caches.
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.