-2

I know there are several threads here about deleting terms with various techniques; I'm going to experiment deleting terms with drush at a certain level, say level three terms and below in the hierarchy. I was just curious if anyone here has any good ideas for this to inspire my work!


I'm pretty sure that can be done with the TermStorage class method loadAllParents, then determining which terms will be removed based on that count. I had forgotten about that method, loadParents would be much harder to use in this case, because we want to move all the way up the hierarchy.

0

Since my answer is being criticized for not being an answer (use loadAllParents and then perform the delete logic), I'll post some code, I guess that's what users want. This is untested and probably needs some more work, but this is basically what I meant

<?php

$manager = \Drupal::entityTypeManager()->getStorage('taxonomy_term');

$vid = 'some_vocabulary_id';

$tree = $manager->loadTree($vid, $parent = 0, $max_depth = NULL, $load_entities = TRUE);

$result = [];

foreach ($tree as $term) {
  $parents = $manager->loadAllParents($term->id());
  if (count($parents) > 3) {
    $result[] = $term;
  }
}

foreach ($result as $item) {
  $manager->delete($item);
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.