Paths to replace with custom breadcrumbs is broken for <title>

Created on 25 March 2022, over 2 years ago
Updated 11 July 2024, 5 months ago

Problem/Motivation

The "Paths to replace with custom breadcrumbs" help says To use the current page title as a title component, use <title>; however that does not work -- <title> crumbs are empty.

The first issue is that unless you select "Use the real page title when available" the code path doesn't even try to generate the title. This is true even if you have selected "Use menu title when available" and "Use page title as fallback for menu title", which I think should work here. The code in question is restricted thus:

if ($this->config->get(EasyBreadcrumbConstants::TITLE_FROM_PAGE_WHEN_AVAILABLE)) {
  $title = $this->normalizeText($this->getTitleString($route_request, $route_match, $replacedTitles));
}

The second issue is that even when it does try to generate a title, Drupal\easy_breadcrumb\TitleResolver::getTitle() triggers an exception:

>>> InvalidArgumentException with message 'The internal path component 'some/trimmed/path' is invalid. Its path component must have a leading slash, e.g. internal:/foo.'

Because of how the path was trimmed earlier in the build() process.

Steps to reproduce

Configure a regexp replacement path with a title component, something like this:

regex!/category/[0-9]+/.+ :: Category | /category :: <title>

Proposed resolution

Early on, build() does this:

$path = trim($this->context->getPathInfo(), '/');

Either change that to this:

$path = rtrim($this->context->getPathInfo(), '/');

or else modify Drupal\easy_breadcrumb\TitleResolver::getTitle() so that it doesn't immediately fail when it winds up with a path which doesn't start with a slash, when it calls this:

$url = Url::fromUri("internal:" . $request->getRequestUri());

And gets an exception:

>>> InvalidArgumentException with message 'The internal path component 'some/trimmed/path' is invalid. Its path component must have a leading slash, e.g. internal:/foo.'

Perhaps changing it to this:

$url = Url::fromUri("internal:/" . $request->getRequestUri());

or if it's still possible for a leading slash to get here, then maybe:

$url = Url::fromUri("internal:/" . ltrim($request->getRequestUri(), '/'));

(Experimentally it's not safe to allow internal:// to occur, as the resulting URL will be wrong.)

I think working around the bug in the TitleResolver would have the smallest impact, but it's not clear to me that the path should ever have had the leading slash trimmed in the first place -- so far as I've seen, Drupal 8+ deals exclusively in leading slash paths -- so the change to the trimming may be the better fix for the sake of consistency and predictability, even if it requires some additional changes?

Remaining tasks

Fix bug with trimmed path.

Make it so titles are also generated when "Use menu title when available" is selected.

User interface changes

n/a.

API changes

?

Data model changes

?

🐛 Bug report
Status

Fixed

Version

2.0

Component

Code

Created by

🇳🇿New Zealand jweowu

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024