Skip to content

Instantly share code, notes, and snippets.

@drmalex07
Last active January 6, 2023 17:12
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save drmalex07/9696000 to your computer and use it in GitHub Desktop.
Save drmalex07/9696000 to your computer and use it in GitHub Desktop.
An example of an EntityFieldQuery in drupal 7. #drupal #php #drupal-entities
<pre><?php
////////////////////////////////////////////////////
// See more at https://drupal.org/node/1343708 //
////////////////////////////////////////////////////
//
// Filter entities based on several {entity,property,field}-based criteria
//
$query = new EntityFieldQuery();
/* filter on base entity properties */
$query->entityCondition('entity_type', 'user');
/* (user-specific) filter on base properties existing on database schema */
$query->propertyCondition('uid', '10', '>');
/* filter on a generic field value */
$query->fieldCondition('field_user_firstname', 'value', 'lalakis', '=');
/* paginate query */
$query->range(0,10);
$result = $query->execute();
//print_r($result);
//
// Pick one from the above results to load as an entity
//
if (count($result) and isset($result['user'])) {
$r = array_shift($result['user']);
$uid = $r->uid;
echo " -- Loading entity with uid=${uid} -- \n";
$e = entity_load_single('user', $uid);
print ' name=' . print_r($e,1) . "\n";
}
?></pre>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment