You are here

21 October 2018
Retrieve entities based on entity properties, field values, and generic entity metadata (bundle, entity type, entity ID, and revision ID). It is not possible to query across multiple entity types. Queries do not automatically respect entity access restrictions. Node access control is performed by the SQL storage engine, but other storage engines might not do this.

Get nodes

Get nodes using EntityFieldQuery.
Source code viewer
  1. $nodes = new EntityFieldQuery();
  2. $nodes
  3. ->entityCondition('entity_type', 'node')
  4. ->entityCondition('bundle', 'model');
  5.  
  6. $nodes = $nodes->execute();
  7. if (isset($nodes['node'])) {
  8. $node_ids = array_keys($nodes['node']);
  9. foreach ($node_ids as $nid) {
  10. dpm($nodes);
  11. }
  12. }
Programming Language: PHP

Field collection

Source code viewer
  1. $efq = new EntityFieldQuery();
  2. $efq->entityCondition('entity_type', 'field_collection_item');
  3.  
  4. // No bundle value, use field name
  5. $efq->propertyCondition('field_name', 'field_appointment_document');
  6.  
  7. // I'm restricting to search items attached to something, so filtering by the FC I want to check.
  8. $efq->propertyCondition('item_id', $doc_fc_ids);
  9.  
  10. // We don't want revisions!
  11. $efq->propertyCondition('archived', 0);
  12.  
  13. // An example to filter by a field attached to the FC.
  14. $efq->fieldCondition('field_document_type', 'tid', array(123,432));
  15.  
  16. // No date information or useful other property info, but I want the newest one only.
  17. // So highest ID is the newest, and only return one result.
  18. $efq->propertyOrderBy('item_id', 'DESC');
  19. $efq->range(0, 1);
Programming Language: PHP