How can I append an Object path to Breadcrumbs?

I have a “Home->Tools[Document, listAction]->Some Tool[Object, detailAction]” hierarchy.

Breadcumbs in twig look like:

<div class="breadcrumbs">
    <div class="container">
        {% if pimcore_render_nav(mainNavigation, 'breadcrumbs', 'render', {maxDepth: 1}) is empty and document.id != 1%}
            <a href="{{ pimcore_document(1) }}">Home</a> <span class="breadseperator"></span> <a href="{{ document }}">{{ document.title }}</a>
        {% elseif document.id != 1 %}
            <a href="{{ pimcore_document(1) }}">Home</a> <span class="breadseperator"></span>
            {{ pimcore_render_nav(mainNavigation, 'breadcrumbs', 'render', {maxDepth: 1}) }}
        {% endif %}
    </div>
</div>

But this doesn’t know when I visit a detailed page.

What is the proper way to append Breadcrumbs with the current object path?

Thanks.

See https://pimcore.com/docs/6.x/Development_Documentation/Documents/Navigation.html#page_Adding-Custom-Items-to-the-Navigation

You can add pages to a navigation with mainNavigation.setPages(pages).
pages is array of page.

A page is an array like:

[
    'order' => 1, // order nr (-1 will be always first)
    'uri' => '/', //path 
    'label' => '', //visible label
    'title' => '', //tooltip text
    'active' => true //active state (boolean)
]

Build pages array in controller action and pass it to view.

My breadcrumb template:

{% set mainNavStartNode = document.getProperty('mainNavStartNode') %}
{% set breadcrumbNav = pimcore_build_nav(document, mainNavStartNode) %}

{% if breadcrumbPath is defined and breadcrumbPath|length %}
  {% do breadcrumbNav.setPages(breadcrumbPath) %}
{% endif %}
{% do pimcore_nav_renderer('breadcrumbs').setMinDepth(null) %}
{% set breadcrumbHtml = pimcore_render_nav(breadcrumbNav, 'breadcrumbs') %}
{% if breadcrumbHtml|length %}
<div class="grid-container">
  <nav class="breadcrumb">
    <a href="/{{ app.request.locale }}">{{ "Home"|trans }}</a> >
    {{ breadcrumbHtml|raw }}
  </nav>
</div>
{% endif %}

Can you please share how you build breadcrumbPath in your controller?

By now I have an ugly solution for now.

Thanks

For a news detail page / path:

$this->view->breadcrumbPath = [
                [
                    'order'  => 1,
                    'uri'    => $this->router->generate('my_bundle_news_index'),
                    'label'  => 'News',
                    'title'  => 'News',
                    'active' => true,
                    'pages'  => [
                        [
                            'order'  => 2,
                            'uri'    => '',
                            'label'  => $news->getTitle(),
                            'title'  => $news->getTitle(),
                            'active' => true,
                        ],
                    ],
                ],
            ];

Hope this helps

1 Like