Last active
August 29, 2015 14:03
-
-
Save amcgowanca/947a89892281efa12373 to your computer and use it in GitHub Desktop.
Drupal 7 - EntityFieldPropertyQuery class
This file contains hidden or 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
<?php | |
/** | |
* @file | |
* Contains class EntityFieldPropertyQuery. | |
*/ | |
/** | |
* EntityFieldQuery class allowing for base table properties to be returned. | |
*/ | |
class EntityFieldPropertyQuery extends EntityFieldQuery { | |
/** | |
* An array of properties to include in the returned entity stubs. | |
* | |
* @var array | |
*/ | |
protected $properties = array(); | |
/** | |
* Adds a property to include in each returned entity stub. | |
* | |
* @param string $property_name | |
* The entity's property to include in returned entity stubs. | |
* | |
* @return CiscoCoreEntityFieldPropertyCondition | |
* Returns this instance. | |
*/ | |
public function addProperty($property_name) { | |
$this->properties[$property_name] = $property_name; | |
return $this; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
function finishQuery($select_query, $id_key = 'entity_id') { | |
$entity_info = entity_get_info($this->entityConditions['entity_type']['value']); | |
foreach ($this->properties as $property_name) { | |
if (!in_array($property_name, $entity_info['schema_fields_sql']['base table'])) { | |
throw new EntityFieldQueryException(t('Entity property %property does not exist in base table.', array('%property' => $property_name))); | |
} | |
$select_query->addField($entity_info['base table'], $property_name, $property_name); | |
} | |
foreach ($this->tags as $tag) { | |
$select_query->addTag($tag); | |
} | |
foreach ($this->metaData as $key => $object) { | |
$select_query->addMetaData($key, $object); | |
} | |
$select_query->addMetaData('entity_field_query', $this); | |
if ($this->range) { | |
$select_query->range($this->range['start'], $this->range['length']); | |
} | |
if ($this->count) { | |
return $select_query->countQuery()->execute()->fetchField(); | |
} | |
$return = array(); | |
foreach ($select_query->execute() as $partial) { | |
$bundle = isset($partial->bundle) ? $partial->bundle : NULL; | |
$entity = entity_create_stub_entity($partial->entity_type, array($partial->entity_id, $partial->revision_id, $bundle)); | |
foreach ($this->properties as $property_name) { | |
if (isset($partial->{$property_name})) { | |
$entity->{$property_name} = $partial->{$property_name}; | |
} | |
} | |
$return[$partial->entity_type][$partial->{$id_key}] = $entity; | |
$this->ordered_results[] = $partial; | |
} | |
return $return; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example: