efq_extra_field.module in EntityFieldQuery Extra Fields 7
File
efq_extra_field.module
View source
<?php
class EntityFieldQueryExtraFields extends EntityFieldQuery {
private $addedFields = array();
function finishQuery($select_query, $id_key = 'entity_id') {
$tables =& $select_query
->getTables();
foreach ($this->fieldConditions as $key => $fieldCondition) {
if ($fieldCondition['operator'] == 'IS NULL' && isset($this->fields[$key]['storage']['details']['sql'][FIELD_LOAD_CURRENT])) {
$keys = array_keys($this->fields[$key]['storage']['details']['sql'][FIELD_LOAD_CURRENT]);
$sql_table = reset($keys);
foreach ($tables as $table_id => $table) {
if ($table['table'] == $sql_table) {
$tables[$table_id]['join type'] = 'LEFT OUTER';
}
}
}
}
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 ($this->addedFields as $addedField) {
$fields = $select_query
->getFields();
if (!empty($addedField['field_name'])) {
$tables = $select_query
->getTables();
$clean_tables = $this
->cleanTables($tables);
$table = 'field_data_' . $addedField['field_name'];
if (isset($clean_tables[$table])) {
$addedField['table'] = $clean_tables[$table]['alias'];
}
$column = $addedField['field_name'] . '_' . $addedField['column'];
$column_alias = $addedField['field_name'] . '_' . $addedField['column_alias'];
}
else {
$column = $addedField['column'];
$column_alias = $addedField['column_alias'];
}
if (!empty($addedField['table'])) {
$select_query
->addField($addedField['table'], $column, $column_alias);
}
else {
$select_query
->addField($fields['entity_id']['table'], $column, $column_alias);
}
}
foreach ($select_query
->execute() as $partial_entity) {
$bundle = isset($partial_entity->bundle) ? $partial_entity->bundle : NULL;
$entity = entity_create_stub_entity($partial_entity->entity_type, array(
$partial_entity->entity_id,
$partial_entity->revision_id,
$bundle,
));
$entity->extraFields = $partial_entity;
if (isset($return[$partial_entity->entity_type][$partial_entity->{$id_key}])) {
$previous_entity = $return[$partial_entity->entity_type][$partial_entity->{$id_key}];
foreach ($previous_entity->extraFields as $id => $child) {
if (!is_array($previous_entity->extraFields->{$id})) {
if ($entity->extraFields->{$id} != $previous_entity->extraFields->{$id}) {
$entity->extraFields->{$id} = array(
$previous_entity->extraFields->{$id},
$entity->extraFields->{$id},
);
}
}
else {
if (!in_array($entity->extraFields->{$id}, $previous_entity->extraFields->{$id})) {
$previous_entity->extraFields->{$id}[] = $entity->extraFields->{$id};
$entity->extraFields->{$id} = $previous_entity->extraFields->{$id};
}
}
}
}
$return[$partial_entity->entity_type][$partial_entity->{$id_key}] = $entity;
$this->ordered_results[] = $partial_entity;
}
return $return;
}
public function addExtraField($field_name, $column, $column_alias = NULL, $table = NULL) {
if (!empty($field_name) && !$this
->checkFieldExists($field_name)) {
$this
->fieldCondition($field_name);
}
$this->addedFields[] = array(
'field_name' => $field_name,
'column' => $column,
'column_alias' => $column_alias,
'table' => $table,
);
return $this;
}
private function cleanTables($tables) {
if (!is_array($tables)) {
return array();
}
foreach ($tables as $table_id => $table) {
if ($table['join type'] == 'INNER') {
$tables[$table['table']] = $table;
unset($tables[$table_id]);
}
}
return $tables;
}
private function checkFieldExists($field_name) {
$fields = $this->fields;
foreach ($fields as $field) {
if (isset($field['field_name']) && $field['field_name'] == $field_name) {
return TRUE;
}
}
return FALSE;
}
}