From 5888d45b4153411941c07228cbba1de8bf270a32 Mon Sep 17 00:00:00 2001 From: Seth Shaw Date: Mon, 12 Aug 2019 06:48:20 -0600 Subject: [PATCH] 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! --- .travis.yml | 2 ++ .../islandora_breadcrumbs.services.yml | 2 +- .../src/IslandoraBreadcrumbBuilder.php | 27 ++++++++++++++----- 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/.travis.yml b/.travis.yml index 82b526f8..2743abda 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,6 +3,8 @@ language: php php: - 7.1 - 7.2 +services: + - mysql matrix: fast_finish: true diff --git a/modules/islandora_breadcrumbs/islandora_breadcrumbs.services.yml b/modules/islandora_breadcrumbs/islandora_breadcrumbs.services.yml index af9efa06..58e3c959 100644 --- a/modules/islandora_breadcrumbs/islandora_breadcrumbs.services.yml +++ b/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 } diff --git a/modules/islandora_breadcrumbs/src/IslandoraBreadcrumbBuilder.php b/modules/islandora_breadcrumbs/src/IslandoraBreadcrumbBuilder.php index 763c523a..54fe9534 100644 --- a/modules/islandora_breadcrumbs/src/IslandoraBreadcrumbBuilder.php +++ b/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 = [];