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