TaxonomyBreadcrumb.php in Taxonomy Breadcrumb 8
File
src/TaxonomyBreadcrumb.php
View source
<?php
namespace Drupal\taxonomy_breadcrumb;
use Drupal\Core\Breadcrumb\Breadcrumb;
use Drupal\Core\Breadcrumb\BreadcrumbBuilderInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Link;
use Drupal\Core\Menu\MenuActiveTrailInterface;
use Drupal\Core\Menu\MenuLinkManagerInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url;
class TaxonomyBreadcrumb implements BreadcrumbBuilderInterface {
protected $config;
protected $menuLinkManager;
protected $entityManager;
protected $termStorage;
protected $menuActiveTrail;
public function __construct(ConfigFactoryInterface $config_factory, MenuLinkManagerInterface $menu_link_manager, EntityManagerInterface $entityManager, MenuActiveTrailInterface $menu_active_trail) {
$this->config = $config_factory
->getEditable('taxonomy_breadcrumb.settings');
$this->menuLinkManager = $menu_link_manager;
$this->entityManager = $entityManager;
$this->termStorage = $entityManager
->getStorage('taxonomy_term');
$this->menuActiveTrail = $menu_active_trail;
}
public function applies(RouteMatchInterface $route_match) {
$node_object = $route_match
->getParameters()
->get('node');
$node_is_fieldable = $node_object instanceof FieldableEntityInterface;
if ($node_is_fieldable) {
$bundle = $node_object
->bundle();
$node_types = $this->config
->get('taxonomy_breadcrumb_node_types');
$exclude_include = $this->config
->get('taxonomy_breadcrumb_include_nodes');
if ($exclude_include) {
if (!$node_types[$bundle]) {
return FALSE;
}
}
else {
if ($node_types[$bundle]) {
return FALSE;
}
}
return TRUE;
}
return FALSE;
}
public function build(RouteMatchInterface $route_match) {
$breadcrumb = new Breadcrumb();
$node_object = $route_match
->getParameters()
->get('node');
$node_is_fieldable = $node_object instanceof FieldableEntityInterface;
if ($this->config
->get('taxonomy_breadcrumb_home')) {
$home = $this->config
->get('taxonomy_breadcrumb_home');
$breadcrumb
->addLink(Link::createFromRoute($home, '<front>'));
}
if ($node_is_fieldable) {
foreach ($node_object
->getFields() as $field) {
if ($field
->getSetting('target_type') == 'taxonomy_term') {
$vocabulary_bundles = $field
->getSettings()['handler_settings']['target_bundles'];
$vocabulary_machine_name = reset($vocabulary_bundles);
$entity_type = 'taxonomy_vocabulary';
$vocabulary_label = $vocabulary_bundles[$vocabulary_machine_name];
$vocabulary_entity = \Drupal::entityTypeManager()
->getStorage($entity_type)
->load($vocabulary_machine_name);
$taxonomy_breadcrumb_path = isset($vocabulary_entity
->getThirdPartySettings("taxonomy_breadcrumb", "taxonomy_breadcrumb_path")['taxonomy_breadcrumb_path']) ? $vocabulary_entity
->getThirdPartySettings("taxonomy_breadcrumb", "taxonomy_breadcrumb_path")['taxonomy_breadcrumb_path'] : '';
if ($taxonomy_breadcrumb_path) {
$breadcrumb
->addLink(Link::fromTextAndUrl($vocabulary_label, Url::fromUri('base:/' . $taxonomy_breadcrumb_path)));
}
foreach ($field
->referencedEntities() as $term) {
if ($term
->get('taxonomy_breadcrumb_path')
->getValue()) {
$breadcrumb
->addLink(Link::fromTextAndUrl($term
->get('taxonomy_breadcrumb_path')
->getValue()[0]['title'], Url::fromUri($term
->get('taxonomy_breadcrumb_path')
->getValue()[0]['uri'])));
}
else {
$breadcrumb
->addLink(Link::createFromRoute($term
->getName(), 'entity.taxonomy_term.canonical', [
'taxonomy_term' => $term
->id(),
]));
}
}
}
}
}
return $breadcrumb;
}
}