From aaaf058bb8371f0ace906dccb98891611e048230 Mon Sep 17 00:00:00 2001 From: OscarMerida Date: Mon, 28 Oct 2024 16:08:50 -0400 Subject: [PATCH 1/5] USAGOV-1789-wizard-datalayer: Add datalayer alter listener for taxonomy term wizards. --- .../src/TaxonomyDatalayerBuilder.php | 4 +- .../custom/usa_twig_vars/usa_twig_vars.module | 1 + .../DatalayerAlterSubscriber.php | 122 ++++++++++++++++++ .../usagov_wizard/usagov_wizard.services.yml | 5 + .../html--taxonomy--term--wizard.html.twig | 2 +- 5 files changed, 132 insertions(+), 2 deletions(-) create mode 100644 web/modules/custom/usagov_wizard/src/EventSubscriber/DatalayerAlterSubscriber.php diff --git a/web/modules/custom/usa_twig_vars/src/TaxonomyDatalayerBuilder.php b/web/modules/custom/usa_twig_vars/src/TaxonomyDatalayerBuilder.php index 96ff990c2d..e6542e6b72 100644 --- a/web/modules/custom/usa_twig_vars/src/TaxonomyDatalayerBuilder.php +++ b/web/modules/custom/usa_twig_vars/src/TaxonomyDatalayerBuilder.php @@ -4,6 +4,7 @@ use Drupal\Core\Breadcrumb\BreadcrumbManager; use Drupal\Core\Entity\EntityMalformedException; +use Drupal\Core\Routing\CurrentRouteMatch; use Drupal\node\Entity\Node; /** @@ -47,6 +48,7 @@ class TaxonomyDatalayerBuilder { private string $isFront; public function __construct( + private CurrentRouteMatch $routeMatch, private BreadcrumbManager $breadcrumbManager, public Node $node, bool $isFront, @@ -129,7 +131,7 @@ public function build(): array { public function fromBreadcrumb(): array { // For all other pages, we need the breadcrumb to pass as taxonomy. // This mimics the system breadcrumb block plugin, without rendering it. - $crumbs = $this->breadcrumbManager->build(\Drupal::routeMatch()); + $crumbs = $this->breadcrumbManager->build($this->routeMatch); $taxonomy = []; foreach ($crumbs->getLinks() as $index => $crumb) { diff --git a/web/modules/custom/usa_twig_vars/usa_twig_vars.module b/web/modules/custom/usa_twig_vars/usa_twig_vars.module index fdc35b2ab0..35c7fbbb78 100644 --- a/web/modules/custom/usa_twig_vars/usa_twig_vars.module +++ b/web/modules/custom/usa_twig_vars/usa_twig_vars.module @@ -74,6 +74,7 @@ function usa_twig_vars_preprocess(&$variables, $hook) { // Data to be sent to GTM. $datalayer = new TaxonomyDatalayerBuilder( + routeMatch: \Drupal::routeMatch(), breadcrumbManager: \Drupal::service('breadcrumb'), node: $node, isFront: $isFront, diff --git a/web/modules/custom/usagov_wizard/src/EventSubscriber/DatalayerAlterSubscriber.php b/web/modules/custom/usagov_wizard/src/EventSubscriber/DatalayerAlterSubscriber.php new file mode 100644 index 0000000000..06174b38b7 --- /dev/null +++ b/web/modules/custom/usagov_wizard/src/EventSubscriber/DatalayerAlterSubscriber.php @@ -0,0 +1,122 @@ + 'onDatalayerAlter', + ]; + } + + /** + * Adds wizard taxonomy information to the datalayer. + */ + public function onDatalayerAlter(DatalayerAlterEvent $event): void { + $term = $this->currentRouteMatch->getParameter('taxonomy_term'); + if (!$term || $term->bundle() !== 'wizard') { + return; + } + + $termStorage = $this->entityTypeManager->getStorage('taxonomy_term'); + + $isStartPage = FALSE; + + $children = $termStorage->loadChildren($term->id()); + $isResult = empty($children); + + if ($term->hasField('parent')) { + $parentTID = $term->parent->getValue()[0]['target_id']; + if ($parentTID === '0') { + $isStartPage = TRUE; + } + } + + + if ($isStartPage) { + $page_type = 'wizard-start'; + } elseif ($isResult) { + $page_type = 'wizard-result'; + } else { + $page_type = 'wizard-question'; + } + + // make any changes need to $event->datalayer array + $event->datalayer['taxonomyID'] = $term->id(); + $event->datalayer['contentType'] = $term->bundle(); + $event->datalayer['language'] = $term->language()->getId(); + $event->datalayer['homepageTest'] = 'not_homepage'; + $event->datalayer['basicPagesubType'] = null; + $event->datalayer['Page_Type'] = $page_type; + $event->datalayer['hasBenefitCategory'] = FALSE; + + $crumbs = $this->breadcrumbManager->build($this->currentRouteMatch); + $links = $crumbs->getLinks(); + + $data = []; + foreach ($links as $i => $link) { + $data[$link->getUrl()->toString()] = $link->getText(); + } + + // add the parents + $vocabParents = $termStorage->loadParents($term->id()); + foreach ($vocabParents as $parent) { + $url = $parent->get('path')->alias; + $data[$url] = $parent->getName(); + } + + $termURL = $term->get('path')->alias; + $data[$termURL] = $term->getName(); + + $count = count($data); + + $i = 0; + foreach ($data as $url => $text) { + $i++; + $urls['Taxonomy_Text_' . $i] = $text; + $urls['Taxonomy_URL_' . $i] = $url; + + if ($i === 6) { + break; + } + } + + if ($i < 6) { + $lastURL = array_key_last($data); + $lastText = $data[$lastURL]; + + for ($i = $count; $i < 6; $i++) { + $urls['Taxonomy_Text_' . ($i + 1)] = $lastText; + $urls['Taxonomy_URL_' . ($i + 1)] = $lastURL; + } + } + + ksort($urls); + $event->datalayer = array_merge($event->datalayer, $urls); + } + + private function getParents(Term $node) { + + } + +} diff --git a/web/modules/custom/usagov_wizard/usagov_wizard.services.yml b/web/modules/custom/usagov_wizard/usagov_wizard.services.yml index 5776fca5ee..cbd10b3803 100755 --- a/web/modules/custom/usagov_wizard/usagov_wizard.services.yml +++ b/web/modules/custom/usagov_wizard/usagov_wizard.services.yml @@ -4,3 +4,8 @@ services: arguments: - '@entity_type.manager' - '@current_route_match' + usagov_wizard_datalayer_alter_subscriber: + class: '\Drupal\usagov_wizard\EventSubscriber\DatalayerAlterSubscriber' + arguments: ['@breadcrumb', '@current_route_match', '@entity_type.manager'] + tags: + - { name: 'event_subscriber' } diff --git a/web/themes/custom/usagov/templates/wizard_2024/html--taxonomy--term--wizard.html.twig b/web/themes/custom/usagov/templates/wizard_2024/html--taxonomy--term--wizard.html.twig index 0008b11c6d..f7a892a2b1 100644 --- a/web/themes/custom/usagov/templates/wizard_2024/html--taxonomy--term--wizard.html.twig +++ b/web/themes/custom/usagov/templates/wizard_2024/html--taxonomy--term--wizard.html.twig @@ -16,7 +16,7 @@ {% endif %} {% endblock %} -{% block taxonomy_data %} +{% block taxonomy_data_old %} {% set titleCounter = 4 %} {% set urlCounter = 4 %} {% if start_page %} From da23136f14f55779a78efb677223f0a5e9b54c80 Mon Sep 17 00:00:00 2001 From: OscarMerida Date: Tue, 29 Oct 2024 11:30:19 -0400 Subject: [PATCH 2/5] USAGOV-1789-wizard-datalayer: Replicate data prep in event instead of preprocess hook --- .../src/TaxonomyDatalayerBuilder.php | 8 +-- .../DatalayerAlterSubscriber.php | 46 +++++++++------- .../custom/usagov_wizard/usagov_wizard.module | 53 +++++++++++-------- .../usagov_wizard/usagov_wizard.services.yml | 2 +- 4 files changed, 63 insertions(+), 46 deletions(-) diff --git a/web/modules/custom/usa_twig_vars/src/TaxonomyDatalayerBuilder.php b/web/modules/custom/usa_twig_vars/src/TaxonomyDatalayerBuilder.php index e6542e6b72..cce076f5f1 100644 --- a/web/modules/custom/usa_twig_vars/src/TaxonomyDatalayerBuilder.php +++ b/web/modules/custom/usa_twig_vars/src/TaxonomyDatalayerBuilder.php @@ -12,11 +12,11 @@ */ class TaxonomyDatalayerBuilder { - private const HOME_TITLE_EN = "Home"; - private const HOME_URL_EN = "/"; + public const HOME_TITLE_EN = "Home"; + public const HOME_URL_EN = "/"; - private const HOME_TITLE_ES = "Página principal"; - private const HOME_URL_ES = "/es/"; + public const HOME_TITLE_ES = "Página principal"; + public const HOME_URL_ES = "/es/"; private const ABOUT_GOVT_EN = "About the U.S. and its government"; private const ABOUT_URL_EN = "/about-the-us"; diff --git a/web/modules/custom/usagov_wizard/src/EventSubscriber/DatalayerAlterSubscriber.php b/web/modules/custom/usagov_wizard/src/EventSubscriber/DatalayerAlterSubscriber.php index 06174b38b7..9efceb40bb 100644 --- a/web/modules/custom/usagov_wizard/src/EventSubscriber/DatalayerAlterSubscriber.php +++ b/web/modules/custom/usagov_wizard/src/EventSubscriber/DatalayerAlterSubscriber.php @@ -5,16 +5,18 @@ use Drupal\Core\Breadcrumb\BreadcrumbManager; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Routing\CurrentRouteMatch; +use Drupal\Core\Url; use Drupal\taxonomy\Entity\Term; use Drupal\usa_twig_vars\Event\DatalayerAlterEvent; +use Drupal\usagov_wizard\MenuChecker; use Symfony\Component\EventDispatcher\EventSubscriberInterface; - /** * Add taxonomy scan wizard info to datalayer. */ class DatalayerAlterSubscriber implements EventSubscriberInterface { public function __construct( + private MenuChecker $menuChecker, private BreadcrumbManager $breadcrumbManager, private CurrentRouteMatch $currentRouteMatch, private EntityTypeManagerInterface $entityTypeManager, @@ -61,6 +63,7 @@ public function onDatalayerAlter(DatalayerAlterEvent $event): void { $page_type = 'wizard-question'; } + // make any changes need to $event->datalayer array $event->datalayer['taxonomyID'] = $term->id(); $event->datalayer['contentType'] = $term->bundle(); @@ -70,25 +73,34 @@ public function onDatalayerAlter(DatalayerAlterEvent $event): void { $event->datalayer['Page_Type'] = $page_type; $event->datalayer['hasBenefitCategory'] = FALSE; - $crumbs = $this->breadcrumbManager->build($this->currentRouteMatch); - $links = $crumbs->getLinks(); - - $data = []; - foreach ($links as $i => $link) { - $data[$link->getUrl()->toString()] = $link->getText(); + $rootTerm = null; + $parents = []; + if ($term->hasField('parent') && !$term->get('parent')->isEmpty()) { + $parents = $this->entityTypeManager + ->getStorage('taxonomy_term') + ->loadAllParents($term->id()); + // Sort parents so "oldest ancestor" is first. + $parents = array_reverse($parents); + $rootTerm = $parents[array_key_first($parents)]; } - // add the parents - $vocabParents = $termStorage->loadParents($term->id()); - foreach ($vocabParents as $parent) { - $url = $parent->get('path')->alias; - $data[$url] = $parent->getName(); + if ($rootTerm) { + $crumbs = usagov_wizard_get_term_breadcrumb($rootTerm); + // Here the first two items will give us the home page + // and the main scam page + $crumbs = array_slice($crumbs, 0, 2); + foreach ($crumbs as $crumb) { + $data[$crumb['url']] = $crumb['text']; + } } - $termURL = $term->get('path')->alias; - $data[$termURL] = $term->getName(); + // the rest comes from the parents of this term + foreach ($parents as $parentTerm) { + $termURL = $parentTerm->get('path')->alias; + $data[$termURL] = $parentTerm->getName(); + } - $count = count($data); + $count = count($data); $i = 0; foreach ($data as $url => $text) { @@ -115,8 +127,4 @@ public function onDatalayerAlter(DatalayerAlterEvent $event): void { $event->datalayer = array_merge($event->datalayer, $urls); } - private function getParents(Term $node) { - - } - } diff --git a/web/modules/custom/usagov_wizard/usagov_wizard.module b/web/modules/custom/usagov_wizard/usagov_wizard.module index 355f14d574..09b065980d 100644 --- a/web/modules/custom/usagov_wizard/usagov_wizard.module +++ b/web/modules/custom/usagov_wizard/usagov_wizard.module @@ -10,6 +10,7 @@ use Drupal\Core\Url; use Drupal\taxonomy\Entity\Term; +use Drupal\usa_twig_vars\TaxonomyDatalayerBuilder; use Drupal\usagov_wizard\MenuChecker; /** @@ -299,7 +300,7 @@ function usagov_wizard_preprocess_page(&$variables) { } /** - * Implements hook_preprocess_HOOK(). + * Implements hook_preprocess_taxonomy_term(). * * Determine where the current page sits in the taxonomy hierarchy and send this * information to the twig so we can decide which of the "previous, next, start @@ -379,30 +380,38 @@ function usagov_wizard_preprocess_breadcrumb(&$variables) { $rootTermTid = $parents[array_key_last($parents)]; if (isset($rootTermTid)) { $rootTerm = Term::load($rootTermTid); - if (isset($rootTerm->field_wizard_breadcrumbs)) { - $crumbs = $rootTerm->field_wizard_breadcrumbs->getValue(); - foreach ($crumbs as $crumb) { - if (isset($crumb['uri'])) { - $crumb['url'] = Url::fromUri($crumb['uri'])->toString(); - } - if (isset($crumb['title'])) { - $crumb['text'] = ($crumb['title']); - } - - $variables['wizard_breadcrumb'][] = $crumb; - } - $language = $taxonomy_term->langcode->value; - $home = [ - 'text' => $language == 'en' ? 'Home' : 'Página principal', - 'url' => $language == 'en' ? '/' : '/es', - ]; - if ($crumbs != NULL) { - array_unshift($variables['wizard_breadcrumb'], $home); - } - } + $variables['wizard_breadcrumb'] = usagov_wizard_get_term_breadcrumb($rootTerm); + } + } + } +} + +function usagov_wizard_get_term_breadcrumb(Term $rootTerm): array { + if (isset($rootTerm->field_wizard_breadcrumbs)) { + $crumbs = $rootTerm->field_wizard_breadcrumbs->getValue(); + $result = []; + foreach ($crumbs as $crumb) { + if (isset($crumb['uri'])) { + $crumb['url'] = Url::fromUri($crumb['uri'])->toString(); } + if (isset($crumb['title'])) { + $crumb['text'] = ($crumb['title']); + } + + $result[] = $crumb; } + $language = $rootTerm->langcode->value; + $home = [ + 'text' => $language === 'en' ? TaxonomyDatalayerBuilder::HOME_TITLE_EN : TaxonomyDatalayerBuilder::HOME_TITLE_ES, + 'url' => $language === 'en' ? TaxonomyDatalayerBuilder::HOME_URL_EN: TaxonomyDatalayerBuilder::HOME_URL_ES, + ]; + if ($crumbs != NULL) { + array_unshift($result, $home); + } + return $result; } + + return []; } /** diff --git a/web/modules/custom/usagov_wizard/usagov_wizard.services.yml b/web/modules/custom/usagov_wizard/usagov_wizard.services.yml index cbd10b3803..7e1929dde3 100755 --- a/web/modules/custom/usagov_wizard/usagov_wizard.services.yml +++ b/web/modules/custom/usagov_wizard/usagov_wizard.services.yml @@ -6,6 +6,6 @@ services: - '@current_route_match' usagov_wizard_datalayer_alter_subscriber: class: '\Drupal\usagov_wizard\EventSubscriber\DatalayerAlterSubscriber' - arguments: ['@breadcrumb', '@current_route_match', '@entity_type.manager'] + arguments: ['@usagov_wizard.menu_checker', '@breadcrumb', '@current_route_match', '@entity_type.manager'] tags: - { name: 'event_subscriber' } From 6dd1fa495ad7b6e1ea3d6778fdd7290cdaba6e8b Mon Sep 17 00:00:00 2001 From: OscarMerida Date: Tue, 29 Oct 2024 11:59:00 -0400 Subject: [PATCH 3/5] USAGOV-1789-wizard-datalayer: Move the hasBenefitCategory element out of the first slot to match current order of keys. --- .../src/EventSubscriber/DatalayerAlterSubscriber.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/web/modules/custom/usagov_wizard/src/EventSubscriber/DatalayerAlterSubscriber.php b/web/modules/custom/usagov_wizard/src/EventSubscriber/DatalayerAlterSubscriber.php index 9efceb40bb..8cb50bcef3 100644 --- a/web/modules/custom/usagov_wizard/src/EventSubscriber/DatalayerAlterSubscriber.php +++ b/web/modules/custom/usagov_wizard/src/EventSubscriber/DatalayerAlterSubscriber.php @@ -63,7 +63,8 @@ public function onDatalayerAlter(DatalayerAlterEvent $event): void { $page_type = 'wizard-question'; } - + // keep the same order + unset($event->datalayer['hasBenefitCategory']); // make any changes need to $event->datalayer array $event->datalayer['taxonomyID'] = $term->id(); $event->datalayer['contentType'] = $term->bundle(); From 6f96cce42b4167c0516c6942ac7cd98b1751fc7b Mon Sep 17 00:00:00 2001 From: OscarMerida Date: Tue, 29 Oct 2024 15:12:49 -0400 Subject: [PATCH 4/5] USAGOV-1789-wizard-datalayer: Fixes PHP Linting errors --- .../DatalayerAlterSubscriber.php | 26 ++++++++++++------- .../custom/usagov_wizard/usagov_wizard.module | 2 +- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/web/modules/custom/usagov_wizard/src/EventSubscriber/DatalayerAlterSubscriber.php b/web/modules/custom/usagov_wizard/src/EventSubscriber/DatalayerAlterSubscriber.php index 8cb50bcef3..8210c7f883 100644 --- a/web/modules/custom/usagov_wizard/src/EventSubscriber/DatalayerAlterSubscriber.php +++ b/web/modules/custom/usagov_wizard/src/EventSubscriber/DatalayerAlterSubscriber.php @@ -10,6 +10,7 @@ use Drupal\usa_twig_vars\Event\DatalayerAlterEvent; use Drupal\usagov_wizard\MenuChecker; use Symfony\Component\EventDispatcher\EventSubscriberInterface; + /** * Add taxonomy scan wizard info to datalayer. */ @@ -42,8 +43,7 @@ public function onDatalayerAlter(DatalayerAlterEvent $event): void { $termStorage = $this->entityTypeManager->getStorage('taxonomy_term'); - $isStartPage = FALSE; - + $isStartPage = FALSE; $children = $termStorage->loadChildren($term->id()); $isResult = empty($children); @@ -54,12 +54,13 @@ public function onDatalayerAlter(DatalayerAlterEvent $event): void { } } - if ($isStartPage) { $page_type = 'wizard-start'; - } elseif ($isResult) { + } + elseif ($isResult) { $page_type = 'wizard-result'; - } else { + } + else { $page_type = 'wizard-question'; } @@ -70,16 +71,16 @@ public function onDatalayerAlter(DatalayerAlterEvent $event): void { $event->datalayer['contentType'] = $term->bundle(); $event->datalayer['language'] = $term->language()->getId(); $event->datalayer['homepageTest'] = 'not_homepage'; - $event->datalayer['basicPagesubType'] = null; + $event->datalayer['basicPagesubType'] = NULL; $event->datalayer['Page_Type'] = $page_type; $event->datalayer['hasBenefitCategory'] = FALSE; - $rootTerm = null; + $rootTerm = NULL; $parents = []; if ($term->hasField('parent') && !$term->get('parent')->isEmpty()) { $parents = $this->entityTypeManager - ->getStorage('taxonomy_term') - ->loadAllParents($term->id()); + ->getStorage('taxonomy_term') + ->loadAllParents($term->id()); // Sort parents so "oldest ancestor" is first. $parents = array_reverse($parents); $rootTerm = $parents[array_key_first($parents)]; @@ -97,7 +98,12 @@ public function onDatalayerAlter(DatalayerAlterEvent $event): void { // the rest comes from the parents of this term foreach ($parents as $parentTerm) { - $termURL = $parentTerm->get('path')->alias; + $path = $parentTerm->get('path'); + $termURL = $path->alias; + // pathalias field items don't prepend the language code for Spanish terms + if ($parentTerm->language()->getId() === 'es') { + $termURL = '/es' . $termURL; + } $data[$termURL] = $parentTerm->getName(); } diff --git a/web/modules/custom/usagov_wizard/usagov_wizard.module b/web/modules/custom/usagov_wizard/usagov_wizard.module index 09b065980d..518333b810 100644 --- a/web/modules/custom/usagov_wizard/usagov_wizard.module +++ b/web/modules/custom/usagov_wizard/usagov_wizard.module @@ -403,7 +403,7 @@ function usagov_wizard_get_term_breadcrumb(Term $rootTerm): array { $language = $rootTerm->langcode->value; $home = [ 'text' => $language === 'en' ? TaxonomyDatalayerBuilder::HOME_TITLE_EN : TaxonomyDatalayerBuilder::HOME_TITLE_ES, - 'url' => $language === 'en' ? TaxonomyDatalayerBuilder::HOME_URL_EN: TaxonomyDatalayerBuilder::HOME_URL_ES, + 'url' => $language === 'en' ? TaxonomyDatalayerBuilder::HOME_URL_EN : TaxonomyDatalayerBuilder::HOME_URL_ES, ]; if ($crumbs != NULL) { array_unshift($result, $home); From 95ba1eb3846a0b33120049bd2e13b52ca615b4dc Mon Sep 17 00:00:00 2001 From: OscarMerida Date: Tue, 29 Oct 2024 15:17:41 -0400 Subject: [PATCH 5/5] USAGOV-1789-wizard-datalayer: Removing now-unused custom datalayer block --- .../custom/usagov_wizard/usagov_wizard.module | 2 +- .../html--taxonomy--term--wizard.html.twig | 100 ------------------ 2 files changed, 1 insertion(+), 101 deletions(-) diff --git a/web/modules/custom/usagov_wizard/usagov_wizard.module b/web/modules/custom/usagov_wizard/usagov_wizard.module index 518333b810..643ee27eb5 100644 --- a/web/modules/custom/usagov_wizard/usagov_wizard.module +++ b/web/modules/custom/usagov_wizard/usagov_wizard.module @@ -430,7 +430,7 @@ function usagov_wizard_preprocess_views_view_field__wizard_options(&$variables) } /** - * Implements hook_preprocess_HOOK(). + * Implements hook_preprocess_views_view_unformatted(). */ function usagov_wizard_preprocess_views_view_unformatted(&$variables) { $taxonomy_term = \Drupal::routeMatch()->getParameter('taxonomy_term'); diff --git a/web/themes/custom/usagov/templates/wizard_2024/html--taxonomy--term--wizard.html.twig b/web/themes/custom/usagov/templates/wizard_2024/html--taxonomy--term--wizard.html.twig index f7a892a2b1..c9419af7f9 100644 --- a/web/themes/custom/usagov/templates/wizard_2024/html--taxonomy--term--wizard.html.twig +++ b/web/themes/custom/usagov/templates/wizard_2024/html--taxonomy--term--wizard.html.twig @@ -15,103 +15,3 @@ {{ header_html|raw }} {% endif %} {% endblock %} - -{% block taxonomy_data_old %} - {% set titleCounter = 4 %} - {% set urlCounter = 4 %} - {% if start_page %} - {% set page_type = 'wizard-start' %} - {% set titleCounter = 2 %} - {% set urlCounter = 2 %} - {% elseif not has_child %} - {% set page_type = 'wizard-result' %} - {% else %} - {% set page_type = 'wizard-question' %} - {% endif %} - -{% endblock %}