You are here

public function Domain301RedirectConfigForm::buildForm in Domain 301 Redirect 8

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides ConfigFormBase::buildForm

File

src/Form/Domain301RedirectConfigForm.php, line 79

Class

Domain301RedirectConfigForm
Provides a settings for domain_301_redirect module.

Namespace

Drupal\domain_301_redirect\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, Request $request = NULL) {
  $config = $this
    ->config('domain_301_redirect.settings');
  $disabled_by_check = $config
    ->get('disabled_by_check');
  $enabled = $config
    ->get('enabled');

  // Warn the user if the redirect was disabled by cron.
  if (!$enabled && $disabled_by_check) {
    $last_checked = $config
      ->get('last_checked');
    $this
      ->messenger()
      ->addWarning($this
      ->t('Redirects have been disabled by cron because the domain was not available at: %date.', [
      '%date' => $this->dateFormatter
        ->format($last_checked),
    ]));
  }
  $form['enabled'] = [
    '#type' => 'radios',
    '#title' => $this
      ->t('Status'),
    '#description' => $this
      ->t('Enable this setting to start 301 redirects to the domain below for all other domains.'),
    '#options' => [
      1 => $this
        ->t('Enabled'),
      0 => $this
        ->t('Disabled'),
    ],
    '#default_value' => $config
      ->get('enabled'),
  ];
  $form['domain'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Domain'),
    '#description' => $this
      ->t('This is the domain that all other domains pointing to this site will be 301 redirected to. This value should also include the scheme such as http or https but will redirect to http if not specified. Example: http://www.testsite.com'),
    '#default_value' => $config
      ->get('domain'),
  ];

  // Per path configuration settings to apply the redirect to specific paths.
  $form['domain_check'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Domain check'),
    '#open' => TRUE,
  ];
  $form['domain_check']['check_period'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Check period'),
    '#description' => $this
      ->t('This option selects the period between domain validation checks. If the domain no longer points to this site, domain redirection will be disabled.'),
    '#options' => [
      0 => $this
        ->t('Disabled'),
      3600 => $this
        ->t('1 hour'),
      7200 => $this
        ->t('2 hours'),
      10800 => $this
        ->t('3 hours'),
      21600 => $this
        ->t('6 hours'),
      43200 => $this
        ->t('12 hours'),
      86400 => $this
        ->t('1 day'),
    ],
    '#default_value' => $config
      ->get('check_period'),
  ];
  $form['domain_check']['domain_check_retries'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Retries'),
    '#description' => $this
      ->t('Number of times to check domain availability before disabling redirects.'),
    '#options' => [
      1 => 1,
      2 => 2,
      3 => 3,
    ],
    '#default_value' => $config
      ->get('domain_check_retries'),
  ];
  $form['domain_check']['domain_check_reenable'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Re-enable domain redirection'),
    '#description' => $this
      ->t('Turn domain redirection on when the domain becomes available.'),
    '#default_value' => $config
      ->get('domain_check_reenable'),
  ];

  // Per path configuration settings to apply the redirect to specific paths.
  $form['applicability'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Pages'),
    '#open' => TRUE,
  ];
  $form['applicability']['pages'] = [
    '#type' => 'textarea',
    '#default_value' => $config
      ->get('pages'),
    '#description' => $this
      ->t("Specify pages by using their paths. Enter one path per line. The '*' character is a wildcard. Example paths are %blog for the blog page and %blog-wildcard for every personal blog. %front is the front page.", [
      '%blog' => '/blog',
      '%blog-wildcard' => '/blog/*',
      '%front' => '<front>',
    ]),
  ];
  $form['applicability']['applicability'] = [
    '#type' => 'radios',
    '#options' => [
      Domain301Redirect::EXCLUDE_METHOD => $this
        ->t('Do not redirect for the listed pages'),
      Domain301Redirect::INCLUDE_METHOD => $this
        ->t('Only redirect for the listed pages'),
    ],
    '#default_value' => $config
      ->get('applicability'),
  ];
  return parent::buildForm($form, $form_state);
}