Skip to content

Instantly share code, notes, and snippets.

@brooke-heaton
Last active March 3, 2017 02:53
Show Gist options
  • Save brooke-heaton/9c4ce984e5638e96b41a8ba6a03410ab to your computer and use it in GitHub Desktop.
Save brooke-heaton/9c4ce984e5638e96b41a8ba6a03410ab to your computer and use it in GitHub Desktop.
Check if a Taxonomy Term exists by Term Name
<?php
/**
* This helper Drupal 7 function can look up if a term exists and is useful
* if adding terms in a postImport() method on a migration or
* if needing to avoid duplicates or relate entities to the term.
*/
public function checkTermExistsByName($name) {
$query = new EntityFieldQuery();
$entities = $query->entityCondition('entity_type', 'taxonomy_term')
->propertyCondition('name', $name)
->range(0, 1)
->execute();
return $entities;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment