You are here

taxonomy_breadcrumb.module in Taxonomy Breadcrumb 8

Generates taxonomy based breadcrumbs on node pages and taxonomy/term pages.

The breadcrumb trail takes on the form: [HOME] >> [VOCABULARY] >> TERM >> [TERM] ...

  • The HOME breadcrumb (if present) links to the homepage. The text displayed for HOME is administrator configurable. If the HOME breadcrumb is not defined by the administrator, it will not appear in the breadcrumb trail.
  • The VOCABULARY breadcrumb (if present) will link to an administrator defined page. If the VOCABULARY does not have an administrator defined page, it will not appear in the breadcrumb trail.
  • Each TERM breadcrumb will link to either (1) taxonomy/term/tid by default, or (2) an administrator defined page if one is defined for the term.
  • These administrator defined "breadcrumb links" for VOCABULARIES and TERMS are controlled from the add/edit vocabulary and add/edit term administrator pages.

Examples: home >> term >> term mysite >> term >> term home >> vocabulary >> term >> term vocabulary >> term >> term

File

taxonomy_breadcrumb.module
View source
<?php

/**
 * @file
 * Generates taxonomy based breadcrumbs on node pages and taxonomy/term pages.
 *
 * The breadcrumb trail takes on the form:
 *   [HOME] >> [VOCABULARY] >> TERM >> [TERM] ...
 *
 *   - The HOME breadcrumb (if present) links to the homepage.  The text
 *     displayed for HOME is administrator configurable.  If the HOME
 *     breadcrumb is not defined by the administrator, it will not appear
 *     in the breadcrumb trail.
 *   - The VOCABULARY breadcrumb (if present) will link to an administrator
 *     defined page.  If the VOCABULARY does not have an administrator
 *     defined page, it will not appear in the breadcrumb trail.
 *   - Each TERM breadcrumb will link to either
 *     (1) taxonomy/term/tid by default, or
 *     (2) an administrator defined page if one is defined for the term.
 *   - These administrator defined "breadcrumb links" for VOCABULARIES and TERMS
 *     are controlled from the add/edit vocabulary and add/edit term
 *     administrator pages.
 *
 * Examples:
 *   home >> term >> term
 *   mysite >> term >> term
 *   home >> vocabulary >> term >> term
 *   vocabulary >> term >> term
 */
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Url;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;

/**
 * Implements hook_help().
 */
function taxonomy_breadcrumb_help($path) {
  switch ($path) {
    case 'admin/help#taxonomy_breadcrumb':
      $output = '<p>' . t('The taxonomy_breadcrumb module generates taxonomy based breadcrumbs on node pages and taxonomy/term pages. The breadcrumb trail takes on the form:') . '</p>';
      $output .= '<p>' . t('[HOME] >> [VOCABULARY] >> TERM >> [TERM] ...') . '</p>';
      $output .= '<ul>';
      $output .= '<li>' . t('The text displayed for HOME is configurable below. The <em>HOME</em> breadcrumb (if present) links to the homepage. The text displayed for HOME is configurable by an administrator. If the HOME breadcrumb is not defined by the administrator, it will not appear in the breadcrumb trail.') . '</li>';
      $output .= '<li>' . t('The <em>VOCABULARY</em> breadcrumb (if present) will link to an administrator defined page. If the VOCABULARY does not have an administrator defined page, it will not appear in the breadcrumb trail. This can be configured on the add/edit vocabulary pages within <a href="@taxonomy">Structure >> Taxonomy</a>', array(
        '@taxonomy' => Url::fromRoute('entity.taxonomy_vocabulary.collection'),
      )) . '</li>';
      $output .= '<li>' . t('Each <em>TERM</em> breadcrumb will link to either (1) taxonomy/term/tid by default, or (2) an administrator defined page if one is defined for the term. This can be configured on the add/edit term pages within <a href="@taxonomy">Structure >> Taxonomy</a>', array(
        '@taxonomy' => Url::fromRoute('entity.taxonomy_vocabulary.collection'),
      )) . '</li>';
      $output .= '</ul>';
      $output .= '<h3>' . t('Examples:') . '</h3>';
      $output .= '<ul>';
      $output .= '<li>' . t('home >> term >> term') . '</li>';
      $output .= '<li>' . t('mysite >> term >> term') . '</li>';
      $output .= '<li>' . t('home >> vocabulary >> term >> term') . '</li>';
      $output .= '<li>' . t('vocabulary >> term >> term') . '</li>';
      $output .= '</ul>';
      return $output;
  }
}

/**
 * Implements hook_ENTITY_TYPE_alter().
 */
function taxonomy_breadcrumb_entity_type_alter(array &$entity_types) {
  $entity_types['taxonomy_vocabulary']
    ->setHandlerClass('form', [
    'default' => 'Drupal\\taxonomy_breadcrumb\\VocabularyForm',
    'reset' => 'Drupal\\taxonomy\\Form\\VocabularyResetForm',
    'delete' => 'Drupal\\taxonomy\\Form\\VocabularyDeleteForm',
  ]);
}

/**
 * Implements hook_ENTITY_TYPE_update().
 */
function taxonomy_breadcrumb_taxonomy_vocabulary_update(EntityInterface $entity) {
  if (!FieldConfig::loadByName('taxonomy_term', $entity
    ->id(), 'taxonomy_breadcrumb_path')) {
    FieldConfig::create(array(
      'field_name' => 'taxonomy_breadcrumb_path',
      'entity_type' => 'taxonomy_term',
      'bundle' => $entity
        ->id(),
      'label' => t('Breadcrumb path (taxonomy_breadcrumb)'),
      'description' => t('The image of this term.'),
      'required' => FALSE,
    ))
      ->save();
  }
}

/**
 * Implements hook_ENTITY_TYPE_insert().
 */
function taxonomy_breadcrumb_taxonomy_vocabulary_insert(EntityInterface $entity) {
  if (!FieldConfig::loadByName('taxonomy_term', $entity
    ->id(), 'taxonomy_breadcrumb_path')) {
    FieldConfig::create(array(
      'field_name' => 'taxonomy_breadcrumb_path',
      'entity_type' => 'taxonomy_term',
      'bundle' => $entity
        ->id(),
      'label' => t('Breadcrumb path (taxonomy_breadcrumb)'),
      'description' => t('The image of this term.'),
      'required' => FALSE,
    ))
      ->save();
  }
}

/**
 * Implements hook_ENTITY_TYPE_delete().
 */
function taxonomy_breadcrumb_taxonomy_vocabulary_delete(EntityInterface $entity) {
  if (FieldStorageConfig::loadByName('taxonomy_term', 'taxonomy_breadcrumb_path')) {
    FieldConfig::loadByName('taxonomy_term', $entity
      ->id(), 'taxonomy_breadcrumb_path')
      ->delete();
  }
}

/**
 * Implements hook_ENTITY_TYPE_update().
 */
function taxonomy_breadcrumb_taxonomy_term_update(EntityInterface $entity) {
  $bundle = $entity
    ->bundle();
  if (!FieldConfig::loadByName('taxonomy_term', $bundle, 'taxonomy_breadcrumb_path')) {
    FieldConfig::create(array(
      'field_name' => 'taxonomy_breadcrumb_path',
      'entity_type' => 'taxonomy_term',
      'bundle' => $bundle,
      'label' => t('Breadcrumb path (taxonomy_breadcrumb)'),
      'description' => t('The image of this term.'),
      'required' => FALSE,
    ))
      ->save();
  }
}

/**
 * Implements hook_ENTITY_TYPE_insert().
 */
function taxonomy_breadcrumb_taxonomy_term_insert(EntityInterface $entity) {
  $bundle = $entity
    ->bundle();
  if (!FieldConfig::loadByName('taxonomy_term', $bundle, 'taxonomy_breadcrumb_path')) {
    FieldConfig::create(array(
      'field_name' => 'taxonomy_breadcrumb_path',
      'entity_type' => 'taxonomy_term',
      'bundle' => $bundle,
      'label' => t('Breadcrumb path (taxonomy_breadcrumb)'),
      'description' => t('The image of this term.'),
      'required' => FALSE,
    ))
      ->save();
  }
}

/**
 * Implements hook_ENTITY_TYPE_delete().
 */
function taxonomy_breadcrumb_taxonomy_term_delete(EntityInterface $entity) {
  if (FieldStorageConfig::loadByName('taxonomy_term', 'taxonomy_breadcrumb_path')) {
    FieldConfig::loadByName('taxonomy_term', $entity
      ->bundle(), 'taxonomy_breadcrumb_path')
      ->delete();
  }
}