Problem/Motivation

During DrupalEasy Office Hours, we reviewed this module and for the most part, it is working well!

One place where it fell short is the fact that tokens do not seem to be supported in the "Link (optional)" field when configuring a custom breadcrumb.

Steps to reproduce

Consider the use case where you want to have a breadcrumb like: Home > Articles > 2023 > Article title.

The "2023" link would lead to a view with a contextual filter - the link being "/articles/2023". This requires the use of a token in the "Link (optional)" field.

Create a new breadcrumb with a segment that has Label of [node:created:html_year] and a Link of /articles/[node:created:html_year] After saving, and going to an article node, note that the token in the "Link (optional)" field was not processed to the 4-digit year.

Proposed resolution

Enable tokens on "Link (optional)" fields.

Remaining tasks

Do the things.

User interface changes

More tokens!

thank you!
-mike

Comments

ultimike created an issue. See original summary.

ultimike’s picture

Looking at the code (SdBreadcrumbBuilder.php ), we're pretty sure you can just add the following around line 283:

      // Process tokens in link_path
      if ($token_service && $link_path) {
        $link_path = $token_service->replace($link_path, ['node' => $node], ['clear' => TRUE]);
        $link_path = UrlHelper::encodePath($link_path);
      }

We haven't tested this - we came to this conclusion only by looking at the code, so please test well.

-mike

sushant-d’s picture

Thanks Mike — great catch! Your suggestion was spot-on and pointed me in the right direction. I adopted the simplified approach with a few additional safeguards:

  • () and () are excluded from token processing since UrlHelper::encodePath() would encode the angle brackets and break their detection downstream.
  • Html::decodeEntities() runs before encodePath() because the token service returns HTML-encoded strings (e.g., &) that need decoding first.
  • External URLs (https://...) skip encodePath() entirely.

This is now implemented and released in 1.1.5. Thanks for the contribution!

sushant-d’s picture

Status: Active » Fixed

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

sushant-d’s picture

Status: Fixed » Closed (fixed)