generated from dof-dss/nicsdru_origins_drupal
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1211 from dof-dss/cscs_breadcrumb
CSCS: Create and enable breadcrumbs module
- Loading branch information
Showing
8 changed files
with
524 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
...es/cscsreviewni/modules/custom/cscsreviewni_breadcrumbs/cscsreviewni_breadcrumbs.info.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
37 changes: 37 additions & 0 deletions
37
...ites/cscsreviewni/modules/custom/cscsreviewni_breadcrumbs/cscsreviewni_breadcrumbs.module
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']); | ||
} | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
...scsreviewni/modules/custom/cscsreviewni_breadcrumbs/cscsreviewni_breadcrumbs.services.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } |
122 changes: 122 additions & 0 deletions
122
web/sites/cscsreviewni/modules/custom/cscsreviewni_breadcrumbs/src/NewsBreadcrumb.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
119 changes: 119 additions & 0 deletions
119
web/sites/cscsreviewni/modules/custom/cscsreviewni_breadcrumbs/src/PageBreadcrumb.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
Oops, something went wrong.