You are here

views_term_hierarchy_weight_field.install in Views Term Hierarchy Weight Field 8

Same filename and directory in other branches
  1. 7 views_term_hierarchy_weight_field.install

File

views_term_hierarchy_weight_field.install
View source
<?php

use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\taxonomy\Entity\Vocabulary;
use Drupal\taxonomy\Entity\Term;
use Drupal\taxonomy\TermInterface;

/**
 * Implements hook_install().
 */
function views_term_hierarchy_weight_field_install() {
  FieldStorageConfig::create(array(
    'field_name' => 'field_tax_hierarchical_weight',
    'entity_type' => 'taxonomy_term',
    'type' => 'integer',
  ))
    ->save();
  FieldStorageConfig::create(array(
    'field_name' => 'field_tax_hierarchical_depth',
    'entity_type' => 'taxonomy_term',
    'type' => 'integer',
  ))
    ->save();
  $vocabularies = Vocabulary::loadMultiple();
  foreach ($vocabularies as $vid => $vocabulary) {
    FieldConfig::create(array(
      'field_name' => 'field_tax_hierarchical_weight',
      'entity_type' => 'taxonomy_term',
      'bundle' => $vid,
      'label' => 'Hierarchical Weight',
    ))
      ->save();
    FieldConfig::create(array(
      'field_name' => 'field_tax_hierarchical_depth',
      'entity_type' => 'taxonomy_term',
      'bundle' => $vid,
      'label' => 'Hierarchical Depth',
    ))
      ->save();
    $taxonomy_storage = \Drupal::service('entity_type.manager')
      ->getStorage('taxonomy_term');
    $tree = $taxonomy_storage
      ->loadTree($vid, $parent = 0, $max_depth = NULL, $load_entities = FALSE);
    views_term_hierarchy_weight_field_calculate_and_set_for_tree($tree);
  }
}

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

  # Deleting field storage.
  FieldStorageConfig::loadByName('taxonomy_term', 'field_tax_hierarchical_weight')
    ->delete();
  FieldStorageConfig::loadByName('taxonomy_term', 'field_tax_hierarchical_depth')
    ->delete();
}