Skip to content

Commit

Permalink
Merge pull request #1211 from dof-dss/cscs_breadcrumb
Browse files Browse the repository at this point in the history
CSCS: Create and enable breadcrumbs module
  • Loading branch information
ooneill547 authored Mar 18, 2022
2 parents fcfb4b2 + f46d190 commit 309e1c1
Show file tree
Hide file tree
Showing 8 changed files with 524 additions and 0 deletions.
1 change: 1 addition & 0 deletions config/cscsreviewni/config/core.extension.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ module:
content_moderation: 0
contextual: 0
cookie_content_blocker: 0
cscsreviewni_breadcrumbs: 0
csp: 0
ctools: 0
date_facet_drilldown: 0
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
name: 'CSCS Review breadcrumbs'
type: module
description: 'Breadcrumb customisations'
core_version_requirement: 8.x || ^9
package: 'CSCS Review'
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

/**
* @file
* Contains cscsreviewni_breadcrumbs.module.
*/

use Drupal\Core\Breadcrumb\Breadcrumb;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url;
use Drupal\Core\Link;

/**
* Implements hook_system_breadcrumb_alter().
*
* Altering the search view page breadcrumbs, when facets are selected the breadcrumb trail is overwritten.
*/
function cscsreviewni_breadcrumbs_system_breadcrumb_alter(Breadcrumb &$breadcrumb, RouteMatchInterface $route_match, array $context) {
$links = $breadcrumb->getLinks();
// Loop through the links for links that have facet queries added to them and unset the link of the last breadcrumb.
// The facets module breadcrumb links them all by default.
foreach ($links as $link) {
// Strip the - search results text from the page title breadcrumb link.
if (preg_match('/([A-Z|a-z]+) - search results/', $link->getText(), $matches)) {
$link->setText($matches[1]);
}

$params = $link->getUrl()->getRouteParameters();
if (!empty($params['facets_query'])) {
// Remove link from the last breadcrumb link.
end($links)->setUrl(Url::fromRoute('<none>'));
$breadcrumb = new Breadcrumb();
$breadcrumb->setLinks($links);
$breadcrumb->addCacheContexts(['url.path']);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
services:
cscsreviewni_breadcrumbs.breadcrumb.view_page:
class: Drupal\cscsreviewni_breadcrumbs\ViewPageBreadcrumb
arguments: ['@request_stack', '@title_resolver']
tags:
- { name: breadcrumb_builder, priority: 1011 }
cscsreviewni_breadcrumbs.breadcrumb.news:
class: Drupal\cscsreviewni_breadcrumbs\NewsBreadcrumb
arguments: [ '@entity_type.manager', '@title_resolver', '@request_stack' ]
tags:
- { name: breadcrumb_builder, priority: 100 }
cscsreviewni_breadcrumbs.breadcrumb.publication:
class: Drupal\cscsreviewni_breadcrumbs\PublicationBreadcrumb
arguments: [ '@entity_type.manager', '@title_resolver', '@request_stack' ]
tags:
- { name: breadcrumb_builder, priority: 100 }
cscsreviewni_breadcrumbs.breadcrumb.page:
class: Drupal\cscsreviewni_breadcrumbs\PageBreadcrumb
arguments: [ '@entity_type.manager', '@title_resolver', '@request_stack' ]
tags:
- { name: breadcrumb_builder, priority: 100 }
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
<?php

namespace Drupal\cscsreviewni_breadcrumbs;

/**
* @file
* Generates the breadcrumb trail for content including:
* - News
*
* In the format:
* > Home
* > News
* > current-page-title
*
* > <front>
* > /news
* > /current-page-title
*/
use Drupal\Core\Breadcrumb\Breadcrumb;
use Drupal\Core\Breadcrumb\BreadcrumbBuilderInterface;
use Drupal\Core\Controller\TitleResolverInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Link;
use Drupal\Core\Url;
use Drupal\node\NodeInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RequestStack;

/**
* {@inheritdoc}
*/
class NewsBreadcrumb implements BreadcrumbBuilderInterface {

/**
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;

/**
* Node object, or null if on a non-node page.
*
* @var \Drupal\node\Entity\Node
*/
protected $node;

/**
* The title resolver.
*
* @var \Drupal\Core\Controller\TitleResolverInterface
*/
protected $titleResolver;

/**
* Symfony\Component\HttpFoundation\RequestStack definition.
*
* @var Symfony\Component\HttpFoundation\RequestStack
*/
protected $request;

/**
* Class constructor.
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager, TitleResolverInterface $title_resolver, RequestStack $request) {
$this->entityTypeManager = $entity_type_manager;
$this->titleResolver = $title_resolver;
$this->request = $request;
}

/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('entity_type.manager'),
$container->get('title_resolver'),
$container->get('request_stack')
);
}

/**
* {@inheritdoc}
*/
public function applies(RouteMatchInterface $route_match) {
$match = FALSE;
$route_name = $route_match->getRouteName();
if ($route_name == 'entity.node.canonical') {
$this->node = $route_match->getParameter('node');
}

if ($route_name == 'entity.node.preview') {
$this->node = $route_match->getParameter('node_preview');
}

if (!empty($this->node)) {
if ($this->node instanceof NodeInterface == FALSE) {
$this->node = $this->entityTypeManager->getStorage('node');
}

if ($this->node->bundle() == 'news') {
$match = TRUE;
}
}

return $match;
}

/**
* {@inheritdoc}
*/
public function build(RouteMatchInterface $route_match) {
$breadcrumb = new Breadcrumb();
$title_resolver = $this->titleResolver->getTitle($this->request->getCurrentRequest(), $route_match->getRouteObject());
$links[] = Link::createFromRoute(t('Home'), '<front>');
$links[] = Link::fromTextandUrl(t('News'), Url::fromRoute('view.news_search.news_search_page'));
$links[] = Link::createFromRoute($title_resolver, '<none>');
$breadcrumb->setLinks($links);
$breadcrumb->addCacheContexts(['url.path']);
return $breadcrumb;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?php

namespace Drupal\cscsreviewni_breadcrumbs;

/**
* @file
* Generates the breadcrumb trail for content including:
* - Basic Page
*
* In the format:
* > Home
* > current-page-title
*
* > <front>
* > /current-page-title
*/
use Drupal\Core\Breadcrumb\Breadcrumb;
use Drupal\Core\Breadcrumb\BreadcrumbBuilderInterface;
use Drupal\Core\Controller\TitleResolverInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Link;
use Drupal\Core\Url;
use Drupal\node\NodeInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RequestStack;

/**
* {@inheritdoc}
*/
class PageBreadcrumb implements BreadcrumbBuilderInterface {

/**
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;

/**
* Node object, or null if on a non-node page.
*
* @var \Drupal\node\Entity\Node
*/
protected $node;

/**
* The title resolver.
*
* @var \Drupal\Core\Controller\TitleResolverInterface
*/
protected $titleResolver;

/**
* Symfony\Component\HttpFoundation\RequestStack definition.
*
* @var Symfony\Component\HttpFoundation\RequestStack
*/
protected $request;

/**
* Class constructor.
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager, TitleResolverInterface $title_resolver, RequestStack $request) {
$this->entityTypeManager = $entity_type_manager;
$this->titleResolver = $title_resolver;
$this->request = $request;
}

/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('entity_type.manager'),
$container->get('title_resolver'),
$container->get('request_stack')
);
}

/**
* {@inheritdoc}
*/
public function applies(RouteMatchInterface $route_match) {
$match = FALSE;
$route_name = $route_match->getRouteName();
if ($route_name == 'entity.node.canonical') {
$this->node = $route_match->getParameter('node');
}

if ($route_name == 'entity.node.preview') {
$this->node = $route_match->getParameter('node_preview');
}

if (!empty($this->node)) {
if ($this->node instanceof NodeInterface == FALSE) {
$this->node = $this->entityTypeManager->getStorage('node');
}

if ($this->node->bundle() == 'basic_page') {
$match = TRUE;
}
}

return $match;
}

/**
* {@inheritdoc}
*/
public function build(RouteMatchInterface $route_match) {
$breadcrumb = new Breadcrumb();
$title_resolver = $this->titleResolver->getTitle($this->request->getCurrentRequest(), $route_match->getRouteObject());
$links[] = Link::createFromRoute(t('Home'), '<front>');
$links[] = Link::createFromRoute($title_resolver, '<none>');
$breadcrumb->setLinks($links);
$breadcrumb->addCacheContexts(['url.path']);
return $breadcrumb;
}

}
Loading

0 comments on commit 309e1c1

Please sign in to comment.