Skip to content

Commit

Permalink
Merge pull request #1897 from acquia/ACMS-4229
Browse files Browse the repository at this point in the history
ACMS-4229: Update google_tag form on tour dashboard.
  • Loading branch information
vipin-mittal-acquia authored Oct 23, 2024
2 parents 3a57761 + ceee729 commit 063d591
Show file tree
Hide file tree
Showing 7 changed files with 247 additions and 42 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@
"php-http/discovery": true,
"phpro/grumphp-shim": true,
"phpstan/extension-installer": true,
"tbachert/spi": true,
"webdriver-binary/binary-chromedriver": true,
"wikimedia/composer-merge-plugin": true,
"tbachert/spi": true
Expand Down
2 changes: 1 addition & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions modules/acquia_cms_common/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@
"require-dev": {
"acquia/cohesion": "^7.4 || ^8.0",
"drupal/acquia_claro": "^1.3",
"drupal/node_revision_delete":"^2",
"drupal/reroute_email":"^2.2",
"drupal/node_revision_delete": "^2",
"drupal/reroute_email": "^2.2",
"drupal/shield": "^1.7"
},
"conflict": {
Expand Down
6 changes: 3 additions & 3 deletions modules/acquia_cms_toolbar/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
"drupal/acquia_cms_common": "^1.9 || ^2.1 || ^3.1",
"drupal/admin_toolbar": "^3.3"
},
"conflict": {
"drupal/acquia_claro": "<1.4"
},
"repositories": {
"assets": {
"type": "composer",
Expand All @@ -17,9 +20,6 @@
"url": "https://packages.drupal.org/8"
}
},
"conflict": {
"drupal/acquia_claro": "<1.4"
},
"config": {
"allow-plugins": {
"composer/installers": true,
Expand Down
8 changes: 8 additions & 0 deletions modules/acquia_cms_tour/css/acquia_cms_tour_dashboard.css
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,14 @@
.acms-dashboard-form-wrapper .js-form-wrapper {
border: 1px solid #808080;
}
.acms-dashboard-form-wrapper .js-form-wrapper .js-form-wrapper {
border: 0px;
padding: 0;
margin: 0;
}
.acms-dashboard-form-wrapper .js-form-wrapper .js-form-wrapper .fieldset__wrapper{
margin-left: 0;
}
.section-top {
display: flex;
align-items: center;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
namespace Drupal\acquia_cms_tour\Plugin\AcquiaCmsTour;

use Drupal\acquia_cms_tour\Form\AcquiaCmsDashboardBase;
use Drupal\Component\Utility\Html;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\google_tag\Entity\TagContainer;

/**
* Plugin implementation of the acquia_cms_tour.
Expand Down Expand Up @@ -62,15 +64,114 @@ public function buildForm(array $form, FormStateInterface $form_state) {
'#collapsible' => TRUE,
'#collapsed' => TRUE,
];
$form[$module]['snippet_parent_uri'] = [
'#type' => 'textfield',
'#required' => TRUE,
'#title' => $this->t('Snippet parent URI'),
'#attributes' => ['placeholder' => $this->t('public:/')],
'#default_value' => $this->config('google_tag.settings')->get('uri'),
'#prefix' => '<div class= "dashboard-fields-wrapper">' . $module_info['description'],
'#suffix' => "</div>",
$form['#id'] = Html::getId($form_state->getBuildInfo()['form_id']);
$accounts_wrapper_id = Html::getUniqueId('accounts-add-more-wrapper');
$account_default_value = $this->config('google_tag.settings')->get('default_google_tag_entity');
$form[$module]['accounts_wrapper'] = [
'#type' => 'fieldset',
'#prefix' => '<div class="dashboard-fields-wrapper remove-fieldset-boundary" id="' . $accounts_wrapper_id . '">
Effortlessly configure and manage Google Tag Manager containers to
seamlessly track application insights.',
'#suffix' => '</div>',
];
// Filter order (tabledrag).
$form[$module]['accounts_wrapper']['accounts'] = [
'#input' => FALSE,
'#tree' => TRUE,
'#type' => 'table',
'#tabledrag' => [
[
'action' => 'order',
'relationship' => 'sibling',
'group' => 'account-order-weight',
],
],
];

$accounts = $form_state->getValue('accounts', []);
if ($accounts === []) {
$config_name = 'google_tag.container.' . $account_default_value;
$entity_accounts = $this->config($config_name)->get('tag_container_ids');
if ($entity_accounts){
foreach ($entity_accounts as $index => $account) {
$accounts[$index]['value'] = $account;
$accounts[$index]['weight'] = $index;
}
}
// Default fallback.
if (count($accounts) === 0) {
$accounts[] = ['value' => '', 'weight' => 0];
}
}

foreach ($accounts as $index => $account) {
$form[$module]['accounts_wrapper']['accounts'][$index]['#attributes']['class'][] = 'draggable';
$form[$module]['accounts_wrapper']['accounts'][$index]['#weight'] = $account['weight'];
$form[$module]['accounts_wrapper']['accounts'][$index]['value'] = [
'#default_value' => (string) ($account['value'] ?? ''),
'#maxlength' => 20,
'#required' => (count($accounts) === 1),
'#size' => 20,
'#type' => 'textfield',
'#pattern' => TagContainer::GOOGLE_TAG_MATCH,
'#ajax' => [
'callback' => [self::class, 'storeGtagAccountsCallback'],
'disable-refocus' => TRUE,
'event' => 'change',
'wrapper' => 'advanced-settings-wrapper',
],
'#attributes' => [
'data-disable-refocus' => 'true',
],
];

$form[$module]['accounts_wrapper']['accounts'][$index]['weight'] = [
'#type' => 'weight',
'#title' => $this->t('Weight for @title', ['@title' => (string) ($account['value'] ?? '')]),
'#title_display' => 'invisible',
'#delta' => 50,
'#default_value' => $index,
'#parents' => ['accounts', $index, 'weight'],
'#attributes' => ['class' => ['account-order-weight']],
];

// If there is more than one id, add the remove button.
if (count($accounts) > 1) {
$form[$module]['accounts_wrapper']['accounts'][$index]['remove'] = [
'#type' => 'submit',
'#value' => $this->t('Remove'),
'#name' => 'remove_gtag_id_' . $index,
'#parameter_index' => $index,
'#limit_validation_errors' => [
['accounts'],
],
'#submit' => [
[self::class, 'removeGtagCallback'],
],
'#ajax' => [
'callback' => [self::class, 'gtagFormCallback'],
'wrapper' => $form['#id'],
],
];
}
}

$id_prefix = implode('-', ['accounts_wrapper', 'accounts']);
// Add blank account.
$form[$module]['accounts_wrapper']['add_gtag_id'] = [
'#type' => 'submit',
'#value' => $this->t('Add another ID'),
'#name' => str_replace('-', '_', $id_prefix) . '_add_gtag_id',
'#submit' => [
[self::class, 'addGtagCallback'],
],
'#ajax' => [
'callback' => [self::class, 'ajaxRefreshAccounts'],
'wrapper' => $accounts_wrapper_id,
'effect' => 'fade',
],
];

$form[$module]['actions']['submit'] = [
'#type' => 'submit',
'#value' => 'Save',
Expand All @@ -82,25 +183,23 @@ public function buildForm(array $form, FormStateInterface $form_state) {
'#limit_validation_errors' => [],
'#submit' => ['::ignoreConfig'],
];
if (isset($module_info['configure'])) {
$form[$module]['actions']['advanced'] = [
'#prefix' => '<div class= "dashboard-tooltiptext">',
'#markup' => $this->linkGenerator->generate(
'Advanced',
Url::fromRoute($module_info['configure'])
),
'#suffix' => "</div>",
];
$form[$module]['actions']['advanced']['information'] = [
'#prefix' => '<b class= "tool-tip__icon">i',
'#suffix' => "</b>",
];
$form[$module]['actions']['advanced']['tooltip-text'] = [
'#prefix' => '<span class= "tooltip">',
'#markup' => $this->t("Opens Advance Configuration in new tab"),
'#suffix' => "</span></div>",
];
}
$form[$module]['actions']['advanced'] = [
'#prefix' => '<div class= "dashboard-tooltiptext">',
'#markup' => $this->linkGenerator->generate(
'Advanced',
Url::fromRoute('entity.google_tag_container.single_form')
),
'#suffix' => "</div>",
];
$form[$module]['actions']['advanced']['information'] = [
'#prefix' => '<b class= "tool-tip__icon">i',
'#suffix' => "</b>",
];
$form[$module]['actions']['advanced']['tooltip-text'] = [
'#prefix' => '<span class= "tooltip">',
'#markup' => $this->t("Opens Advance Configuration in new tab"),
'#suffix' => "</span></div>",
];
}
return $form;
}
Expand All @@ -109,8 +208,44 @@ public function buildForm(array $form, FormStateInterface $form_state) {
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$snippet_parent_uri = $form_state->getValue(['snippet_parent_uri']);
$this->config('google_tag.settings')->set('uri', $snippet_parent_uri)->save();

$tag_container_ids = [];
$default_id = $config_id = '';
$account_default_value = $this->config('google_tag.settings')->get('default_google_tag_entity');

$config_name = 'google_tag.container.' . $account_default_value;
foreach ($form_state->getValue('accounts') as $account) {
if (!$default_id) {
$default_id = $account['value'];
}
$tag_container_ids[$account['weight']] = $account['value'];
}
if ($account_default_value == NULL) {
// Set the ID and Label based on the first Google Tag.
$config_name .= uniqid($default_id . '.', TRUE);
}
// Need to save tags without weights otherwise it doesn't show up on UI.
if ($this->config($config_name)->get('id') === NULL) {
// Set the ID and Label based on the first Google Tag.
$config_id = uniqid($default_id . '.', TRUE);
TagContainer::create([
'id' => $config_id,
'label' => $default_id,
'tag_container_ids' => array_values($tag_container_ids),
'status' => 1,
'weight' => 0,
])->save();
}
else {
$config_id = $this->config($config_name)->get('id');
$config = TagContainer::load($config_id);
$config->set('tag_container_ids', array_values($tag_container_ids));
$config->save();
}
if($this->configFactory->getEditable('google_tag.settings')->get('default_google_tag_entity') !== NULL) {
$this->configFactory->getEditable('google_tag.settings')->set('default_google_tag_entity', $config_id)->save();
}

$this->setConfigurationState();
$this->messenger()->addStatus('The configuration options have been saved.');
}
Expand All @@ -126,8 +261,71 @@ public function ignoreConfig(array &$form, FormStateInterface $form_state) {
* {@inheritdoc}
*/
public function checkMinConfiguration(): bool {
$uri = $this->config('google_tag.settings')->get('uri');
$uri = $this->config('google_tag.settings')->get('default_google_tag_entity');
return (bool) $uri;
}

/**
* Submit handler for the "add-one-more" button.
*
* Increments the max counter and causes a rebuild.
*/
public static function storeGtagAccountsCallback(array &$form, FormStateInterface $form_state) {
// Update Advanced Settings Form.
return $form['google_tag']['accounts_wrapper'];
}

/**
* Submit handler for the "remove one" button.
*
* Decrements the max counter and causes a form rebuild.
*/
public static function removeGtagCallback(array &$form, FormStateInterface $form_state) {
$triggering_element = $form_state->getTriggeringElement();
$index = $triggering_element['#parameter_index'];
$accounts = $form_state->getValue('accounts', []);
unset($accounts[$index]);
$form_state->setValue('accounts', $accounts);
$form_state->setRebuild();
}

/**
* Callback for both ajax account buttons.
*
* Selects and returns the fieldset with the names in it.
*/
public static function gtagFormCallback(array &$form, FormStateInterface $form_state) {
return $form;
}

/**
* Submit handler for the "add-one-more" button.
*
* Increments the max counter and causes a rebuild.
*/
public static function addGtagCallback(array &$form, FormStateInterface $form_state) {
$accounts = $form_state->getValue('accounts', []);
$accounts[] = [
'value' => '',
'weight' => count($accounts),
];
$form_state->setValue('accounts', $accounts);
$form_state->setRebuild();
}

/**
* Callback for add more gtag accounts.
*
* @param array $form
* Form array.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* Form state.
*
* @return mixed
* Accounts wrapper.
*/
public static function ajaxRefreshAccounts(array $form, FormStateInterface $form_state) {
return $form['google_tag']['accounts_wrapper'];
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,14 @@ public function testGoogleTagManager() {
$container = $assert_session->elementExists('css', '.acquia-cms-google-tag-manager-form');
// Assert that save and advanced buttons are present on form.
$assert_session->buttonExists('Save');
// Assert that the expected fields show up.
$assert_session->fieldExists('Snippet parent URI');
// Save Snippet parent URI.
$dummy_uri = 'public://tempdir';
$container->fillField('edit-snippet-parent-uri', $dummy_uri);
$dummy_tag = 'GT-XXXXXXXXX';
$container->fillField('edit-accounts-0-value', $dummy_tag);
$container->pressButton('Save');
$assert_session->pageTextContains('The configuration options have been saved.');
// Test that the config values we expect are set correctly.
$google_tag_uri = $this->config('google_tag.settings')->get('uri');
$this->assertSame($google_tag_uri, $dummy_uri);
$tag_id = $this->config($this->config('google_tag.settings')->get('default_google_tag_entity'))->get('tag_container_ids');
$this->assertEquals($tag_id, [$dummy_tag]);
}

}

0 comments on commit 063d591

Please sign in to comment.