Last active
January 6, 2023 17:12
-
-
Save drmalex07/9696000 to your computer and use it in GitHub Desktop.
An example of an EntityFieldQuery in drupal 7. #drupal #php #drupal-entities
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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