Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Drupal 8 Custom Breadcrumb block printing the 2 top level breadcrumbs including page title
<?php
namespace Drupal\custom_breadcrumb_block\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Breadcrumb\BreadcrumbBuilderInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Controller\TitleResolverInterface;
/**
* Provides a 'Breadcrumb' Block.
*
* @Block(
* id = "custom_breadcrumb_block",
* admin_label = @Translation("Custom Breadcrumb block"),
* category = @Translation("Custom Blocks"),
* )
*/
class CustomBreadcrumbBlock extends BlockBase implements ContainerFactoryPluginInterface {
/**
* The breadcrumb manager.
*
* @var \Drupal\Core\Breadcrumb\BreadcrumbBuilderInterface
*/
protected $breadcrumbManager;
/**
* The current route match.
*
* @var \Drupal\Core\Routing\RouteMatchInterface
*/
protected $routeMatch;
/**
* The title resolver service.
*
* @var \Drupal\Core\Controller\TitleResolverInterface
*/
protected $titleResolver;
/**
* Constructs a new CustomBreadcrumbBlock object.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Breadcrumb\BreadcrumbBuilderInterface $breadcrumb_manager
* The breadcrumb manager.
* @param \Drupal\Core\Routing\RouteMatchInterface $route_match
* The current route match.
* @param \Drupal\Core\Controller\TitleResolverInterface $titleResolver
* The title resolver.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, BreadcrumbBuilderInterface $breadcrumb_manager, RouteMatchInterface $route_match, TitleResolverInterface $titleResolver) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->breadcrumbManager = $breadcrumb_manager;
$this->routeMatch = $route_match;
$this->titleResolver = $titleResolver;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('breadcrumb'),
$container->get('current_route_match'),
$container->get('title_resolver')
);
}
/**
* {@inheritdoc}
*/
public function build() {
$breadcrumbs = $this->breadcrumbManager->build($this->routeMatch)->getLinks();
$items = [];
if(!empty($breadcrumbs[1])){
$items[] = $breadcrumbs[1]->getText();
}
if(!empty($breadcrumbs[2])){
$items[] = $breadcrumbs[2]->getText();
}
// Fallback: Page title
$request = \Drupal::request();
$page_title = $this->titleResolver->getTitle($request, $this->routeMatch->getRouteObject());
if(!empty($page_title)){
$items[] = $page_title;
}
return [
'#type' => 'inline_template',
'#template' => '
<div class="custom-markup-block">
<div class="first">
{{ linkFirst }}
</div>
{% if linkSecond is not empty %}
<div class="second">
{{ linkSecond }}
</div>
{% endif %}
</div>',
'#context' => [
'linkFirst' => !empty($items[0]) ? $items[0] : NULL,
'linkSecond' => !empty($items[1]) ? $items[1] : NULL,
],
'#cache' => array(
'contexts' => array('url.path'),
),
];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment