Skip to content

Instantly share code, notes, and snippets.

@brooke-heaton
Last active March 3, 2017 02:53
Show Gist options
  • Save brooke-heaton/c0a837b116c5f249b480cd529201ee61 to your computer and use it in GitHub Desktop.
Save brooke-heaton/c0a837b116c5f249b480cd529201ee61 to your computer and use it in GitHub Desktop.
Check if a node exists by node title using Entity Field Query (EFQ)
<? php
/**
* This Drupal 7 helper function can look up if a node exists and is useful
* if adding nodes in a postImport() method on a migration or
* if needing to avoid duplicates or relate nodes.
*/
public function checkNodeExistsByTitle($title) {
$query = new EntityFieldQuery();
$entities = $query->entityCondition('entity_type', 'node')
->propertyCondition('type', 'topic')
->propertyCondition('title', $title)
->range(0, 1)
->execute();
return $entities;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment