Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

USAGOV-2177: USAGOV-2177 breadcrumbs for directory pages #2173

Open
wants to merge 14 commits into
base: dev
Choose a base branch
from
130 changes: 103 additions & 27 deletions web/modules/custom/usa_twig_vars/src/TaxonomyDatalayerBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Drupal\Core\Breadcrumb\ChainBreadcrumbBuilderInterface;
use Drupal\Core\Entity\EntityMalformedException;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\node\Entity\Node;
use Drupal\node\NodeInterface;

/**
Expand Down Expand Up @@ -39,18 +40,6 @@ class TaxonomyDatalayerBuilder {
public const HOME_TITLE_ES = "Página principal";
public const HOME_URL_ES = "/es/";

private const string ABOUT_GOVT_EN = "About the U.S. and its government";
private const string ABOUT_URL_EN = "/about-the-us";

private const string ABOUT_GOVT_ES = "Acerca de EE. UU. y su Gobierno";
private const string ABOUT_URL_ES = "/es/acerca-de-estados-unidos";

private const string AGENCY_INDEX_URL_EN = '/agency-index';
private const string AGENCY_INDEX_URL_ES = '/es/indice-agencias';

private const string STATE_INDEX_URL_EN = '/state-governments';
private const string STATE_INDEX_URL_ES = '/es/gobiernos-estatales';

/**
* Language code for entity.
*
Expand Down Expand Up @@ -78,6 +67,93 @@ public function __construct(
$this->isFront = $isFront ? 'homepage' : 'not_homepage';
}

public static function aboutGovtEn() {
static $ret = FALSE;
if ($ret === FALSE) {
$sysPath = \Drupal::service('path_alias.manager')->getPathByAlias(self::aboutUrlEn());
$nid = str_replace('/node/', '', $sysPath);
$ret = Node::load($nid)->getTitle();
}
return $ret;
}

public static function aboutUrlEn() {
return "/about-the-us";
}

public static function aboutGovtEs() {
static $ret = FALSE;
if ($ret === FALSE) {
$sysPath = \Drupal::service('path_alias.manager')->getPathByAlias(self::aboutUrlEs());
$nid = str_replace('/node/', '', $sysPath);
$node = Node::load($nid);
if (!empty($node)) {
$ret = $node->getTitle();
}
}
return $ret;
}

public static function aboutUrlEs() {
return "/acerca-de-estados-unidos";
}

public static function agencyIndexEn() {
static $ret = FALSE;
if ($ret === FALSE) {
$sysPath = \Drupal::service('path_alias.manager')->getPathByAlias(self::agencyIndexUrlEn());
$nid = str_replace('/node/', '', $sysPath);
$ret = Node::load($nid)->getTitle();
}
return $ret;
}

public static function agencyIndexUrlEn() {
return '/agency-index';
}

public static function agencyIndexEs() {
static $ret = FALSE;
if ($ret === FALSE) {
$sysPath = \Drupal::service('path_alias.manager')->getPathByAlias(self::agencyIndexUrlEs());
$nid = str_replace('/node/', '', $sysPath);
$ret = Node::load($nid)->getTitle();
}
return $ret;
}

public static function agencyIndexUrlEs() {
return '/es/indice-agencias';
}

public static function stateIndexEn() {
static $ret = FALSE;
if ($ret === FALSE) {
$sysPath = \Drupal::service('path_alias.manager')->getPathByAlias(self::stateIndexUrlEn());
$nid = str_replace('/node/', '', $sysPath);
$ret = Node::load($nid)->getTitle();
}
return $ret;
}

public static function stateIndexUrlEn() {
return '/state-governments';
}

public static function stateIndexEs() {
static $ret = FALSE;
if ($ret === FALSE) {
$sysPath = \Drupal::service('path_alias.manager')->getPathByAlias(self::stateIndexUrlEs());
$nid = str_replace('/node/', '', $sysPath);
$ret = Node::load($nid)->getTitle();
}
return $ret;
}

public static function stateIndexUrlEs() {
return '/es/gobiernos-estatales';
}

/**
* Builds the datalayer array.
*
Expand Down Expand Up @@ -237,22 +313,22 @@ public function getFederalAgency(): array {
switch ($this->langcode) {
case 'en':
$taxonomy["Taxonomy_Text_1"] = self::HOME_TITLE_EN;
$taxonomy["Taxonomy_Text_2"] = self::ABOUT_GOVT_EN;
$taxonomy["Taxonomy_Text_2"] = self::aboutGovtEn();
$taxonomy["Taxonomy_Text_3"] = "A-Z index of U.S. government departments and agencies";

$taxonomy["Taxonomy_URL_1"] = self::HOME_URL_EN;
$taxonomy["Taxonomy_URL_2"] = self::ABOUT_URL_EN;
$taxonomy["Taxonomy_URL_3"] = self::AGENCY_INDEX_URL_EN;
$taxonomy["Taxonomy_URL_2"] = self::aboutUrlEn();
$taxonomy["Taxonomy_URL_3"] = self::agencyIndexUrlEn();
break;

case 'es':
$taxonomy["Taxonomy_Text_1"] = self::HOME_TITLE_ES;
$taxonomy["Taxonomy_Text_2"] = self::ABOUT_GOVT_ES;
$taxonomy["Taxonomy_Text_2"] = self::aboutGovtEs();
$taxonomy["Taxonomy_Text_3"] = "Agencias federales";

$taxonomy["Taxonomy_URL_1"] = self::HOME_URL_ES;
$taxonomy["Taxonomy_URL_2"] = self::ABOUT_URL_ES;
$taxonomy["Taxonomy_URL_3"] = self::AGENCY_INDEX_URL_ES;
$taxonomy["Taxonomy_URL_2"] = self::aboutUrlEs();
$taxonomy["Taxonomy_URL_3"] = self::agencyIndexUrlEs();
}

$agencyName = htmlspecialchars($this->node->getTitle(), ENT_QUOTES, 'UTF-8');
Expand Down Expand Up @@ -280,12 +356,12 @@ public function getStateDirectory(): array {
case 'en':
$taxonomy["Taxonomy_Text_1"] = self::HOME_TITLE_EN;

$taxonomy["Taxonomy_Text_2"] = self::ABOUT_GOVT_EN;
$taxonomy["Taxonomy_Text_2"] = self::aboutGovtEn();
$taxonomy["Taxonomy_Text_3"] = "State governments";

$taxonomy["Taxonomy_URL_1"] = self::HOME_URL_EN;
$taxonomy["Taxonomy_URL_2"] = self::ABOUT_URL_EN;
$taxonomy["Taxonomy_URL_3"] = self::STATE_INDEX_URL_EN;
$taxonomy["Taxonomy_URL_2"] = self::aboutUrlEn();
$taxonomy["Taxonomy_URL_3"] = self::stateIndexUrlEn();
break;

case 'es':
Expand All @@ -295,8 +371,8 @@ public function getStateDirectory(): array {
$taxonomy["Taxonomy_Text_3"] = "Gobiernos estatales";

$taxonomy["Taxonomy_URL_1"] = self::HOME_URL_ES;
$taxonomy["Taxonomy_URL_2"] = self::ABOUT_URL_ES;
$taxonomy["Taxonomy_URL_3"] = self::STATE_INDEX_URL_ES;
$taxonomy["Taxonomy_URL_2"] = self::aboutUrlEs();
$taxonomy["Taxonomy_URL_3"] = self::stateIndexUrlEs();
}

$agencyName = htmlspecialchars($this->node->getTitle(), ENT_QUOTES, 'UTF-8');
Expand Down Expand Up @@ -328,8 +404,8 @@ private function isFederalDirectoryIndex(): bool {
// These paths are standard pages but should be coded differently.
try {
switch ($this->node->toUrl()->toString()) {
case self::AGENCY_INDEX_URL_EN:
case self::AGENCY_INDEX_URL_ES:
case self::agencyIndexUrlEn():
case self::agencyIndexUrlEs():
return TRUE;
}
}
Expand All @@ -348,8 +424,8 @@ private function isStateDirectoryIndex(): bool {
// These paths are also standard pages but should be coded differently.
try {
switch ($this->node->toUrl()->toString()) {
case self::STATE_INDEX_URL_EN:
case self::STATE_INDEX_URL_ES:
case self::stateIndexUrlEn():
case self::stateIndexUrlEs():
return TRUE;
}
}
Expand Down
34 changes: 0 additions & 34 deletions web/themes/custom/usagov/templates/breadcrumb.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -7,40 +7,6 @@
* - breadcrumb: Breadcrumb trail items.
*/
#}
{% if path('<current>') matches '#\/agencies\/*#'%}
{% set breadcrumb = [
{ 'url': "/", 'text': 'Home' },
{ 'url': "/about-the-us", 'text': 'About the U.S. and its government' },
{ 'url': '/agency-index', 'text': drupal_field('title', 'node', 629) },
{ 'text': drupal_title() }
] %}
{% elseif path('<current>') matches '#\/states\/*#' %}
{% set breadcrumb = [
{ 'url': "/", 'text': 'Home' },
{ 'url': "/about-the-us", 'text': 'About the U.S. and its government' },
{ 'url': "/state-governments", 'text': 'State governments' },
{ 'text': drupal_title() }
] %}
omerida marked this conversation as resolved.
Show resolved Hide resolved
{% elseif path('<current>') matches '#\/es\/agencias\/*#' %}
{% set breadcrumb = [
{ 'url': "/es", 'text': 'Página principal' },
{ 'url': "/es/acerca-de-estados-unidos", 'text': 'Acerca de EE. UU. y su Gobierno' },
{ 'url': '/es/indice-agencias', 'text': drupal_field('title', 'node', 1696) },
{ 'text': drupal_title() }
] %}
{% elseif path('<current>') matches '#\/es\/estados\/*#' %}
{% set breadcrumb = [
{ 'url': "/es", 'text': 'Página principal' },
{ 'url': "/es/acerca-de-estados-unidos", 'text': 'Acerca de EE. UU. y su Gobierno' },
{ 'url': "/es/gobiernos-estatales", 'text': 'Gobiernos estatales' },
{ 'text': drupal_title() }
] %}
{% endif %}

