You are here

taxonomy_term_depth.install in Taxonomy Term Depth 8

Same filename and directory in other branches
  1. 8.2 taxonomy_term_depth.install
  2. 7 taxonomy_term_depth.install

Install file.

File

taxonomy_term_depth.install
View source
<?php

/**
 * @file
 * Install file.
 */
use Drupal\Core\Database\Database;

/**
 * Require all constants
 */
require_once __DIR__ . '/constants.inc';

/**
 * Implements hook_install()
 */
function taxonomy_term_depth_install() {
  _taxonomy_term_depth_install_addfield();

  // Create field storage for the 'Highlight' base field.
  $entity_manager = \Drupal::service('entity.manager');
  $definition = $entity_manager
    ->getFieldStorageDefinitions('taxonomy_term')['depth_level'];
  $entity_manager
    ->onFieldStorageDefinitionCreate($definition);

  // Queue all terms to update depths.

  /**
   * @var $queue_manager Drupal\taxonomy_term_depth\QueueManager\Manager.
   */
  $queue_manager = \Drupal::service('taxonomy_term_depth.queue_service');
  $queue_manager
    ->queueBatch();
}
function _taxonomy_term_depth_install_addfield() {
  $schema = Database::getConnection()
    ->schema();
  if ($schema
    ->tableExists('taxonomy_term_field_data')) {
    $specs['fields']['depth'] = array(
      'type' => 'int',
      'size' => 'tiny',
      'description' => 'Taxonomy depth (deprecated. do not use!)',
      'default' => NULL,
    );
    $specs['indexes'] = array(
      'depth' => array(
        'depth',
        'tid',
      ),
    );
    $schema
      ->addField('taxonomy_term_field_data', 'depth', $specs['fields']['depth'], $specs['indexes']);
  }
}

/**
 * Implements hook_uninstall()
 */
function taxonomy_term_depth_uninstall() {

  // @todo: Replace with DefinitionUpdate handlers as explained on https://www.drupal.org/node/2078241
  $schema = Database::getConnection()
    ->schema();
  if ($schema
    ->tableExists('taxonomy_term_field_data')) {
    $schema
      ->dropField('taxonomy_term_field_data', 'depth');
  }
  $entity_manager = \Drupal::entityManager();
  $definition = $entity_manager
    ->getLastInstalledFieldStorageDefinitions('taxonomy_term')['depth_level'];
  if ($definition) {
    $entity_manager
      ->onFieldStorageDefinitionDelete($definition);
  }
}

/**
 * Update entity schema.
 */
function taxonomy_term_depth_update_8005() {
  taxonomy_term_depth_uninstall();
  taxonomy_term_depth_install();
  \Drupal::entityTypeManager()
    ->clearCachedDefinitions();
  \Drupal::entityDefinitionUpdateManager()
    ->applyUpdates();
}