You are here

public function GoogleOptimizeAdminSettingsForm::submitForm in Google Optimize 8

Form submission 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 ConfigFormBase::submitForm

File

src/Form/GoogleOptimizeAdminSettingsForm.php, line 116

Class

GoogleOptimizeAdminSettingsForm
Implements Google Optimize Admin Settings form.

Namespace

Drupal\google_optimize\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $this->configFactory
    ->getEditable('google_optimize.settings')
    ->set('hide_page_enable', $form_state
    ->getValue('hide_page_enable'))
    ->set('container_ids', $form_state
    ->getValue('container_ids'))
    ->set('hide_page_timeout', $form_state
    ->getValue('hide_page_timeout'))
    ->set('hide_page_class_name', $form_state
    ->getValue('hide_page_class_name'))
    ->set('hide_page_pages', $form_state
    ->getValue('hide_page_pages'))
    ->set('hide_page_roles', $form_state
    ->getValue('hide_page_roles'))
    ->save();

  // Add the container id(s) to Google Analytics.
  $config = $this->configFactory
    ->getEditable('google_analytics.settings');
  $codesnippet = $config
    ->get('codesnippet');

  // Get the google_analytics version. The module does not have version info
  // So get from composer.json. If no composer.json then nothing can be done.
  $version = '';
  if ($info = json_decode(file_get_contents(realpath('composer.json')))) {
    foreach ($info->require as $key => $value) {
      if ($key == 'drupal/google_analytics') {
        $version = $value;
        break;
      }
    }
  }
  if (strpos($version, '3') === 0) {
    $codesnippet['before'] = "optimize_id|" . $form_state
      ->getValue('container_ids');
  }
  else {
    $codesnippet['before'] = "ga('require', '" . $form_state
      ->getValue('container_ids') . "');";
  }
  $config
    ->set('codesnippet', $codesnippet)
    ->save();
  parent::submitForm($form, $form_state);
}