You are here

class SettingsForm in GoogleTagManager 8

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

Hierarchy

Expanded class hierarchy of SettingsForm

1 string reference to 'SettingsForm'
google_tag.routing.yml in ./google_tag.routing.yml
google_tag.routing.yml

File

src/Form/SettingsForm.php, line 12

Namespace

Drupal\google_tag\Form
View source
class SettingsForm extends ConfigFormBase {
  use ContainerTrait;

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'google_tag_settings';
  }

  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames() {
    return [
      'google_tag.settings',
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $this->container = $this
      ->config('google_tag.settings');
    $this->prefix = '_default_container.';

    // Build form elements.
    $description = $this
      ->t('<br />After configuring the module settings and default properties for a new container, <strong>add a container</strong> on the <a href=":url">container management page</a>.', [
      ':url' => Url::fromRoute('entity.google_tag_container.collection')
        ->toString(),
    ]);
    $form['instruction'] = [
      '#type' => 'markup',
      '#markup' => $description,
    ];
    $form['module'] = $this
      ->moduleFieldset($form_state);
    $form['settings'] = [
      '#type' => 'vertical_tabs',
      '#title' => $this
        ->t('Default container settings'),
      '#description' => $this
        ->t('The default container settings that apply to a new container.'),
      '#attributes' => [
        'class' => [
          'google-tag',
        ],
      ],
    ];
    $form['conditions'] = [
      '#type' => 'vertical_tabs',
      '#title' => $this
        ->t('Default insertion conditions'),
      '#description' => $this
        ->t('The default snippet insertion conditions that apply to a new container.'),
      '#attributes' => [
        'class' => [
          'google-tag',
        ],
      ],
      '#attached' => [
        'library' => [
          'google_tag/drupal.settings_form',
        ],
      ],
    ];
    $form['advanced'] = $this
      ->advancedFieldset($form_state);
    $form['path'] = $this
      ->pathFieldset($form_state);
    $form['role'] = $this
      ->roleFieldset($form_state);
    $form['status'] = $this
      ->statusFieldset($form_state);
    return parent::buildForm($form, $form_state);
  }

  /**
   * Fieldset builder for the module settings form.
   */
  public function moduleFieldset(FormStateInterface $form_state) {
    $config = $this
      ->config('google_tag.settings');

    // Build form elements.
    $fieldset = [
      '#type' => 'fieldset',
      '#title' => $this
        ->t('Module settings'),
      '#description' => $this
        ->t('The settings that apply to all containers.'),
      '#collapsible' => TRUE,
      '#collapsed' => FALSE,
    ];
    $fieldset['uri'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Snippet parent URI'),
      '#description' => $this
        ->t('The parent URI for saving snippet files. Snippet files will be saved to "[uri]/google_tag". Enter a plain stream wrapper with a single trailing slash like "public:/".'),
      '#default_value' => $config
        ->get('uri'),
      '#attributes' => [
        'placeholder' => [
          'public:/',
        ],
      ],
      '#required' => TRUE,
    ];
    $fieldset['compact_snippet'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Compact the JavaScript snippet'),
      '#description' => $this
        ->t('If checked, then the JavaScript snippet will be compacted to remove unnecessary whitespace. This is <strong>recommended on production sites</strong>. Leave unchecked to output a snippet that can be examined using a JavaScript debugger in the browser.'),
      '#default_value' => $config
        ->get('compact_snippet'),
    ];
    $fieldset['include_file'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Include the snippet as a file'),
      '#description' => $this
        ->t('If checked, then each JavaScript snippet will be included as a file. This is <strong>recommended</strong>. Leave unchecked to inline each snippet into the page. This only applies to data layer and script snippets.'),
      '#default_value' => $config
        ->get('include_file'),
    ];
    $fieldset['rebuild_snippets'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Recreate snippets on cache rebuild'),
      '#description' => $this
        ->t('If checked, then the JavaScript snippet files will be created during a cache rebuild. This is <strong>recommended on production sites</strong>. If not checked, any missing snippet files will be created during a page response.'),
      '#default_value' => $config
        ->get('rebuild_snippets'),
    ];
    $fieldset['flush_snippets'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Flush snippet directory on cache rebuild'),
      '#description' => $this
        ->t('If checked, then the snippet directory will be deleted during a cache rebuild. If not checked, then manual intervention may be required to tidy up the snippet directory (e.g. remove snippet files for a deleted container).'),
      '#default_value' => $config
        ->get('flush_snippets'),
    ];
    $fieldset['debug_output'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Show debug output'),
      '#description' => $this
        ->t('If checked, then the result of each snippet insertion condition will be shown in the message area. Enable <strong>only for development</strong> purposes.'),
      '#default_value' => $config
        ->get('debug_output'),
    ];
    return $fieldset;
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    $this
      ->validateFormValues($form, $form_state);
    parent::validateForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  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));
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigFormBase::create public static function Instantiates a new instance of this class. Overrides FormBase::create 13
ConfigFormBase::__construct public function Constructs a \Drupal\system\ConfigFormBase object. 11
ConfigFormBaseTrait::config protected function Retrieves a configuration object.
ContainerTrait::$container protected property The container entity.
ContainerTrait::$prefix protected property The property prefix that allows reuse by container and settings forms.
ContainerTrait::advancedFieldset public function Fieldset builder for the container settings form.
ContainerTrait::cleanText public function Cleans a string representing a list of items.
ContainerTrait::genericFieldset public function Fieldset builder for the container settings form.
ContainerTrait::pathFieldset public function Fieldset builder for the container settings form.
ContainerTrait::roleFieldset public function Fieldset builder for the container settings form.
ContainerTrait::specialT protected function Returns a translated string after placeholder substitution.
ContainerTrait::statesArray public function Returns states array for a form element.
ContainerTrait::statusFieldset public function Fieldset builder for the container settings form.
ContainerTrait::validateFormValues public function
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FormBase::$configFactory protected property The config factory. 1
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::configFactory protected function Gets the config factory for this form. 1
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
SettingsForm::buildForm public function Form constructor. Overrides ConfigFormBase::buildForm
SettingsForm::getEditableConfigNames protected function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
SettingsForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
SettingsForm::moduleFieldset public function Fieldset builder for the module settings form.
SettingsForm::submitForm public function Form submission handler. Overrides ConfigFormBase::submitForm
SettingsForm::validateForm public function Form validation handler. Overrides FormBase::validateForm
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.