You are here

public static function Term::postDelete in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/taxonomy/src/Entity/Term.php \Drupal\taxonomy\Entity\Term::postDelete()

Acts on deleted entities before the delete hook is invoked.

Used after the entities are deleted but before invoking the delete hook.

Parameters

\Drupal\Core\Entity\EntityStorageInterface $storage: The entity storage object.

\Drupal\Core\Entity\EntityInterface[] $entities: An array of entities.

Overrides EntityBase::postDelete

File

core/modules/taxonomy/src/Entity/Term.php, line 78

Class

Term
Defines the taxonomy term entity.

Namespace

Drupal\taxonomy\Entity

Code

public static function postDelete(EntityStorageInterface $storage, array $entities) {
  parent::postDelete($storage, $entities);

  // See if any of the term's children are about to be become orphans.
  $orphans = [];

  /** @var \Drupal\taxonomy\TermInterface $term */
  foreach ($entities as $tid => $term) {
    if ($children = $storage
      ->getChildren($term)) {

      /** @var \Drupal\taxonomy\TermInterface $child */
      foreach ($children as $child) {
        $parent = $child
          ->get('parent');

        // Update child parents item list.
        $parent
          ->filter(function ($item) use ($tid) {
          return $item->target_id != $tid;
        });

        // If the term has multiple parents, we don't delete it.
        if ($parent
          ->count()) {
          $child
            ->save();
        }
        else {
          $orphans[] = $child;
        }
      }
    }
  }
  if (!empty($orphans)) {
    $storage
      ->delete($orphans);
  }
}