You are here

public static function TaxonomyManagerHelper::deleteTerms in Taxonomy Manager 8

Same name and namespace in other branches
  1. 2.0.x src/TaxonomyManagerHelper.php \Drupal\taxonomy_manager\TaxonomyManagerHelper::deleteTerms()

Helper function that deletes terms.

Optionally orphans (terms where parent get deleted) can be deleted as well.

Difference to core: deletion of orphans optional.

Parameters

array $tids: Array of term ids to delete.

bool $delete_orphans: If TRUE, orphans get deleted.

1 call to TaxonomyManagerHelper::deleteTerms()
DeleteTermsForm::submitForm in src/Form/DeleteTermsForm.php
Form submission handler.

File

src/TaxonomyManagerHelper.php, line 117

Class

TaxonomyManagerHelper
Class for taxonomy manager helper.

Namespace

Drupal\taxonomy_manager

Code

public static function deleteTerms(array $tids, $delete_orphans = FALSE) {
  $deleted_terms = [];
  $remaining_child_terms = [];
  while (count($tids) > 0) {
    $orphans = [];
    foreach ($tids as $tid) {
      $children = \Drupal::entityTypeManager()
        ->getStorage('taxonomy_term')
        ->loadChildren($tid);
      if (!empty($children)) {
        foreach ($children as $child) {
          $parents = \Drupal::entityTypeManager()
            ->getStorage('taxonomy_term')
            ->loadParents($child
            ->id());
          if ($delete_orphans) {
            if (count($parents) == 1) {
              $orphans[$child->tid] = $child
                ->id();
            }
            else {
              $remaining_child_terms[$child
                ->id()] = $child
                ->getName();
            }
          }
          else {
            $remaining_child_terms[$child
              ->id()] = $child
              ->getName();
            if ($parents) {

              // Parents structure see TermStorage::updateTermHierarchy.
              $parents_array = [];
              foreach ($parents as $parent) {
                if ($parent
                  ->id() != $tid) {
                  $parent->target_id = $parent
                    ->id();
                  $parents_array[$parent
                    ->id()] = $parent;
                }
              }
              if (empty($parents_array)) {
                $parents_array = [
                  0,
                ];
              }
              $child->parent = $parents_array;
              \Drupal::entityTypeManager()
                ->getStorage('taxonomy_term')
                ->deleteTermHierarchy([
                $child
                  ->id(),
              ]);
              \Drupal::entityTypeManager()
                ->getStorage('taxonomy_term')
                ->updateTermHierarchy($child);
            }
          }
        }
      }
      $term = \Drupal::entityTypeManager()
        ->getStorage('taxonomy_term')
        ->load($tid);
      if ($term) {
        $deleted_terms[] = $term
          ->getName();
        $term
          ->delete();
      }
    }
    $tids = $orphans;
  }
  return [
    'deleted_terms' => $deleted_terms,
    'remaining_child_terms' => $remaining_child_terms,
  ];
}