You are here

function taxonomy_term_depth_entity_update in Taxonomy Term Depth 8.2

Same name and namespace in other branches
  1. 8 taxonomy_term_depth.module \taxonomy_term_depth_entity_update()
  2. 7 taxonomy_term_depth.module \taxonomy_term_depth_entity_update()

Implements hook_entity_update();

1 call to taxonomy_term_depth_entity_update()
taxonomy_term_depth_entity_insert in ./taxonomy_term_depth.module
Implements hook_entity_insert()

File

./taxonomy_term_depth.module, line 54
Main module file.

Code

function taxonomy_term_depth_entity_update($entity) {
  $entity_manager = \Drupal::entityTypeManager();
  $entity_type = $entity_manager
    ->getDefinition('taxonomy_term');
  $tablename = $entity_type
    ->getDataTable();

  // Update depth of the item on save
  if ($entity
    ->getEntityTypeId() == 'taxonomy_term') {

    /**
     * @var $entity \Drupal\taxonomy\Entity\Term
     */

    // Only update if depth was changed
    if (NULL === $entity->depth_level
      ->first() || $entity->depth_level
      ->first()->value != _taxonomy_term_depth_get_nocache($entity
      ->id())) {
      $depth = taxonomy_term_depth_get_by_tid($entity
        ->id(), TRUE);
      $entity->depth_level
        ->setValue([
        'value' => $depth,
      ]);
    }
  }
}