Fixes #5634 Allow search results to use canonical URL #5658
Conversation
There was a problem hiding this comment.
Pull request overview
This PR updates stock Drupal node search results so they can link to a node’s configured canonical URL, addressing issue #5634 for content that should point visitors to an external or curated canonical destination.
Changes:
- Adds
hook_preprocess_search_result()inaz_core. - Replaces the search result URL with the
[node:az-canonical-url]token value when available. - Converts canonical URI values into rendered URL strings for the search result template.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| if ($node && ($node instanceof NodeInterface)) { | ||
| // Get the token value of the canonical url. | ||
| $token = \Drupal::token()->replace('[node:az-canonical-url]', ['node' => $node]); | ||
| if (!empty($token)) { | ||
| try { | ||
| // Turn routes and internal URIs into urls. | ||
| $url = Url::fromUri($token); | ||
| $variables['url'] = $url->toString(); | ||
| } | ||
| catch (\InvalidArgumentException $e) { | ||
|
|
||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
This should be fixed in az_core_canonical_url(), where that token is evaluated, not here.
There was a problem hiding this comment.
Updated in 58cd6c3. I moved the defensive handling into az_core_canonical_url() by checking hasField('field_az_link') and ensuring the field is not empty before reading its URI, so the token callback safely falls back to the node URL when the field is unavailable.
|
Copilot previously said the following:
This does not appear to occur because The php |
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Description
This PR allows the stock Drupal search to use the canonical URL for nodes.
It also hardens canonical URL token evaluation in
az_core_canonical_url()by checking thatfield_az_linkexists and is populated before reading it, and falling back to the node URL when it is not available.Release notes
Related issues
#5634
How to test
/admin/config/search/pages/search/nodejet streamfield_az_linkvalue (fallback URL is used)Types of changes
Arizona Quickstart (install profile, custom modules, custom theme)
Drupal core
Drupal contrib projects
Checklist