You are here

public function SettingsForm::submitForm in GoogleTagManager 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/SettingsForm.php, line 145

Class

SettingsForm
Defines the Google tag manager module and default container settings form.

Namespace

Drupal\google_tag\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $config = $this
    ->config('google_tag.settings');
  $old_uri = $config
    ->get('uri');
  $settings = $config
    ->get();
  unset($settings['_default_container'], $settings['_core']);
  foreach (array_keys($settings) as $key) {
    $config
      ->set($key, $form_state
      ->getValue($key));
  }
  $default_container = $config
    ->get('_default_container');
  unset($default_container['container_id']);
  foreach (array_keys($default_container) as $key) {
    $config
      ->set("_default_container.{$key}", $form_state
      ->getValue($key));
  }
  $config
    ->save();
  parent::submitForm($form, $form_state);

  // @todo Only display if a container exists?
  $message = 'Changes to default container settings and insertion conditions <strong>only apply to new containers</strong>. To modify settings for existing containers, click the container management link below.';
  $args = [
    '%directory' => $old_uri . '/google_tag',
  ];
  $this
    ->messenger()
    ->addWarning($this
    ->t($message, $args));
  $new_uri = $config
    ->get('uri');
  if ($old_uri != $new_uri) {

    // The snippet uri changed; recreate snippets for all containers.
    global $_google_tag_display_message;
    $_google_tag_display_message = TRUE;
    _google_tag_assets_create();
    $message = 'The snippet directory was changed and the snippet files were created in the new directory. The old directory at %directory was not deleted.';
    $args = [
      '%directory' => $old_uri . '/google_tag',
    ];
    $this
      ->messenger()
      ->addWarning($this
      ->t($message, $args));
  }
}