class SettingsForm in GoogleTagManager 8
Defines the Google tag manager module and default container settings form.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\Core\Form\ConfigFormBase uses ConfigFormBaseTrait
- class \Drupal\google_tag\Form\SettingsForm uses ContainerTrait
- class \Drupal\Core\Form\ConfigFormBase uses ConfigFormBaseTrait
Expanded class hierarchy of SettingsForm
1 string reference to 'SettingsForm'
File
- src/
Form/ SettingsForm.php, line 12
Namespace
Drupal\google_tag\FormView 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
Name![]() |
Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ConfigFormBase:: |
public static | function |
Instantiates a new instance of this class. Overrides FormBase:: |
13 |
ConfigFormBase:: |
public | function | Constructs a \Drupal\system\ConfigFormBase object. | 11 |
ConfigFormBaseTrait:: |
protected | function | Retrieves a configuration object. | |
ContainerTrait:: |
protected | property | The container entity. | |
ContainerTrait:: |
protected | property | The property prefix that allows reuse by container and settings forms. | |
ContainerTrait:: |
public | function | Fieldset builder for the container settings form. | |
ContainerTrait:: |
public | function | Cleans a string representing a list of items. | |
ContainerTrait:: |
public | function | Fieldset builder for the container settings form. | |
ContainerTrait:: |
public | function | Fieldset builder for the container settings form. | |
ContainerTrait:: |
public | function | Fieldset builder for the container settings form. | |
ContainerTrait:: |
protected | function | Returns a translated string after placeholder substitution. | |
ContainerTrait:: |
public | function | Returns states array for a form element. | |
ContainerTrait:: |
public | function | Fieldset builder for the container settings form. | |
ContainerTrait:: |
public | function | ||
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
FormBase:: |
protected | property | The config factory. | 1 |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 1 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
protected | function | Gets the current user. | |
FormBase:: |
protected | function | Gets the request object. | |
FormBase:: |
protected | function | Gets the route match. | |
FormBase:: |
protected | function | Gets the logger for a specific channel. | |
FormBase:: |
protected | function |
Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: |
|
FormBase:: |
public | function | Resets the configuration factory. | |
FormBase:: |
public | function | Sets the config factory for this form. | |
FormBase:: |
public | function | Sets the request stack object to use. | |
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
SettingsForm:: |
public | function |
Form constructor. Overrides ConfigFormBase:: |
|
SettingsForm:: |
protected | function |
Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait:: |
|
SettingsForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
SettingsForm:: |
public | function | Fieldset builder for the module settings form. | |
SettingsForm:: |
public | function |
Form submission handler. Overrides ConfigFormBase:: |
|
SettingsForm:: |
public | function |
Form validation handler. Overrides FormBase:: |
|
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. |