I noticed per ticket https://www.drupal.org/node/2871090 that the breadcrumb variable has been removed and if I understand correctly is only accessible via blocks layout now. If this is the case, what is the recommend way to add breadcrumbs in twig templates?
Ive tried with a preprocess hook:
function mysubtheme_preprocess_page(&$variables) {
// Setup breadcrumbs.
$breadcrumb_block = \Drupal\block\Entity\Block::load('breadcrumbs');
$breadcrumb_block_content = \Drupal::entityTypeManager()
->getViewBuilder('block')
->view($breadcrumb_block);
$variables['breadcrumbs'] = $breadcrumb_block_content;
}
But when printing `{{ breadcrumbs }}` theres no output and a dump is just null.
Ive tried the following with twig_tweak to print the block directly in the twig:
{{ drupal_block('breadcrumbs') }}
But only works when the block is enabled in a region which obviously results the block rendering twice.
How would we get the breadcrumbs printed in twig without having to add a block to a region?
Thanks!
Comments
Comment #2
markhalliwellYou can't (at least not very easily).
They're blocks, they should just be assigned to the region you need. If you need a new region, create a sub-theme and add a specific "breadcrumb" region that you can manipulate in templates.
Comment #3
justclint CreditAttribution: justclint as a volunteer commentedThanks for the quick response @markcarver. Thats unfortunate to hear. Hopefully theres a better solution in the near future.
Even creating a new region still has the issue that you can only print the region in page.html.twig level template files. I eventually ended up using this solution to be able to print my regions to deeper twig files.
https://atendesigngroup.com/blog/making-region-content-available-node-te...
So I have this working now but this still seems like an unnecessary way to do it especially when building a component based theme where markup is isolated on a per component basis in a style guide like Pattern Lab or KSS for example.
Maybe this could provide a case why being able to render a theme variable (or other solution) thats not block dependent might be a good idea.
Thanks!
Comment #4
markhalliwellI'm confused, this is no different than creating a "breadcrumb" variable on the page level as well.
This is basically how its always been in Drupal. The only difference in 8.x is that they moved the code to a block where its placement can be controlled via the UI rather than one having to know any code to alter its placement on the page.
I honestly don't even understand this sentence. From my perspective, it appears that you're attempting to fight the [theme] system rather than understand how it works in the first place.
I have no idea why you would need access to breadcrumbs deeper than the page itself as it is a singular, global (page level), component designed to identify where one is at on a website.
Comment #5
flyke CreditAttribution: flyke commentedEnable twig_tweak module, and then:
{{ drupal_block('system_breadcrumb_block') }}
Comment #6
guaneagler CreditAttribution: guaneagler as a volunteer commented@flyke Thank you. Problem solved with your tips.
Comment #7
jaesperanza CreditAttribution: jaesperanza as a volunteer and commented@flyke, thanks! Working with a custom theme and paragraphs and this fixes some layout limitations, being duplicated similar to OP. Created a custom region where this is loaded. Perfect!
Comment #8
yusufhmWith twig_tweak, I found we can also use this:
{{ drupal_breadcrumb() }}
See https://www.drupal.org/docs/contributed-modules/twig-tweak/cheat-sheet#s...