You are here

public function HotjarAdminSettingsForm::validateForm in Hotjar 8.2

Same name and namespace in other branches
  1. 8 src/Form/HotjarAdminSettingsForm.php \Drupal\hotjar\Form\HotjarAdminSettingsForm::validateForm()

Form validation handler.

Parameters

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

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

Overrides FormBase::validateForm

File

src/Form/HotjarAdminSettingsForm.php, line 238

Class

HotjarAdminSettingsForm
Configure Hotjar settings for this site.

Namespace

Drupal\hotjar\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) {
  parent::validateForm($form, $form_state);

  // Trim some text values.
  $form_state
    ->setValue('hotjar_account', trim($form_state
    ->getValue('hotjar_account')));
  $form_state
    ->setValue('hotjar_snippet_version', trim($form_state
    ->getValue('hotjar_snippet_version')));
  $form_state
    ->setValue('hotjar_snippet_path', trim($form_state
    ->getValue('hotjar_snippet_path')));
  $form_state
    ->setValue('hotjar_attachment_mode', trim($form_state
    ->getValue('hotjar_attachment_mode')));
  $pages = _hotjar_clean_pages_value($form_state
    ->getValue('hotjar_pages'));
  $form_state
    ->setValue('hotjar_pages', $pages);
  $form_state
    ->setValue('hotjar_roles', array_filter($form_state
    ->getValue('hotjar_roles')));

  // Verify that every path is prefixed with a slash.
  if ($form_state
    ->getValue('hotjar_visibility_pages') != 2) {
    $pages = preg_split('/(\\r\\n?|\\n)/', $form_state
      ->getValue('hotjar_pages'));
    foreach ($pages as $page) {
      if (strpos($page, '/') !== 0 && $page !== '<front>') {
        $form_state
          ->setErrorByName('hotjar_pages', $this
          ->t('Path "@page" not prefixed with slash.', [
          '@page' => $page,
        ]));

        // Drupal forms show one error only.
        break;
      }
    }
  }
  $scheme = StreamWrapperManager::getScheme($form_state
    ->getValue('hotjar_snippet_path'));
  if (!$this->streamWrapperManager
    ->isValidScheme($scheme)) {
    $form_state
      ->setErrorByName('hotjar_snippet_path', $this
      ->t('Snippet path must starts with <code>public://</code>, <code>private://</code> or any other valid file stream.'));
  }
}