Browse Source

Fix Breadcrumb error when viewing node tab: Issue 1238 (#155)

* Fix applies when viewing a node's tab

* remove logging message used during dev

* clarify note about getRawParameters

* update build to use getRawParameters

* use dependency injection for node storage

* TRAVIS!
pull/729/head
Seth Shaw 5 years ago committed by Jared Whiklo
parent
commit
5888d45b41
  1. 2
      .travis.yml
  2. 2
      modules/islandora_breadcrumbs/islandora_breadcrumbs.services.yml
  3. 27
      modules/islandora_breadcrumbs/src/IslandoraBreadcrumbBuilder.php

2
.travis.yml

@ -3,6 +3,8 @@ language: php
php:
- 7.1
- 7.2
services:
- mysql
matrix:
fast_finish: true

2
modules/islandora_breadcrumbs/islandora_breadcrumbs.services.yml

@ -1,6 +1,6 @@
services:
islandora_breadcrumbs.breadcrumb:
class: Drupal\islandora_breadcrumbs\IslandoraBreadcrumbBuilder
arguments: ['@config.factory']
arguments: ['@entity_type.manager', '@config.factory']
tags:
- { name: breadcrumb_builder, priority: 100 }

27
modules/islandora_breadcrumbs/src/IslandoraBreadcrumbBuilder.php

@ -3,6 +3,7 @@
namespace Drupal\islandora_breadcrumbs;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Breadcrumb\Breadcrumb;
use Drupal\Core\Breadcrumb\BreadcrumbBuilderInterface;
@ -20,13 +21,23 @@ class IslandoraBreadcrumbBuilder implements BreadcrumbBuilderInterface {
*/
protected $config;
/**
* Storage to load nodes.
*
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected $nodeStorage;
/**
* Constructs a breadcrumb builder.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_manager
* Storage to load nodes.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The configuration factory.
*/
public function __construct(ConfigFactoryInterface $config_factory) {
public function __construct(EntityTypeManagerInterface $entity_manager, ConfigFactoryInterface $config_factory) {
$this->nodeStorage = $entity_manager->getStorage('node');
$this->config = $config_factory->get('islandora.breadcrumbs');
}
@ -34,10 +45,13 @@ class IslandoraBreadcrumbBuilder implements BreadcrumbBuilderInterface {
* {@inheritdoc}
*/
public function applies(RouteMatchInterface $attributes) {
$parameters = $attributes->getParameters()->all();
if (!empty($parameters['node'])) {
return ($parameters['node']->hasField($this->config->get('referenceField')) &&
!$parameters['node']->get($this->config->get('referenceField'))->isEmpty());
// Using getRawParameters for consistency (always gives a
// node ID string) because getParameters sometimes returns
// a node ID string and sometimes returns a node object.
$nid = $attributes->getRawParameters()->get('node');
if (!empty($nid)) {
$node = $this->nodeStorage->load($nid);
return (!empty($node) && $node->hasField($this->config->get('referenceField')) && !$node->get($this->config->get('referenceField'))->isEmpty());
}
}
@ -46,7 +60,8 @@ class IslandoraBreadcrumbBuilder implements BreadcrumbBuilderInterface {
*/
public function build(RouteMatchInterface $route_match) {
$node = $route_match->getParameter('node');
$nid = $route_match->getRawParameters()->get('node');
$node = $this->nodeStorage->load($nid);
$breadcrumb = new Breadcrumb();
$chain = [];

Loading…
Cancel
Save