Problem/Motivation

Empty breadcrumbs are rendered by the path based breadcrumb builder if the path has an empty title.

Steps to reproduce

Visit /batch/1

Proposed resolution

I think if a route returns an empty title it should not be added to the $links array in \Drupal\system\PathBasedBreadcrumbBuilder::build

Remaining tasks

Review

User interface changes

Empty breadcrumbs are no longer rendered.

/batch/1 before patch:
batch page before patch

/batch/1 after patch:
batch page after patch

API changes

Data model changes

Release notes snippet

Issue fork drupal-3344200

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

alexpott created an issue. See original summary.

alexpott’s picture

          $title = $this->titleResolver->getTitle($route_request, $route_match->getRouteObject());
          if (!isset($title)) {
            // Fallback to using the raw path component as the title if the
            // route is missing a _title or _title_callback attribute.
            $title = str_replace(['-', '_'], ' ', Unicode::ucfirst(end($path_elements)));
          }

Well this fallback is suppose to prevent empty titles but it's not.

alexpott’s picture

Status: Active » Needs review
StatusFileSize
new4.62 KB

Something like this would make the fallback work as expected.

smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs Review Queue Initiative

Locally ran testEmptyStringStaticTitle without the fix and it passes. Shouldn't this fail? If not would this be out of scope as it's not testing the bug?

alexpott’s picture

Status: Needs work » Needs review
StatusFileSize
new820 bytes
new3.15 KB
new4.92 KB

@smustgrave yeah that's because of how the code currently works. The test that fails is \Drupal\Tests\Core\Controller\TitleResolverTest::testDynamicTitle(). I added \Drupal\Tests\Core\Controller\TitleResolverTest::testEmptyStringStaticTitle() to show that currently the way dynamic and static title behave is inconsistent.

I've added yet another test to prove consistent behaviour - testing a route with absolutely no title - ie. no callback or _title.

alexpott’s picture

So if we do steps to reproduce on #3220437: [PP-1] Empty breadcrumb at node/add and node/add/{content_type} when frontpage view is enabled you get a breadcrumb of Home >> Node - it creates a title of Node based on the path. So this code...

          $title = $this->titleResolver->getTitle($route_request, $route_match->getRouteObject());
          if (!isset($title)) {
            // Fallback to using the raw path component as the title if the
            // route is missing a _title or _title_callback attribute.
            $title = str_replace(['-', '_'], ' ', Unicode::ucfirst(end($path_elements)));
          }

Is working as expected. BUT for me this code is pretty interesting if you consider multilingual sites. Paths in Drupal especially the admin interface will be English so I'm not convinced that this behaviour is correct. Another option would be to omit pages without titles from breadcrumbs.

The last submitted patch, 5: 3344200-5.test-only.patch, failed testing. View results

andypost’s picture

+++ b/core/lib/Drupal/Core/Controller/TitleResolver.php
@@ -76,6 +77,12 @@ public function getTitle(Request $request, Route $route) {
       // Fall back to a static string from the route.
       $route_title = $this->t($title, $args, $options);
...
+    if ($route_title === '' || ($route_title instanceof TranslatableMarkup && $route_title->getUntranslatedString() === '')) {
+      return NULL;

Maybe better to check for instance of before converting value to string value object?

alexpott’s picture

@andypost === does not convert anything - see https://3v4l.org/cWOn9

andypost’s picture

Thanks 👍 then only #6 a question, maybe follow-up to discuss what to do with empty title for breadcrumbs, for example instead of omitting we can display elepsis character to make it navigable

aaronmchale’s picture

Status: Needs review » Reviewed & tested by the community

Yeah I also find it interesting that "Batch" and "Node" start appearing with the relevant patch(s) applied. Agree with comment #6, and I guess we're only now seeing the true result of what that part of the Breadcrumb Builder ends up outputting, which is technically better than a weird looking empty breadcrumb part.

Technically the patch works as expected, so happy to RTBC this.

But as noted we should revisit that part of the Breadcrumb Builder to determine if that is actually the behaviour we want.

alexpott’s picture

Discussed with @catch and @larowlan - there is consensus that rather than using the path we should skip the link - like we do for items that a user does not have access too.

alexpott’s picture

Status: Reviewed & tested by the community » Needs work

For #11

alexpott’s picture

Status: Needs work » Needs review
Issue tags: +Needs change record
StatusFileSize
new6.18 KB
new11.1 KB

Here's an implementation that skips paths with no titles. We'll need a change to update this.

smustgrave’s picture

Status: Needs review » Needs work
StatusFileSize
new215.42 KB

working

Here's a screenshot of visiting /batch/11

Breadcrumb is not rendering empty crumb, example Home > [blank]

Moving to NW for the change record.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

vipul tulse’s picture

StatusFileSize
new11.1 KB

Rerolled patch for Drupal 10.3.0

acbramley made their first commit to this issue’s fork.

acbramley’s picture

Issue summary: View changes
Issue tags: -Needs change record
StatusFileSize
new36.34 KB
new36.24 KB
acbramley’s picture

Status: Needs work » Needs review
smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Last step for a CR appears complete. Believe this one is good to go

needs-review-queue-bot’s picture

Status: Reviewed & tested by the community » Needs work
StatusFileSize
new2.92 KB

The Needs Review Queue Bot tested this issue. It fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".

This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.

Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.

acbramley’s picture

Status: Needs work » Reviewed & tested by the community

  • catch committed a247b77a on 11.x
    Issue #3344200 by acbramley, alexpott, smustgrave, aaronmchale:...
catch’s picture

Status: Reviewed & tested by the community » Fixed

Committed/pushed to 11.x, thanks!

I think this is more of a straight bugfix than a behaviour change, but given the CR etc. playing it safe and not backporting it.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.