function Term::postDelete

Same name and namespace in other branches
  1. 9 core/modules/taxonomy/src/Entity/Term.php \Drupal\taxonomy\Entity\Term::postDelete()
  2. 8.9.x core/modules/taxonomy/src/Entity/Term.php \Drupal\taxonomy\Entity\Term::postDelete()
  3. 11.x core/modules/taxonomy/src/Entity/Term.php \Drupal\taxonomy\Entity\Term::postDelete()

Overrides EntityBase::postDelete

File

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

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);
  }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.