You are here

taxonomy_breadcrumb.install in Taxonomy Breadcrumb 8

Install file for the taxonomy_breadcrumb module.

File

taxonomy_breadcrumb.install
View source
<?php

/**
 * @file
 * Install file for the taxonomy_breadcrumb module.
 */
use Drupal\Core\Field\FieldException;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;

/**
 * Implements hook_install().
 */
function taxonomy_breadcrumb_install() {

  // Get all the vocabularies machine name.
  $bundles = array_keys(\Drupal::entityTypeManager()
    ->getStorage('taxonomy_vocabulary')
    ->loadMultiple());
  $entity_type = 'taxonomy_term';
  $field_name = 'taxonomy_breadcrumb_path';
  if (!FieldStorageConfig::loadByName($entity_type, $field_name)) {
    try {
      FieldStorageConfig::create(array(
        'field_name' => $field_name,
        'entity_type' => 'taxonomy_term',
        'type' => 'link',
      ))
        ->save();
    } catch (FieldException $e) {
    }
  }
  foreach ($bundles as $bundle) {
    if (!FieldConfig::loadByName($entity_type, $bundle, $field_name)) {
      try {
        FieldConfig::create(array(
          'field_name' => $field_name,
          'entity_type' => 'taxonomy_term',
          'bundle' => $bundle,
          'label' => t('Breadcrumb path (taxonomy_breadcrumb)'),
          'description' => t('Taxonomy Breadcrumb of this term.'),
          'required' => FALSE,
        ))
          ->save();
      } catch (FieldException $e) {
      }
    }
  }
}

/**
 * Implements hook_uninstall().
 */
function taxonomy_breadcrumb_uninstall() {

  // Remove variables.
  \Drupal::configFactory()
    ->getEditable('taxonomy_breadcrumb.settings')
    ->delete();

  // Delete taxonomy term field and its instances.
  $entity_type = 'taxonomy_term';
  $field_name = 'taxonomy_breadcrumb_path';
  $bundles = array_keys(\Drupal::entityTypeManager()
    ->getStorage('taxonomy_vocabulary')
    ->loadMultiple());
  foreach ($bundles as $bundle) {
    if (FieldConfig::loadByName($entity_type, $bundle, $field_name)) {
      try {
        FieldConfig::loadByName($entity_type, $bundle, $field_name)
          ->delete();
      } catch (FieldException $e) {
      }
    }
  }
  if (FieldStorageConfig::loadByName($entity_type, $field_name)) {
    try {
      FieldStorageConfig::loadByName($entity_type, $field_name)
        ->delete();
    } catch (FieldException $e) {
    }
  }
}