Skip to content

Fixes #5634 Allow search results to use canonical URL #5658

Merged
bberndt-uaz merged 8 commits into
mainfrom
issue/5634
May 29, 2026
Merged

Fixes #5634 Allow search results to use canonical URL #5658
bberndt-uaz merged 8 commits into
mainfrom
issue/5634

Conversation

@tadean
Copy link
Copy Markdown
Contributor

@tadean tadean commented May 28, 2026

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 that field_az_link exists and is populated before reading it, and falling back to the node URL when it is not available.

Release notes

The built-in Drupal search form will now use a node's canonical URL where available.

Related issues

#5634

How to test

  • enable az_demo
  • run cron
  • visit /admin/config/search/pages
  • run cron again, until the index is 100%
  • visit /search/node
  • search for jet stream
  • verify that the demo content with a canonical url points to the canonical location, and that others point to the node as normal
  • create additional content if desired, with or without a canonical url
  • run cron again
  • verify that search results use the canonical url correctly
  • verify results still work for node types/content without a populated field_az_link value (fallback URL is used)

Types of changes

Arizona Quickstart (install profile, custom modules, custom theme)

  • Patch release changes
    • Bug fix
    • Accessibility, performance, or security improvement
    • Critical institutional link or brand change
    • Adding experimental module
    • Update experimental module
  • Minor release changes
    • New feature
    • Breaking or visual change to existing behavior
    • Upgrade experimental module to stable
    • Enable existing module by default or database update
    • Non-critical brand change
    • New internal API or API improvement with backwards compatibility
    • Risky or disruptive cleanup to comply with coding standards
    • High-risk or disruptive change (requires upgrade path, risks regression, etc.)
  • Other or unknown
    • Other or unknown

Drupal core

  • Patch release changes
    • Security update
    • Patch level release (non-security bug-fix release)
    • Patch removal that's no longer necessary
  • Minor release changes
    • Major or minor level update
  • Other or unknown
    • Other or unknown

Drupal contrib projects

  • Patch release changes
    • Security update
    • Patch or minor level update
    • Add new module
    • Patch removal that's no longer necessary
  • Minor release changes
    • Major level update
  • Other or unknown
    • Other or unknown

Checklist

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.
  • My change requires release notes.

@tadean tadean self-assigned this May 28, 2026
@tadean tadean requested review from a team as code owners May 28, 2026 23:04
@tadean tadean added the backport Changes that have been backported from another branch label May 28, 2026
@tadean tadean added patch release Issues to be included in the next patch release usability Improvements to usability of Quickstart components user experience Improvements to user experience release notes backport-2.x Changes to be back-ported to the 2.x development branch labels May 28, 2026
@tadean tadean moved this from Todo to Needs review in 3.3.7 bug-fix patch release May 28, 2026
@tadean tadean moved this to Needs review in 3.4.0 minor release May 28, 2026
@tadean tadean moved this from Todo to Needs review in 3.4.1 bug-fix patch release May 29, 2026
@joeparsons joeparsons added the bug Something isn't working label May 29, 2026
@joeparsons joeparsons requested a review from Copilot May 29, 2026 16:56
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

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() in az_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.

Comment on lines +604 to +617
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) {

}
}
}
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This should be fixed in az_core_canonical_url(), where that token is evaluated, not here.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@copilot See @tadean's findings in #5658 (comment)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

@tadean
Copy link
Copy Markdown
Contributor Author

tadean commented May 29, 2026

Copilot previously said the following:

This preprocess runs for every node search result, but the [node:az-canonical-url] token callback reads field_az_link directly. Search results for node bundles that do not have that field can trigger an unknown-field exception instead of falling back to the normal search URL. Limit the override to nodes that actually have a populated canonical link field, since the default search URL is already correct otherwise.

This does not appear to occur because az_core_canonical_url() (which provides the value of this token) uses the magic metthod call if (!empty($node->field_az_link[0]->uri)) {

The php empty() language construct calls the __isset method if the object defines it. Given the functionality of ContentEntityBase::__isset() it seems like this should not occur for undefined fields.

joeparsons
joeparsons previously approved these changes May 29, 2026
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 2 comments.

Comment thread modules/custom/az_core/az_core.module Outdated
Comment thread modules/custom/az_core/az_core.module
Copilot AI changed the title Fixes #5634 Allow search results to use canonical url Allow search results to use canonical URL safely May 29, 2026
tadean and others added 2 commits May 29, 2026 14:07
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>
@tadean tadean changed the title Allow search results to use canonical URL safely Allow search results to use canonical URL May 29, 2026
@bberndt-uaz bberndt-uaz removed the backport Changes that have been backported from another branch label May 29, 2026
@github-project-automation github-project-automation Bot moved this from Needs review to Ready to merge in 3.4.1 bug-fix patch release May 29, 2026
@github-project-automation github-project-automation Bot moved this from Needs review to Ready to merge in 3.3.7 bug-fix patch release May 29, 2026
@joeparsons joeparsons changed the title Allow search results to use canonical URL Fixes #5634 Allow search results to use canonical URL May 29, 2026
@bberndt-uaz bberndt-uaz merged commit 0b15c63 into main May 29, 2026
33 checks passed
@bberndt-uaz bberndt-uaz deleted the issue/5634 branch May 29, 2026 22:20
@github-project-automation github-project-automation Bot moved this from Ready to merge to Done in 3.4.1 bug-fix patch release May 29, 2026
@github-project-automation github-project-automation Bot moved this from Ready to merge to Done in 3.3.7 bug-fix patch release May 29, 2026
bberndt-uaz pushed a commit that referenced this pull request May 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport-2.x Changes to be back-ported to the 2.x development branch bug Something isn't working patch release Issues to be included in the next patch release release notes usability Improvements to usability of Quickstart components user experience Improvements to user experience

Projects

Development

Successfully merging this pull request may close these issues.

5 participants