-
Notifications
You must be signed in to change notification settings - Fork 10
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
base: dev
Are you sure you want to change the base?
Conversation
@@ -444,3 +444,65 @@ function usagov_theme_suggestions_alter(array &$suggestions, array $variables, $ | |||
} | |||
} | |||
} | |||
|
|||
function usagov_preprocess_breadcrumb(&$variables) { |
There was a problem hiding this comment.
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(); |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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.
$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(); |
There was a problem hiding this comment.
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(); |
There was a problem hiding this comment.
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(); |
There was a problem hiding this comment.
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(); |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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(); |
There was a problem hiding this comment.
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(); |
There was a problem hiding this comment.
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
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
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.
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.