Problem/Motivation

template_preprocess_breadcrumb() assumes all links are Link objects, while hook_system_breadcrumb_alter() tells you that you can use strings, and breadcrumb.html.twig supports it.

Proposed resolution

Check if the object is an object first.

Remaining tasks

Write a test.

User interface changes

API changes

Comments

tim.plunkett’s picture

Status: Active » Needs review
StatusFileSize
new1.29 KB
tim.plunkett’s picture

tim.plunkett’s picture

This still doesn't address the fact that breadcrumb.html.twig bypasses the LinkGenerator... Separate issue?

geerlingguy’s picture

+1, this should at least fix the problem and allow the API to work as it says it should.

tim.plunkett’s picture

StatusFileSize
new1.83 KB
new2.39 KB

This would fix that part too. Needs tests for both.

Crell’s picture

This seems like it's papering over the inconsistency rather than fixing it. Why not amend the preprocessor to take an array of link objects only instead of the mixed approach, which creates inconsistency and complexity?

star-szr’s picture

Also, now that we have a link generator available from Twig, this seems pretty related:

#2342745: Allow Twig link function to pass in HTML attributes

jhedstrom’s picture

Status: Needs review » Needs work

Still needs tests, and I don't think #6 has been addressed.

kgoel’s picture

Status: Needs work » Needs review
StatusFileSize
new1.74 KB

Reroll, working on #6 and tests

Crell’s picture

Status: Needs review » Needs work

I'd rather we fix the inconsistency rather than working around it.

dawehner’s picture

template_preprocess_breadcrumb() assumes all links are Link objects, while hook_system_breadcrumb_alter() tells you that you can use strings, and breadcrumb.html.twig supports it.

Well, I think the alter documentation should be adapted, value objects are a good thing to use here.

dawehner’s picture

They are especially helpful as we potentially could cary along the cacheability metadata.

dawehner’s picture

To be clear, there are usecases to add for example the title of the current page onto the breadcrumb. This is something just done on the theme level, its no logic involved in that,
so a preprocess function should be able to add a string there. This means the template doesn't change.

fgm’s picture

So in short, this means the template_preprocess_breadcrumb() should look like this .

function template_preprocess_breadcrumb(&$variables) {
  $variables['breadcrumb'] = array();
  /** @var \Drupal\Core\Link $link */
  foreach ($variables['links'] as $key => $link) {
    $is_object = $link instanceof Link; 
    $variables['breadcrumb'][$key] = [
      'text' => $is_object ? $link->getText() : $link, 
      'url' => $is_object ? $link->getUrl()->toString() : NULL,
    ];
  }
}
kgoel’s picture

Status: Needs work » Needs review
StatusFileSize
new2.86 KB
new7.2 KB

Status: Needs review » Needs work

The last submitted patch, 15: 2345801-15.patch, failed testing.

fgm’s picture

Status: Needs work » Needs review
StatusFileSize
new5.75 KB

It could be like this, maybe : the menu_test code generates a breadcrumb with both link and plain strings, which are preprocessed normally.

Status: Needs review » Needs work

The last submitted patch, 17: 2345801-breadcrumb_string-17.patch, failed testing.

kgoel’s picture

Status: Needs work » Needs review
StatusFileSize
new2.72 KB
new1.61 KB

Status: Needs review » Needs work

The last submitted patch, 19: 2345801-19.patch, failed testing.

Crell’s picture

Minor nit:

+++ b/core/modules/system/templates/breadcrumb.html.twig
@@ -17,7 +17,7 @@
-        {% else %}
+        {%  else %}

An extra space crept in here.

dawehner’s picture

Status: Needs work » Needs review
Issue tags: -Needs tests
StatusFileSize
new704 bytes

hook_system_breadcrumb_alter() now also deals with link objects only.

Let's just fix the docs here.

berdir’s picture

Status: Needs review » Needs work

Fine, but then we should fix them properly, that makes no sense, now you're doing createFromRoute() with the return value from Drupal::l() :)

berdir’s picture

For the record, having a breadcrumb that is *not* a link is a perfectly valid use case, e.g. if you want to current page also be shown in the breadcrumb. I did that now with the route: Link::createFromRoute($title, '<none>');

joelpittet’s picture

I agree and have a use case in d7 where my client had a mega menu and the headings need to be in the menu aka <nolink> from the special menu items project.

Home link > section title > sub section link > current page title

dawehner’s picture

For the record, having a breadcrumb that is *not* a link is a perfectly valid use case, e.g. if you want to current page also be shown in the breadcrumb. I did that now with the route: Link::createFromRoute($title, '');

When we talked about that on some other issue we came to the conclusion that no link is part of the template level, not part of the breadcrumb level for itself.
So its a different domain where you need just the current page title, which can be implemented purely on the theme level.

mr.baileys’s picture

Status: Needs work » Needs review
StatusFileSize
new693 bytes

Addresses #23

dawehner’s picture

Status: Needs review » Reviewed & tested by the community

Fine, but then we should fix them properly, that makes no sense, now you're doing createFromRoute() with the return value from Drupal::l() :)

Ha, good point!

wim leers’s picture

Component: routing system » menu system
Issue tags: +Trivial patch of the month, +Documentation
alexpott’s picture

Priority: Major » Normal
Status: Reviewed & tested by the community » Fixed

Committed 0b5b1f1 and pushed to 8.0.x. Thanks!

  • alexpott committed 0b5b1f1 on 8.0.x
    Issue #2345801 by kgoel, tim.plunkett, dawehner, fgm, mr.baileys, Crell...

Status: Fixed » Closed (fixed)

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