You are here

public function TaxonomyMenuTrailsBreadcrumbBuilder::applies in Taxonomy Menu Trails 8

Whether this breadcrumb builder should be used to build the breadcrumb.

Parameters

\Drupal\Core\Routing\RouteMatchInterface $route_match: The current route match.

Return value

bool TRUE if this builder should be used or FALSE to let other builders decide.

Overrides BreadcrumbBuilderInterface::applies

File

src/TaxonomyMenuTrailsBreadcrumbBuilder.php, line 76

Class

TaxonomyMenuTrailsBreadcrumbBuilder

Namespace

Drupal\taxonomy_menu_trails

Code

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;
}