You are here

function entity_property_query in Entity API 7

Queries for entities having the given property value.

Parameters

$entity_type: The type of the entity.

$property: The name of the property to query for.

$value: A single property value or an array of possible values to query for.

$limit: Limit the number of results. Defaults to 30.

Return value

array An array of entity ids or NULL if there is no information how to query for the given property.

1 call to entity_property_query()
EntityMetadataTestCase::testEntityQuery in ./entity.test
Tests using entity_property_query().

File

includes/entity.property.inc, line 111
Provides API functions around hook_entity_property_info(). Also see entity.info.inc, which cares for providing entity property info for all core entity types.

Code

function entity_property_query($entity_type, $property, $value, $limit = 30) {
  $properties = entity_get_all_property_info($entity_type);
  $info = $properties[$property] + array(
    'type' => 'text',
    'queryable' => !empty($properties[$property]['schema field']),
  );

  // We still support the deprecated query callback, so just add in EFQ-based
  // callbacks in case 'queryable' is set to TRUE and make use of the callback.
  if ($info['queryable'] && empty($info['query callback'])) {
    $info['query callback'] = !empty($info['field']) ? 'entity_metadata_field_query' : 'entity_metadata_table_query';
  }
  $type = $info['type'];

  // Make sure an entity or a list of entities are passed on as identifiers
  // with the help of the wrappers. For that ensure the data type matches the
  // passed on value(s).
  if (is_array($value) && !entity_property_list_extract_type($type)) {
    $type = 'list<' . $type . '>';
  }
  elseif (!is_array($value) && entity_property_list_extract_type($type)) {
    $type = entity_property_list_extract_type($type);
  }
  $wrapper = entity_metadata_wrapper($type, $value);
  $value = $wrapper
    ->value(array(
    'identifier' => TRUE,
  ));
  if (!empty($info['query callback'])) {
    return $info['query callback']($entity_type, $property, $value, $limit);
  }
}