diff --git contrib/search_api_views/includes/handler_field.inc contrib/search_api_views/includes/handler_field.inc index a8fd27f..86e5ec1 100644 --- contrib/search_api_views/includes/handler_field.inc +++ contrib/search_api_views/includes/handler_field.inc @@ -5,6 +5,8 @@ * don't need any special treatment and don't have special options. * * Handles lists automatically. + * + * @TODO Building off of views_handler_field_prerender_list might give better list handling. */ class SearchApiViewsHandlerField extends views_handler_field { @@ -84,7 +86,7 @@ class SearchApiViewsHandlerField extends views_handler_field { */ public function query() { // Add the field. - $this->query->addField($this->real_field); + $this->field_alias = $this->query->addField($this->real_field); $this->addAdditionalFields(); } @@ -108,7 +110,7 @@ class SearchApiViewsHandlerField extends views_handler_field { } if (!empty($fields) && is_array($fields)) { foreach ($fields as $identifier => $info) { - $this->query->addField(is_array($info) ? $info['field'] : $info); + $this->aliases[$identifier] = $this->query->addField(is_array($info) ? $info['field'] : $info); } } } @@ -120,14 +122,25 @@ class SearchApiViewsHandlerField extends views_handler_field { $this->query->sort($this->real_field, $order); } + public function get_value($values, $field = NULL) { + // Look in the _fields property first. + $alias = isset($field) ? $this->aliases[$field] : $this->field_alias; + if (isset($values->_fields[$alias])) { + return $values->_fields[$alias]; + } + + // Maybe it's a regular value. + return parent::get_value($values, $field); + } + /** * Render the field. * * @param array $values * The values retrieved from the database. */ - public function render(array $values) { - $value = $values[$this->real_field]; + public function render($values) { + $value = $this->get_value($values); if (is_array($value)) { return $this->renderArray($value, $values); } @@ -137,7 +150,7 @@ class SearchApiViewsHandlerField extends views_handler_field { /** * Render a list of values. */ - protected function renderArray($value, array $values) { + protected function renderArray($value, $values) { if (isset($this->options['list']['mode'])) { if ($this->options['list']['mode'] == 'first') { $value = count($value) ? array_shift($value) : NULL; @@ -165,13 +178,13 @@ class SearchApiViewsHandlerField extends views_handler_field { * Render a value as a link to the entity, if applicable. */ // @todo Let e.g. author:name be linked to author instead - protected function renderLink($value, array $values) { + protected function renderLink($value, $values) { $render = $this->renderValue($value, $values); if (!$this->options['link_to_entity']) { return $render; } - $entity = $values['entity']; - $type = isset($values['search_api_views_entity_type']) ? $values['search_api_views_entity_type'] : $this->query->getIndex()->entity_type; + $entity = $values->entity; + $type = isset($values->search_api_views_entity_type) ? $values->search_api_views_entity_type : $this->query->getIndex()->entity_type; $url = entity_uri($type, $entity); return l($render, $url['path'], array('html' => TRUE) + $url['options']); } diff --git contrib/search_api_views/includes/handler_field_entity.inc contrib/search_api_views/includes/handler_field_entity.inc index 4a6913a..e0f7c66 100644 --- contrib/search_api_views/includes/handler_field_entity.inc +++ contrib/search_api_views/includes/handler_field_entity.inc @@ -46,7 +46,7 @@ class SearchApiViewsHandlerFieldEntity extends SearchApiViewsHandlerField { /** * Render a value as a link to the entity, if applicable. */ - protected function renderLink($value, array $values) { + protected function renderLink($value, $values) { $render = $this->renderValue($value, $values); if (!$this->options['link_to_entity'] || !is_numeric($value)) { return $render; diff --git contrib/search_api_views/includes/query.inc contrib/search_api_views/includes/query.inc index a064c1f..c068259 100644 --- contrib/search_api_views/includes/query.inc +++ contrib/search_api_views/includes/query.inc @@ -89,6 +89,7 @@ class SearchApiViewsQuery extends views_plugin_query { */ public function addField($field) { $this->fields[$field] = TRUE; + return $field; } /** @@ -156,12 +157,12 @@ class SearchApiViewsQuery extends views_plugin_query { protected function addResults(array $results, $view) { // Maybe the service class or a postprocessor already set the entities. foreach ($results as $id => $result) { + $view->result[$id] = (object) $result; if (!empty($result['entity'])) { $wrapper = entity_metadata_wrapper($this->index->entity_type, $result['entity']); $fields = $this->extractFields($wrapper, $this->fields); if ($fields) { - $view->result[$id] = $fields; - $view->result[$id]['search_api_relevance'] = $result['score']; + $view->result[$id]->_fields = $fields; } else { $ids[] = $id; @@ -182,9 +183,8 @@ class SearchApiViewsQuery extends views_plugin_query { continue; } $wrapper = entity_metadata_wrapper($this->index->entity_type, $entity); - $view->result[$id]['entity'] = $entity; - $view->result[$id]['search_api_relevance'] = $results[$id]['score']; - $view->result[$id] += $this->extractFields($wrapper, $this->fields); + $view->result[$id]->_fields = $this->extractFields($wrapper, $this->fields); + $view->result[$id]->entity = $entity; } catch (Exception $e) { continue;