Skip to content

Commit 0220bb4

Browse files
df-nikitaAlexSkrypnyk
authored andcommitted
#416: alignined term selection logic with other methods
1 parent 383e824 commit 0220bb4

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

src/Drupal/TaxonomyTrait.php

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -257,14 +257,20 @@ public function taxonomyDeleteTerm(string $vocabulary_machine_name, string $term
257257
throw new \RuntimeException(sprintf('The vocabulary "%s" does not exist.', $vocabulary_machine_name));
258258
}
259259

260-
$terms = \Drupal::entityTypeManager()
261-
->getStorage('taxonomy_term')
262-
->loadByProperties([
263-
'name' => $term_name,
264-
'vid' => $vocabulary_machine_name,
265-
]);
260+
$tids = $this->taxonomyLoadMultiple($vocabulary_machine_name, [
261+
'name' => $term_name,
262+
]);
266263

267-
$term = reset($terms);
264+
if (empty($tids)) {
265+
throw new \RuntimeException(sprintf('Unable to find the term "%s" in the vocabulary "%s".', $term_name, $vocabulary_machine_name));
266+
}
267+
268+
// Use the term created last.
269+
ksort($tids);
270+
$tid = end($tids);
271+
$term = \Drupal::entityTypeManager()
272+
->getStorage('taxonomy_term')
273+
->load($tid);
268274

269275
if (!$term) {
270276
throw new \RuntimeException(sprintf('Unable to find the term "%s" in the vocabulary "%s".', $term_name, $vocabulary_machine_name));

0 commit comments

Comments
 (0)