You are here

public function GoogleAnalyticsAdminSettingsForm::validateForm in Google Analytics 8.3

Same name and namespace in other branches
  1. 8.2 src/Form/GoogleAnalyticsAdminSettingsForm.php \Drupal\google_analytics\Form\GoogleAnalyticsAdminSettingsForm::validateForm()
  2. 4.x src/Form/GoogleAnalyticsAdminSettingsForm.php \Drupal\google_analytics\Form\GoogleAnalyticsAdminSettingsForm::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/GoogleAnalyticsAdminSettingsForm.php, line 595

Class

GoogleAnalyticsAdminSettingsForm
Configure Google_Analytics settings for this site.

Namespace

Drupal\google_analytics\Form

Code

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

  // Trim custom dimensions and metrics.
  foreach ($form_state
    ->getValue([
    'google_analytics_custom_dimension',
    'indexes',
  ]) as $dimension) {
    $form_state
      ->setValue([
      'google_analytics_custom_dimension',
      'indexes',
      $dimension['index'],
      'value',
    ], trim($dimension['value']));

    // Remove empty values from the array.
    if (!mb_strlen($form_state
      ->getValue([
      'google_analytics_custom_dimension',
      'indexes',
      $dimension['index'],
      'value',
    ]))) {
      $form_state
        ->unsetValue([
        'google_analytics_custom_dimension',
        'indexes',
        $dimension['index'],
      ]);
    }
  }
  $form_state
    ->setValue('google_analytics_custom_dimension', $form_state
    ->getValue([
    'google_analytics_custom_dimension',
    'indexes',
  ]));
  foreach ($form_state
    ->getValue([
    'google_analytics_custom_metric',
    'indexes',
  ]) as $metric) {
    $form_state
      ->setValue([
      'google_analytics_custom_metric',
      'indexes',
      $metric['index'],
      'value',
    ], trim($metric['value']));

    // Remove empty values from the array.
    if (!mb_strlen($form_state
      ->getValue([
      'google_analytics_custom_metric',
      'indexes',
      $metric['index'],
      'value',
    ]))) {
      $form_state
        ->unsetValue([
        'google_analytics_custom_metric',
        'indexes',
        $metric['index'],
      ]);
    }
  }
  $form_state
    ->setValue('google_analytics_custom_metric', $form_state
    ->getValue([
    'google_analytics_custom_metric',
    'indexes',
  ]));

  // Trim some text values.
  $form_state
    ->setValue('google_analytics_account', trim($form_state
    ->getValue('google_analytics_account')));
  $form_state
    ->setValue('google_analytics_visibility_request_path_pages', trim($form_state
    ->getValue('google_analytics_visibility_request_path_pages')));
  $form_state
    ->setValue('google_analytics_cross_domains', trim($form_state
    ->getValue('google_analytics_cross_domains')));
  $form_state
    ->setValue('google_analytics_codesnippet_before', trim($form_state
    ->getValue('google_analytics_codesnippet_before')));
  $form_state
    ->setValue('google_analytics_codesnippet_after', trim($form_state
    ->getValue('google_analytics_codesnippet_after')));
  $form_state
    ->setValue('google_analytics_visibility_user_role_roles', array_filter($form_state
    ->getValue('google_analytics_visibility_user_role_roles')));
  $form_state
    ->setValue('google_analytics_trackmessages', array_filter($form_state
    ->getValue('google_analytics_trackmessages')));

  // Replace all type of dashes (n-dash, m-dash, minus) with normal dashes.
  $form_state
    ->setValue('google_analytics_account', str_replace([
    '–',
    '—',
    '−',
  ], '-', $form_state
    ->getValue('google_analytics_account')));
  if (!preg_match('/^UA-\\d+-\\d+$/', $form_state
    ->getValue('google_analytics_account'))) {
    $form_state
      ->setErrorByName('google_analytics_account', $this
      ->t('A valid Google Analytics Web Property ID is case sensitive and formatted like UA-xxxxxxx-yy.'));
  }

  // If multiple top-level domains has been selected, a domain names list is
  // required.
  if ($form_state
    ->getValue('google_analytics_domain_mode') == 2 && $form_state
    ->isValueEmpty('google_analytics_cross_domains')) {
    $form_state
      ->setErrorByName('google_analytics_cross_domains', $this
      ->t('A list of top-level domains is required if <em>Multiple top-level domains</em> has been selected.'));
  }

  // Clear the domain list if cross domains are disabled.
  if ($form_state
    ->getValue('google_analytics_domain_mode') != 2) {
    $form_state
      ->setValue('google_analytics_cross_domains', '');
  }

  // Verify that every path is prefixed with a slash, but don't check PHP
  // code snippets and do not check for slashes if no paths configured.
  if ($form_state
    ->getValue('google_analytics_visibility_request_path_mode') != 2 && !empty($form_state
    ->getValue('google_analytics_visibility_request_path_pages'))) {
    $pages = preg_split('/(\\r\\n?|\\n)/', $form_state
      ->getValue('google_analytics_visibility_request_path_pages'));
    foreach ($pages as $page) {
      if (strpos($page, '/') !== 0 && $page !== '<front>') {
        $form_state
          ->setErrorByName('google_analytics_visibility_request_path_pages', $this
          ->t('Path "@page" not prefixed with slash.', [
          '@page' => $page,
        ]));

        // Drupal forms show one error only.
        break;
      }
    }
  }

  // Disallow empty list of download file extensions.
  if ($form_state
    ->getValue('google_analytics_trackfiles') && $form_state
    ->isValueEmpty('google_analytics_trackfiles_extensions')) {
    $form_state
      ->setErrorByName('google_analytics_trackfiles_extensions', $this
      ->t('List of download file extensions cannot empty.'));
  }

  // Clear obsolete local cache if cache has been disabled.
  if ($form_state
    ->isValueEmpty('google_analytics_cache') && $form['advanced']['google_analytics_cache']['#default_value']) {
    google_analytics_clear_js_cache();
  }

  // This is for the Newbie's who cannot read a text area description.
  if (stristr($form_state
    ->getValue('google_analytics_codesnippet_before'), 'google-analytics.com/analytics.js')) {
    $form_state
      ->setErrorByName('google_analytics_codesnippet_before', $this
      ->t('Do not add the tracker code provided by Google into the javascript code snippets! This module already builds the tracker code based on your Google Analytics account number and settings.'));
  }
  if (stristr($form_state
    ->getValue('google_analytics_codesnippet_after'), 'google-analytics.com/analytics.js')) {
    $form_state
      ->setErrorByName('google_analytics_codesnippet_after', $this
      ->t('Do not add the tracker code provided by Google into the javascript code snippets! This module already builds the tracker code based on your Google Analytics account number and settings.'));
  }
  if (preg_match('/(.*)<\\/?script(.*)>(.*)/i', $form_state
    ->getValue('google_analytics_codesnippet_before'))) {
    $form_state
      ->setErrorByName('google_analytics_codesnippet_before', $this
      ->t('Do not include the &lt;script&gt; tags in the javascript code snippets.'));
  }
  if (preg_match('/(.*)<\\/?script(.*)>(.*)/i', $form_state
    ->getValue('google_analytics_codesnippet_after'))) {
    $form_state
      ->setErrorByName('google_analytics_codesnippet_after', $this
      ->t('Do not include the &lt;script&gt; tags in the javascript code snippets.'));
  }
}