You are here

class TermDelete in Taxonomy Delete 8

Class TermDelete.

@package Drupal\taxonomy_delete

Hierarchy

Expanded class hierarchy of TermDelete

1 string reference to 'TermDelete'
taxonomy_delete.services.yml in ./taxonomy_delete.services.yml
taxonomy_delete.services.yml
1 service uses TermDelete
taxonomy_delete.term_delete in ./taxonomy_delete.services.yml
Drupal\taxonomy_delete\TermDelete

File

src/TermDelete.php, line 11

Namespace

Drupal\taxonomy_delete
View source
class TermDelete {

  /**
   * Drupal\Core\Entity\EntityTypeManagerInterface definition.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * Use core services object.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager) {
    $this->entityTypeManager = $entity_type_manager;
  }

  /**
   * Delete terms by Vocabulary.
   *
   * @param string $vid
   *   The Vocabulary from which the Terms needs to be deleted.
   *
   * @return array
   *   An array of terms which gets deleted.
   */
  public function deleteTermByVid($vid) {
    $terms = [];
    $controller = $this->entityTypeManager
      ->getStorage('taxonomy_term');
    $tree = $controller
      ->loadTree($vid);
    foreach ($tree as $term) {
      $terms[] = $term->tid;
    }
    $entities = $controller
      ->loadMultiple($terms);
    $controller
      ->delete($entities);
    return count($terms);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
TermDelete::$entityTypeManager protected property Drupal\Core\Entity\EntityTypeManagerInterface definition.
TermDelete::deleteTermByVid public function Delete terms by Vocabulary.
TermDelete::__construct public function Use core services object.