TaxonomyMenuTrailsBreadcrumbBuilder.php in Taxonomy Menu Trails 8
File
src/TaxonomyMenuTrailsBreadcrumbBuilder.php
View source
<?php
namespace Drupal\taxonomy_menu_trails;
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;
class TaxonomyMenuTrailsBreadcrumbBuilder implements BreadcrumbBuilderInterface {
use \Drupal\Core\StringTranslation\StringTranslationTrait;
protected $configFactory;
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->configFactory = $config_factory;
$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();
$entity_type = 'node';
$type = \Drupal::service('entity.manager')
->getStorage('node_type')
->load($bundle);
$configurations = $type
->getThirdPartySetting('taxonomy_menu_trails', 'taxonomy_menu_trails');
switch ($configurations['set_breadcrumb']) {
case "never":
return FALSE;
case 'if_empty':
$trailIds = $this->menuActiveTrail
->getActiveTrailIds('main');
if (!$trailIds) {
return FALSE;
}
break;
case 'always':
break;
}
foreach (\Drupal::entityManager()
->getFieldDefinitions($entity_type, $bundle) as $field_name => $field_definition) {
if ($configurations['taxonomy_term_references'][$field_name]) {
if (!empty($field_definition
->getTargetBundle())) {
if ($field_definition
->getType() == 'entity_reference') {
foreach ($node_object
->getFields() as $field) {
if ($field
->getSetting('target_type') == 'taxonomy_term') {
return TRUE;
}
}
}
}
}
}
}
return FALSE;
}
public function build(RouteMatchInterface $route_match) {
$breadcrumb = new Breadcrumb();
$breadcrumb
->addLink(Link::createFromRoute($this
->t('Home'), '<front>'));
$node_object = $route_match
->getParameters()
->get('node');
$node_is_fieldable = $node_object instanceof FieldableEntityInterface;
if ($node_is_fieldable) {
foreach ($node_object
->getFields() as $field) {
if ($field
->getSetting('target_type') == 'taxonomy_term') {
foreach ($field
->referencedEntities() as $term) {
$url = $term
->toUrl();
$route_links = $this->menuLinkManager
->loadLinksByRoute($url
->getRouteName(), $url
->getRouteParameters());
if (!empty($route_links)) {
$taxonomy_term_link = reset($route_links);
$plugin_definition = $taxonomy_term_link
->getPluginDefinition();
$taxonomy_term_id = $plugin_definition['route_parameters']['taxonomy_term'];
$parents = $this->termStorage
->loadAllParents($taxonomy_term_id);
foreach (array_reverse($parents) as $term1) {
$term1 = $this->entityManager
->getTranslationFromContext($term1);
$breadcrumb
->addLink(Link::createFromRoute($term1
->getName(), 'entity.taxonomy_term.canonical', [
'taxonomy_term' => $term1
->id(),
]));
}
}
}
}
}
}
return $breadcrumb;
}
}