You are here

function ApachesolrAttachmentsEntityFieldQuery::finishQuery in Apache Solr Attachments 7

Same name and namespace in other branches
  1. 6.3 apachesolr_attachments.module \ApachesolrAttachmentsEntityFieldQuery::finishQuery()

Finishes the query.

Adds tags, metaData, range and returns the requested list or count.

Parameters

SelectQuery $select_query: A SelectQuery which has entity_type, entity_id, revision_id and bundle fields added.

$id_key: Which field's values to use as the returned array keys.

Return value

See EntityFieldQuery::execute().

Overrides EntityFieldQuery::finishQuery

File

./apachesolr_attachments.module, line 797
Provides a file attachment search implementation for use with the Apache Solr module

Class

ApachesolrAttachmentsEntityFieldQuery

Code

function finishQuery($select_query, $id_key = 'entity_id') {
  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'])) {
      $column = $addedField['field_name'] . '_' . $addedField['column'];
      $column_alias = $addedField['field_name'] . '_' . $addedField['column_alias'];
    }
    else {
      $column = $addedField['column'];
      $column_alias = $addedField['column_alias'];
    }
    $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,
    ));

    // This is adding the file id using our metaData field.
    $entity->extraFields = $partial_entity;

    //$entity->file_fid = $partial_entity->{$this->metaData['field_key']};
    $return[$partial_entity->entity_type][$partial_entity->{$id_key} . '_' . $partial_entity->{$column}] = $entity;
    $this->ordered_results[] = $partial_entity;
  }
  return $return;
}