summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorhass2016-08-09 21:26:53 (GMT)
committerhass2016-08-09 21:26:53 (GMT)
commitaed44c1c27341c14d2051ff5d4d24e5a0aab12d3 (patch)
treeb92f3a42139f493b6b4ba2f1940313093c8c76b2
parentc8eb7f18120d897b2d81fd4687ea30527606b27b (diff)
Issue #159865 by hass, larowlan: add JS snippets for google analytics8.x-2.1
permission
-rw-r--r--google_analytics.permissions.yml4
-rw-r--r--src/Form/GoogleAnalyticsAdminSettingsForm.php8
-rw-r--r--src/Tests/GoogleAnalyticsBasicTest.php29
3 files changed, 39 insertions, 2 deletions
diff --git a/google_analytics.permissions.yml b/google_analytics.permissions.yml
index 946dbd7..03542ee 100644
--- a/google_analytics.permissions.yml
+++ b/google_analytics.permissions.yml
@@ -8,3 +8,7 @@ use PHP for google analytics tracking visibility:
title: 'Use PHP for tracking visibility'
description: 'Enter PHP code in the field for tracking visibility settings.'
restrict access: true
+add JS snippets for google analytics:
+ title: 'Add JavaScript snippets'
+ description: 'Enter JavaScript code snippets for advanced Google Analytics functionality.'
+ restrict access: true
diff --git a/src/Form/GoogleAnalyticsAdminSettingsForm.php b/src/Form/GoogleAnalyticsAdminSettingsForm.php
index 79d6a11..b3c1817 100644
--- a/src/Form/GoogleAnalyticsAdminSettingsForm.php
+++ b/src/Form/GoogleAnalyticsAdminSettingsForm.php
@@ -484,6 +484,8 @@ class GoogleAnalyticsAdminSettingsForm extends ConfigFormBase {
];
}
+ $user_access_add_js_snippets = !$this->currentUser()->hasPermission('add JS snippets for google analytics');
+ $user_access_add_js_snippets_permission_warning = $user_access_add_js_snippets ? ' <em>' . $this->t('This field has been disabled because you do not have sufficient permissions to edit it.') . '</em>' : '';
$form['advanced']['codesnippet'] = [
'#type' => 'details',
'#title' => $this->t('Custom JavaScript code'),
@@ -502,15 +504,17 @@ class GoogleAnalyticsAdminSettingsForm extends ConfigFormBase {
'#type' => 'textarea',
'#title' => $this->t('Code snippet (before)'),
'#default_value' => $config->get('codesnippet.before'),
+ '#disabled' => $user_access_add_js_snippets,
'#rows' => 5,
- '#description' => $this->t('Code in this textarea will be added <strong>before</strong> <code>ga("send", "pageview");</code>.'),
+ '#description' => $this->t('Code in this textarea will be added <strong>before</strong> <code>ga("send", "pageview");</code>.') . $user_access_add_js_snippets_permission_warning,
];
$form['advanced']['codesnippet']['google_analytics_codesnippet_after'] = [
'#type' => 'textarea',
'#title' => $this->t('Code snippet (after)'),
'#default_value' => $config->get('codesnippet.after'),
+ '#disabled' => $user_access_add_js_snippets,
'#rows' => 5,
- '#description' => $this->t('Code in this textarea will be added <strong>after</strong> <code>ga("send", "pageview");</code>. This is useful if you\'d like to track a site in two accounts.'),
+ '#description' => $this->t('Code in this textarea will be added <strong>after</strong> <code>ga("send", "pageview");</code>. This is useful if you\'d like to track a site in two accounts.') . $user_access_add_js_snippets_permission_warning,
];
$form['advanced']['google_analytics_debug'] = [
diff --git a/src/Tests/GoogleAnalyticsBasicTest.php b/src/Tests/GoogleAnalyticsBasicTest.php
index 24ad281..f659606 100644
--- a/src/Tests/GoogleAnalyticsBasicTest.php
+++ b/src/Tests/GoogleAnalyticsBasicTest.php
@@ -14,6 +14,13 @@ use Drupal\simpletest\WebTestBase;
class GoogleAnalyticsBasicTest extends WebTestBase {
/**
+ * User without permissions to use snippets.
+ *
+ * @var \Drupal\user\UserInterface
+ */
+ protected $noSnippetUser;
+
+ /**
* Modules to enable.
*
* @var array
@@ -38,6 +45,8 @@ class GoogleAnalyticsBasicTest extends WebTestBase {
];
// User to set up google_analytics.
+ $this->noSnippetUser = $this->drupalCreateUser($permissions);
+ $permissions[] = 'add JS snippets for google analytics';
$this->admin_user = $this->drupalCreateUser($permissions);
$this->drupalLogin($this->admin_user);
@@ -68,6 +77,26 @@ class GoogleAnalyticsBasicTest extends WebTestBase {
$edit['google_analytics_account'] = $this->randomMachineName(2);
$this->drupalPostForm('admin/config/system/google-analytics', $edit, t('Save configuration'));
$this->assertRaw(t('A valid Google Analytics Web Property ID is case sensitive and formatted like UA-xxxxxxx-yy.'), '[testGoogleAnalyticsConfiguration]: Invalid Web Property ID number validated.');
+
+ // User should have access to code snippets.
+ $this->assertFieldByName('google_analytics_codesnippet_create');
+ $this->assertFieldByName('google_analytics_codesnippet_before');
+ $this->assertFieldByName('google_analytics_codesnippet_after');
+ $this->assertNoFieldByXPath("//textarea[@name='google_analytics_codesnippet_create' and @disabled='disabled']", NULL, '"Create only fields" is enabled.');
+ $this->assertNoFieldByXPath("//textarea[@name='google_analytics_codesnippet_before' and @disabled='disabled']", NULL, '"Code snippet (before)" is enabled.');
+ $this->assertNoFieldByXPath("//textarea[@name='google_analytics_codesnippet_after' and @disabled='disabled']", NULL, '"Code snippet (after)" is enabled.');
+
+ // Login as user without JS permissions.
+ $this->drupalLogin($this->noSnippetUser);
+ $this->drupalGet('admin/config/system/google-analytics');
+
+ // User should *not* have access to snippets, but create fields.
+ $this->assertFieldByName('google_analytics_codesnippet_create');
+ $this->assertFieldByName('google_analytics_codesnippet_before');
+ $this->assertFieldByName('google_analytics_codesnippet_after');
+ $this->assertNoFieldByXPath("//textarea[@name='google_analytics_codesnippet_create' and @disabled='disabled']", NULL, '"Create only fields" is enabled.');
+ $this->assertFieldByXPath("//textarea[@name='google_analytics_codesnippet_before' and @disabled='disabled']", NULL, '"Code snippet (before)" is disabled.');
+ $this->assertFieldByXPath("//textarea[@name='google_analytics_codesnippet_after' and @disabled='disabled']", NULL, '"Code snippet (after)" is disabled.');
}
/**