class TaxonomyMenuTrailsBreadcrumbBuilder in Taxonomy Menu Trails 8
Hierarchy
- class \Drupal\taxonomy_menu_trails\TaxonomyMenuTrailsBreadcrumbBuilder implements BreadcrumbBuilderInterface uses StringTranslationTrait
Expanded class hierarchy of TaxonomyMenuTrailsBreadcrumbBuilder
1 string reference to 'TaxonomyMenuTrailsBreadcrumbBuilder'
1 service uses TaxonomyMenuTrailsBreadcrumbBuilder
File
- src/
TaxonomyMenuTrailsBreadcrumbBuilder.php, line 19
Namespace
Drupal\taxonomy_menu_trailsView source
class TaxonomyMenuTrailsBreadcrumbBuilder implements BreadcrumbBuilderInterface {
use \Drupal\Core\StringTranslation\StringTranslationTrait;
/**
* The configuration object generator.
*
* @var \Drupal\Core\Config\ConfigFactory
*/
protected $configFactory;
/**
* The menu link manager interface.
*
* @var \Drupal\Core\Menu\MenuLinkManagerInterface
*/
protected $menuLinkManager;
/**
* The entity manager.
*
* @var \Drupal\Core\Entity\EntityManagerInterface
*/
protected $entityManager;
/**
* The taxonomy storage.
*
* @var \Drupal\Taxonomy\TermStorageInterface
*/
protected $termStorage;
/**
* The menu active trail interface.
*
* @var \Drupal\Core\Menu\MenuActiveTrailInterface
*/
protected $menuActiveTrail;
/**
* {@inheritdoc}
*/
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;
}
/**
* {@inheritdoc}
*/
public function applies(RouteMatchInterface $route_match) {
// This might be a "node" with no fields, e.g. a route to a "revision" URL,
// so we don't check for taxonomy fields on unfieldable nodes:
$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);
// Check if node type is selected in content type taxonomy trails settings.
// Check all the settings from content type configuration form.
$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) {
// Look for a "taxonomy attachment" by node field, regardless of language.
if ($configurations['taxonomy_term_references'][$field_name]) {
if (!empty($field_definition
->getTargetBundle())) {
// Check for term_reference/entity_reference fields from the content type.
if ($field_definition
->getType() == 'entity_reference') {
// Check all taxonomy terms applying to the current page.
foreach ($node_object
->getFields() as $field) {
if ($field
->getSetting('target_type') == 'taxonomy_term') {
return TRUE;
}
}
}
}
}
}
}
return FALSE;
}
/**
* {@inheritdoc}
*/
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) {
// Check all taxonomy terms applying to the current page.
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;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
TaxonomyMenuTrailsBreadcrumbBuilder:: |
protected | property | The configuration object generator. | |
TaxonomyMenuTrailsBreadcrumbBuilder:: |
protected | property | The entity manager. | |
TaxonomyMenuTrailsBreadcrumbBuilder:: |
protected | property | The menu active trail interface. | |
TaxonomyMenuTrailsBreadcrumbBuilder:: |
protected | property | The menu link manager interface. | |
TaxonomyMenuTrailsBreadcrumbBuilder:: |
protected | property | The taxonomy storage. | |
TaxonomyMenuTrailsBreadcrumbBuilder:: |
public | function |
Whether this breadcrumb builder should be used to build the breadcrumb. Overrides BreadcrumbBuilderInterface:: |
|
TaxonomyMenuTrailsBreadcrumbBuilder:: |
public | function |
Builds the breadcrumb. Overrides BreadcrumbBuilderInterface:: |
|
TaxonomyMenuTrailsBreadcrumbBuilder:: |
public | function |