{% if wizard_breadcrumb %}
{% set breadcrumb = wizard_breadcrumb %}
{% endif %}


{% if breadcrumb %}

Expand Down
66 changes: 66 additions & 0 deletions web/themes/custom/usagov/usagov.theme
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use Drupal\node\Entity\Node;
use Drupal\node\NodeInterface;
use Drupal\taxonomy\Entity\Term;
use Drupal\usa_twig_vars\TaxonomyDatalayerBuilder;

/**
* @file
Expand Down Expand Up @@ -465,3 +466,68 @@ function usagov_theme_suggestions_alter(array &$suggestions, array $variables, s
}
}
}

function usagov_preprocess_breadcrumb(&$variables) {
DaleMHFrey marked this conversation as resolved.
Show resolved Hide resolved

$node = \Drupal::routeMatch()->getParameter('node');
if (empty($node)) {
return;
}

if (str_starts_with($node->toUrl()->toString(), '/agencies/')) {
$variables['breadcrumb'] = [];
$variables['breadcrumb'][] = ['text' => 'Home', 'url' => '/'];
$variables['breadcrumb'][] = [
'text' => TaxonomyDatalayerBuilder::aboutGovtEn(),
'url' => TaxonomyDatalayerBuilder::aboutUrlEn()
];
$variables['breadcrumb'][] = [
'text' => TaxonomyDatalayerBuilder::agencyIndexEn(),
'url' => TaxonomyDatalayerBuilder::agencyIndexUrlEn()
];
$variables['breadcrumb'][] = ['text' => Drupal::service('title_resolver')->getTitle(\Drupal::request(), \Drupal::routeMatch()->getRouteObject())];
}

if (str_starts_with('/states/', $node->toUrl()->toString())) {
$variables['breadcrumb'] = [];
$variables['breadcrumb'][] = ['text' => 'Home', 'url' => '/'];
$variables['breadcrumb'][] = [
'text' => TaxonomyDatalayerBuilder::aboutGovtEn(),
'url' => TaxonomyDatalayerBuilder::aboutUrlEn()
];
$variables['breadcrumb'][] = [
'text' => TaxonomyDatalayerBuilder::stateIndexEn(),
'url' => TaxonomyDatalayerBuilder::stateIndexUrlEn()
];
$variables['breadcrumb'][] = ['text' => Drupal::service('title_resolver')->getTitle(\Drupal::request(), \Drupal::routeMatch()->getRouteObject())];
}

if (str_starts_with($node->toUrl()->toString(), '/es/agencias/')) {
$variables['breadcrumb'] = [];
$variables['breadcrumb'][] = ['text' => 'Home', 'url' => 'https://www.usa.gov/'];
$variables['breadcrumb'][] = [
'text' => TaxonomyDatalayerBuilder::aboutGovtEs(),
'url' => TaxonomyDatalayerBuilder::aboutUrlEs()
];
$variables['breadcrumb'][] = [
'text' => TaxonomyDatalayerBuilder::agencyIndexEs(),
'url' => TaxonomyDatalayerBuilder::agencyIndexUrlEs()
];
$variables['breadcrumb'][] = ['text' => Drupal::service('title_resolver')->getTitle(\Drupal::request(), \Drupal::routeMatch()->getRouteObject())];
}

if (str_starts_with($node->toUrl()->toString(), '/es/estados/')) {
$variables['breadcrumb'] = [];
$variables['breadcrumb'][] = ['text' => 'Home', 'url' => 'https://www.usa.gov/'];
$variables['breadcrumb'][] = [
'text' => TaxonomyDatalayerBuilder::aboutGovtEs(),
'url' => TaxonomyDatalayerBuilder::aboutUrlEs()
];
$variables['breadcrumb'][] = [
'text' => TaxonomyDatalayerBuilder::stateIndexEs(),
'url' => TaxonomyDatalayerBuilder::stateIndexUrlEs()
];
$variables['breadcrumb'][] = ['text' => Drupal::service('title_resolver')->getTitle(\Drupal::request(), \Drupal::routeMatch()->getRouteObject())];
}

}
Loading