SonataSeoBundle
2.x

Reference Guide

  • 1. Installation
  • 2. Twig Helpers
  • 3. Usage
  • 4. Breadcrumb
    • 4.1. Create your own breadcrumb
  • 5. Social blocks
  • 6. Sitemap
SonataSeoBundle
  • »
  • 4. Breadcrumb
  • Edit on GitHub

4. Breadcrumb¶

The SonataSeoBundle let’s you create your own breadcrumbs based on your different website modules (news, products catalog…). The bundle uses KnpMenuBundle to generate the breadcrumb. Please refer to related documentation.

4.1. Create your own breadcrumb¶

First, you need to create a BlockService to handle your breadcrumbs. You can extend Sonata\SeoBundle\Block\Breadcrumb\BaseBreadcrumbMenuBlockService:

namespace App\Block;

use Sonata\BlockBundle\Block\BlockContextInterface;
use Sonata\SeoBundle\Block\Breadcrumb\BaseBreadcrumbMenuBlockService;

class MyCustomBreadcrumbBlockService extends BaseBreadcrumbMenuBlockService
{
    public function getName()
    {
        return 'app.block.breadcrumb';
    }

    protected function getMenu(BlockContextInterface $blockContext)
    {
        $menu = $this->getRootMenu($blockContext);

        $menu->addChild('my_awesome_action');

        return $menu;
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
<!-- config/services.xml -->

<service id="app.bundle.block.breadcrumb" class="App\Block\MyCustomBreadcrumbBlockService">
    <argument>my_custom_context</argument>
    <argument>acme.bundle.block.breadcrumb</argument>
    <argument type="service" id="templating"/>
    <argument type="service" id="knp_menu.menu_provider"/>
    <argument type="service" id="knp_menu.factory"/>
    <tag name="sonata.block"/>
    <tag name="sonata.breadcrumb"/>
</service>

You can also override the breadcrumb order by defining a priority:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
<!-- config/services.xml -->

<service id="app.bundle.block.breadcrumb" class="App\Block\MyCustomBreadcrumbBlockService">
    <argument>my_custom_context</argument>
    <argument>acme.bundle.block.breadcrumb</argument>
    <argument type="service" id="templating"/>
    <argument type="service" id="knp_menu.menu_provider"/>
    <argument type="service" id="knp_menu.factory"/>
    <tag name="sonata.block"/>
    <tag name="sonata.breadcrumb" priority="-127"/>
</service>

And to render the breadcrumb, just use this Twig helper :

1
2
3
4
{{ sonata_block_render_event('breadcrumb', {
    'context': 'my_custom_context',
    'current_uri': app.request.requestUri
}) }}

You can also use the shortcut to render the current breadcrumb :

1
{{ sonata_seo_breadcrumb() }}
Next Previous

© Copyright 2010-2021, Thomas Rabaix. Revision 5f2c73f7.

Built with Sphinx using a theme provided by Read the Docs.
Read the Docs v: 2.x
Versions
4.x
3.x
2.x
Downloads
On Read the Docs
Project Home
Builds