Skip to content

Commit

Permalink
Merge branch 'dev' into USAGOV-2066-es-redirect
Browse files Browse the repository at this point in the history
  • Loading branch information
akf authored Nov 6, 2024
2 parents ba3a65b + 73e4c6e commit 70cbbc8
Show file tree
Hide file tree
Showing 10 changed files with 200 additions and 150 deletions.
4 changes: 0 additions & 4 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -768,7 +768,6 @@ workflows:
- dev
- stage
- prod
- pxbf-release-v0.8.0.beta.1
- build-and-push-container:
requires:
- approve-build-and-push-container
Expand All @@ -779,7 +778,6 @@ workflows:
- dev
- stage
- prod
- pxbf-release-v0.8.0.beta.1
- approve-dev-deployment:
type: approval
requires:
Expand All @@ -788,7 +786,6 @@ workflows:
branches:
only:
- dev
- pxbf-release-v0.8.0.beta.1
- approve-dr-deployment:
type: approval
requires:
Expand All @@ -804,7 +801,6 @@ workflows:
branches:
only:
- dev
- pxbf-release-v0.8.0.beta.1
- deploy-to-cloudgov-dr:
requires:
- approve-dr-deployment
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@

use Drupal\Core\Breadcrumb\BreadcrumbManager;
use Drupal\Core\Entity\EntityMalformedException;
use Drupal\Core\Routing\CurrentRouteMatch;
use Drupal\node\Entity\Node;

/**
* Builds a datalayer array we can serialize to JSON in our twig template.
*/
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";
Expand Down Expand Up @@ -47,6 +48,7 @@ class TaxonomyDatalayerBuilder {
private string $isFront;

public function __construct(
private CurrentRouteMatch $routeMatch,
private BreadcrumbManager $breadcrumbManager,
public Node $node,
bool $isFront,
Expand Down Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions web/modules/custom/usa_twig_vars/usa_twig_vars.module
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
<?php

namespace Drupal\usagov_wizard\EventSubscriber;

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,
) {}

/**
* {@inheritDoc}
*/
public static function getSubscribedEvents() {
return [
DatalayerAlterEvent::EVENT_NAME => '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);
}

}
55 changes: 32 additions & 23 deletions web/modules/custom/usagov_wizard/usagov_wizard.module
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

use Drupal\Core\Url;
use Drupal\taxonomy\Entity\Term;
use Drupal\usa_twig_vars\TaxonomyDatalayerBuilder;
use Drupal\usagov_wizard\MenuChecker;

/**
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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().
*/
Expand All @@ -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');
Expand Down
5 changes: 5 additions & 0 deletions web/modules/custom/usagov_wizard/usagov_wizard.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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' }
4 changes: 2 additions & 2 deletions web/themes/custom/usagov/scripts/reportAProblem.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ function fieldValidation() {
label.after(
'<span id="' +
errorId +
'" class="err-label usa-error" tabindex="0">' +
'" class="err-label usa-error" tabindex="-1">' +
error +
"</span>"
);
Expand Down Expand Up @@ -92,7 +92,7 @@ function fieldValidation() {
// If there is at least 1 error, focus the screen on the first error message.
if (!noErrors) {
var elem = document.querySelector(".err-label");
elem.focus();
// elem.focus(); // commented out so focus can goto the error box first
var viewportOffset = elem.getBoundingClientRect();
var top = viewportOffset.top;
if (top < 108) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@
<div class="usa-alert__body">
<h2 class="usa-alert__heading">Su información contiene errores</h2>
<li id="alert_error_00NU0000004z90C" class="usa-alert__text usa-error--alert">
<a href="#label_name"><span>Escriba su nombre</span></a>
<a href="#00NU0000004z90C"><span>Escriba su nombre</span></a>
</li>
<li id="alert_error_email" class="usa-alert__text usa-error--alert">
<a href="#label_email"><span>Escriba su email</span></a>
<a href="#email"><span>Escriba su email</span></a>
</li>
<li id="alert_error_description" class="usa-alert__text usa-error--alert">
<a href="#label_description"><span>Escriba la descripción</span></a>
<a href="#description"><span>Escriba la descripción</span></a>
</li>
<li id="alert_error_recaptcha_large" class="usa-alert__text usa-error--alert">
{# <li id="alert_error_recaptcha_large" class="usa-alert__text usa-error--alert">
<a href="#recaptcha-large-container"><span>Complete el reCaptcha</span></a>
</li>
<li id="alert_error_recaptcha_small" class="usa-alert__text usa-error--alert">
<a href="#recaptcha-small-container"><span>Fill out the reCaptcha</span></a>
</li>
</li> #}
</ul>
</div>
</div>
Expand Down Expand Up @@ -58,7 +58,7 @@
maxlength="255"
size="40"
required
data-error="Escriba su nombre"
data-error="Error: Escriba su nombre"
data-name="First Name"
/>
</p>
Expand All @@ -75,7 +75,7 @@
maxlength="80"
size="40"
required\
data-error="Escriba su email"
data-error="Error: Escriba su email"
data-name="Email Address"
/>
</p>
Expand All @@ -94,7 +94,7 @@
rows="10"
maxlength="1000"
required
data-error="Escriba la descripción"
data-error="Error: Escriba la descripción"
data-name="Description"
></textarea>
<text class="usa-description" id="report-issue-box-es">Por favor limite su comentario a 1000 caracteres.</text>
Expand Down
Loading

0 comments on commit 70cbbc8

Please sign in to comment.