You are here

public static function FormManglerService::validateFormRedirect in Rabbit Hole 8

Same name and namespace in other branches
  1. 2.x src/FormManglerService.php \Drupal\rabbit_hole\FormManglerService::validateFormRedirect()

Validate user input before saving it.

Parameters

array $form: The form.

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

File

src/FormManglerService.php, line 345

Class

FormManglerService
Provides necessary form alterations.

Namespace

Drupal\rabbit_hole

Code

public static function validateFormRedirect(array $form, FormStateInterface &$form_state) {
  $rh_action = $form_state
    ->getValue('rh_action');

  // Validate URL of page redirect.
  if ($rh_action == 'page_redirect') {
    $redirect = $form_state
      ->getValue('rh_redirect');
    if (!UrlHelper::isExternal($redirect) && $redirect !== '<front>') {
      $scheme = parse_url($redirect, PHP_URL_SCHEME);

      // Check if internal URL matches requirements of
      // \Drupal\Core\Url::fromUserInput.
      $accepted_internal_characters = [
        '/',
        '?',
        '#',
        '[',
      ];
      if ($scheme === NULL && !in_array(substr($redirect, 0, 1), $accepted_internal_characters)) {
        $form_state
          ->setErrorByName('rh_redirect', t("Internal path '@string' must begin with a '/', '?', '#', or be a token.", [
          '@string' => $redirect,
        ]));
      }
    }
  }
}