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 2 commits into
base: dev
Choose a base branch
from

Conversation

DaleMHFrey
Copy link
Contributor

@DaleMHFrey DaleMHFrey commented Jan 15, 2025

https://cm-jira.usa.gov/browse/USAGOV-2177

Description

For State & Agency pages, relocated breadcrumb logic to the template file, and made it look up the title of the node's parents.

Type of Changes

  • New Feature
  • Bugfix
  • Frontend (Twig, Sass, JS)
  • Drupal Config (requires "drush cim")
  • New Modules (requires rebuild)
  • Documentation
  • Infrastructure
  • Other

Testing Instructions

Go to a state page like /states/alaska or an agency page like /agencies/u-s-abilityone-commission - compare what you see on dev vs Prod. As said in the ticket:
Good: The U.S. and its government
Bad: About the U.S. and its government

Post PR Approval Instructions

Follow these steps as soon as you merge the new changes.

  1. Go to the USAGov Circle CI project.
  2. Find the commit of this pull request.
  3. Build and deploy the changes.
  4. Update the Jira ticket by changing the ticket status to Review in Test and add a comment. State whether the change is already visible on cms-dev.usa.gov and beta-dev.usa.gov, or if the deployment is still in process.

@omerida omerida self-requested a review January 15, 2025 21:48
@@ -444,3 +444,65 @@ function usagov_theme_suggestions_alter(array &$suggestions, array $variables, $
}
}
}

function usagov_preprocess_breadcrumb(&$variables) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if a theme preprocess function is the right spot or if we should use hook_system_breadcrumb_alter(). The homepage titles are also hard coded in the taxonomy datalayer and published pages report. If they could all get the data from one place by fetching the breadcrumb for a node, that would be ideal Does this function affect the published-pages.csv file and datalayer JSON on the page?


function usagov_preprocess_breadcrumb(&$variables) {

$database = \Drupal::database();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's avoid direct SQL queries, there are potential security issues plus we have to then be aware of potential schema changes in these tables when upgrading tables. This site is handy for working with Drupals entity queries: https://www.drupalatyourfingertips.com/queries

You can find similar lookups in the datalayer code and usagov menu block modules. I'll add examples below.


$database = \Drupal::database();

if (strpos($_SERVER['REQUEST_URI'], 'agencies/') !== FALSE) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to test if the current page is the federal agency page, see TaxonomyDatalayerBuilder::isFederalDirectoryIndex()

// get current node
$node = \Drupal::routeMatch()->getParameter('node');
// check if agency index is english page
if ($this->node->toUrl()->toString() === TaxonomyDatalayerBuilder::AGENCY_INDEX_URL_EN) {

Similar test for Spanish page, uses a different constant.

Comment on lines +455 to +457
$nid = $database->query("SELECT path FROM path_alias WHERE alias LIKE '%about-the-us%'")->fetchField();
$nid = str_replace('/node/', '', $nid);
$title = $database->query("SELECT title FROM node_field_data WHERE nid = {$nid}")->fetchField();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lookup the internal based on alias, this should return '/node/NID. Extract nid from there, then use Node::load($nid)` to get the Node entity and get the title from it.

$path = \Drupal::service('path_alias.manager')->getPathByAlias($url);

$nid = str_replace('/node/', '', $nid);
$title = $database->query("SELECT title FROM node_field_data WHERE nid = {$nid}")->fetchField();
$variables['breadcrumb'][] = ['text' => $title, 'url' => '/about-the-us'];
$nid = $database->query("SELECT path FROM path_alias WHERE alias LIKE '%agency-index%'")->fetchField();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above but for Spanish, you might have to include the langcode as the second parameter to getPathByAlias()

$nid = str_replace('/node/', '', $nid);
$title = $database->query("SELECT title FROM node_field_data WHERE nid = {$nid}")->fetchField();
$variables['breadcrumb'][] = ['text' => $title, 'url' => '/about-the-us'];
$nid = $database->query("SELECT path FROM path_alias WHERE alias LIKE '%state-governments%'")->fetchField();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use path alias manager to get the node and then its title

if (strpos($_SERVER['REQUEST_URI'], 'agencias/') !== FALSE) {
$variables['breadcrumb'] = [];
$variables['breadcrumb'][] = ['text' => 'Home', 'url' => 'https://www.usa.gov/'];
$nid = $database->query("SELECT path FROM path_alias WHERE alias LIKE '%acerca-de-estados-unidos%'")->fetchField();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use path alias manager to get the node and then its title

$variables['breadcrumb'][] = ['text' => Drupal::service('title_resolver')->getTitle(\Drupal::request(), \Drupal::routeMatch()->getRouteObject())];
}

if (strpos($_SERVER['REQUEST_URI'], 'estados/') !== FALSE) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above, get the node for the current match request to test the full URL path. This will match any URL that has estados/ in it

if (strpos($_SERVER['REQUEST_URI'], 'estados/') !== FALSE) {
$variables['breadcrumb'] = [];
$variables['breadcrumb'][] = ['text' => 'Home', 'url' => 'https://www.usa.gov/'];
$nid = $database->query("SELECT path FROM path_alias WHERE alias LIKE '%acerca-de-estados-unidos%'")->fetchField();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use path alias manager to get the node and then its title

$nid = str_replace('/node/', '', $nid);
$title = $database->query("SELECT title FROM node_field_data WHERE nid = {$nid}")->fetchField();
$variables['breadcrumb'][] = ['text' => $title, 'url' => '/acerca-de-estados-unidos'];
$nid = $database->query("SELECT path FROM path_alias WHERE alias LIKE '%gobiernos-estatales%'")->fetchField();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use path alias manager to get the node and then its title

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants