You are here

public function ContainerTrait::validateFormValues in GoogleTagManager 8

2 calls to ContainerTrait::validateFormValues()
ContainerForm::validateForm in src/Form/ContainerForm.php
Form validation handler.
SettingsForm::validateForm in src/Form/SettingsForm.php
Form validation handler.

File

src/Form/ContainerTrait.php, line 251

Class

ContainerTrait
Defines shared routines for the container and settings forms.

Namespace

Drupal\google_tag\Form

Code

public function validateFormValues(array &$form, FormStateInterface $form_state) {

  // Specific to the container form.
  $container_id = $form_state
    ->getValue('container_id');
  if (!is_null($container_id)) {
    $container_id = trim($container_id);
    $container_id = str_replace([
      '–',
      '—',
      '−',
    ], '-', $container_id);
    $form_state
      ->setValue('container_id', $container_id);
    if (!preg_match('/^GTM-\\w{4,}$/', $container_id)) {

      // @todo Is there a more specific regular expression that applies?
      // @todo Is there a way to validate the container ID?
      // It may be valid but not the correct one for the website.
      $form_state
        ->setError($form['general']['container_id'], $this
        ->t('A valid container ID is case sensitive and formatted like GTM-xxxxxx.'));
    }
  }

  // Specific to the settings form.
  $uri = $form_state
    ->getValue('uri');
  if (!is_null($uri) && $form['#form_id'] == 'google_tag_settings') {
    $uri = trim($uri);
    $form_state
      ->setValue('uri', $uri);
    $directory = $uri;
    if (substr($directory, -3) == '://') {
      $args = [
        '%directory' => $directory,
      ];
      $message = 'The snippet parent uri %directory is invalid. Enter a single trailing slash to specify a plain stream wrapper.';
      $form_state
        ->setError($form['module']['uri'], $this
        ->t($message, $args));
    }

    // Allow for a plain stream wrapper with one trailing slash.
    $directory .= substr($directory, -2) == ':/' ? '/' : '';
    if (!is_dir($directory) || !_google_tag_is_writable($directory) || !_google_tag_is_executable($directory)) {
      $args = [
        '%directory' => $directory,
      ];
      $message = 'The snippet parent uri %directory is invalid, possibly due to file system permissions. The directory either does not exist, or is not writable or searchable.';
      $form_state
        ->setError($form['module']['uri'], $this
        ->t($message, $args));
    }
  }

  // Trim the text values.
  $environment_id = trim($form_state
    ->getValue('environment_id'));
  $form_state
    ->setValue('data_layer', trim($form_state
    ->getValue('data_layer')));
  $form_state
    ->setValue('path_list', $this
    ->cleanText($form_state
    ->getValue('path_list')));
  $form_state
    ->setValue('status_list', $this
    ->cleanText($form_state
    ->getValue('status_list')));
  $form_state
    ->setValue('whitelist_classes', $this
    ->cleanText($form_state
    ->getValue('whitelist_classes')));
  $form_state
    ->setValue('blacklist_classes', $this
    ->cleanText($form_state
    ->getValue('blacklist_classes')));

  // Replace all types of dashes (n-dash, m-dash, minus) with a normal dash.
  $environment_id = str_replace([
    '–',
    '—',
    '−',
  ], '-', $environment_id);
  $form_state
    ->setValue('environment_id', $environment_id);
  $form_state
    ->setValue('role_list', array_filter($form_state
    ->getValue('role_list')));
  if ($form_state
    ->getValue('include_environment') && !preg_match('/^env-\\d{1,}$/', $environment_id)) {
    $form_state
      ->setError($form['advanced']['environment_id'], $this
      ->t('A valid environment ID is case sensitive and formatted like env-x.'));
  }
}