You are here

function google_tag_settings_form_validate in GoogleTagManager 7.2

Same name and namespace in other branches
  1. 7 includes/admin.inc \google_tag_settings_form_validate()

Form validation handler for google_tag_settings_form().

File

includes/form/settings.inc, line 201
Contains the settings form callbacks.

Code

function google_tag_settings_form_validate($form, &$form_state) {
  $values =& $form_state['values'];

  // Trim the text values.
  // $values['container_id'] = trim($values['container_id']);
  $values['uri'] = trim($values['uri']);
  $values['environment_id'] = trim($values['environment_id']);
  $values['data_layer'] = trim($values['data_layer']);
  google_tag_text_clean($values['path_list']);
  google_tag_text_clean($values['status_list']);
  google_tag_text_clean($values['whitelist_classes']);
  google_tag_text_clean($values['blacklist_classes']);

  // Replace all types of dashes (n-dash, m-dash, minus) with a normal dash.
  // $values['container_id'] = str_replace(array('–', '—', '−'), '-', $values['container_id']);
  $values['environment_id'] = str_replace(array(
    '–',
    '—',
    '−',
  ), '-', $values['environment_id']);
  $values['role_list'] = array_filter($values['role_list']);

  /*
    if (!preg_match('/^GTM-\w{4,}$/', $values['container_id'])) {
      // @todo Is there a more specific regular expression that applies?
      // @todo Is there a way to "test the connection" to determine a valid ID for
      // a container? It may be valid but not the correct one for the website.
      form_set_error('container_id', t('A valid container ID is case sensitive and formatted like GTM-xxxxxx.'));
    }
  */
  if ($values['include_environment'] && !preg_match('/^env-\\d{1,}$/', $values['environment_id'])) {
    form_set_error('environment_id', t('A valid environment ID is case sensitive and formatted like env-x.'));
  }
  if ($message = _google_tag_data_layer_verify($values['data_layer'])) {
    form_set_error('data_layer', $message);
  }
  if ($values['include_classes']) {
    if (empty($values['whitelist_classes']) && empty($values['blacklist_classes'])) {
      form_set_error('include_classes', t('Enter listed classes in at least one field, or uncheck the box.'));
      form_set_error('whitelist_classes', '');
      form_set_error('blacklist_classes', '');
    }
  }
  $directory = $values['uri'];
  if (substr($directory, -3) == '://') {
    $args = array(
      '%directory' => $directory,
    );
    $message = 'The snippet parent uri %directory is invalid. Enter a single trailing slash to specify a plain stream wrapper.';
    form_set_error('uri', 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 = array(
      '%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_set_error('uri', t($message, $args));
  }
}