diff --git a/web/modules/custom/usa_twig_vars/src/TaxonomyDatalayerBuilder.php b/web/modules/custom/usa_twig_vars/src/TaxonomyDatalayerBuilder.php index 96ff990c2d..cce076f5f1 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; /** @@ -11,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"; @@ -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..8210c7f883 --- /dev/null +++ b/web/modules/custom/usagov_wizard/src/EventSubscriber/DatalayerAlterSubscriber.php @@ -0,0 +1,137 @@ + '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'; + } + + // 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(); + $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; + + $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)]; + } + + 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']; + } + } + + // the rest comes from the parents of this term + foreach ($parents as $parentTerm) { + $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(); + } + + $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); + } + +} diff --git a/web/modules/custom/usagov_wizard/usagov_wizard.module b/web/modules/custom/usagov_wizard/usagov_wizard.module index 355f14d574..643ee27eb5 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,32 +380,40 @@ 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 []; +} + /** * Implements hook_preprocess_HOOK(). */ @@ -421,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/modules/custom/usagov_wizard/usagov_wizard.services.yml b/web/modules/custom/usagov_wizard/usagov_wizard.services.yml index 5776fca5ee..7e1929dde3 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: ['@usagov_wizard.menu_checker', '@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..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 %} - {% 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 %}