You are here

breadcrumb_extra_field.module in Breadcrumb Extra Field 8

Same filename and directory in other branches
  1. 7 breadcrumb_extra_field.module
  2. 2.x breadcrumb_extra_field.module

Main module file.

File

breadcrumb_extra_field.module
View source
<?php

/**
 * @file
 * Main module file.
 */
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
define('BREADCRUMB_EXTRA_FIELD_ALLOWED_ENTITY_TYPES', serialize([
  'node',
  'user',
  'taxonomy_term',
  'comment',
  'bean',
  'block_content',
  'commerce_product',
  'group',
]));
define('BREADCRUMB_EXTRA_FIELD_ADMIN', 'breadcrumb_extra_field_admin');

/**
 * Implements hook_entity_extra_field_info().
 */
function breadcrumb_extra_field_entity_extra_field_info() {
  $extra = [];
  $admin = \Drupal::config('breadcrumb_extra_field.settings')
    ->get(BREADCRUMB_EXTRA_FIELD_ADMIN);
  $entity_info = \Drupal::service('entity_type.manager')
    ->getDefinitions();
  foreach ($entity_info as $entity_type_key => $entity_type) {
    $bundle_options = [];
    $bundles = \Drupal::service('entity_type.bundle.info')
      ->getBundleInfo($entity_type_key);

    // Add breadcrumb to configured entity types.
    foreach ($bundles as $bundle_key => $bundle) {
      if (!empty($admin[$entity_type_key]) && isset($admin[$entity_type_key][$bundle_key]) && $admin[$entity_type_key][$bundle_key]) {
        $extra[$entity_type_key][$bundle_key]['display']['breadcrumb'] = [
          'label' => t('Breadcrumb'),
          'description' => t('Breadcrumb'),
          'weight' => 0,
          'visible' => FALSE,
        ];
      }
    }
  }
  return $extra;
}

/**
 * Implements hook_entity_view().
 */
function breadcrumb_extra_field_entity_view(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode) {
  if ($display
    ->getComponent('breadcrumb')) {
    $build['breadcrumb'] = Drupal::service('breadcrumb')
      ->build(Drupal::routeMatch())
      ->toRenderable();
  }
}