E
E
Evgeny Makov2016-10-20 08:44:20
Drupal
Evgeny Makov, 2016-10-20 08:44:20

Search Api and views_get_view() or views_get_view_results(), how to get field values ​​in views?

Created a view based on the Search API index.
I'm trying to get the results of a view with fields, since we need exactly the fields of objects, limited to the output of the page specified in the arguments.

<?php$myview = views_get_view('test');
$myview->set_display('page_2');
$myview->set_current_page(1); 
$myview->pre_execute();
$myview->execute();
var_dump($myview->result); 
?>

As a result, $myview->result contains an entity object, apparently search_api(?), of this structure
[0]=>
object(stdClass)#2965 (2) {
["entity"]=>
int(1032)
["_entity_properties"]=>
array(3) {
["search_api_relevance"]=>
float(1)
["search_api_excerpt"]=>
string(0) ""
["search_api_id"]=>
string(4) "1032"
}
}

view_get_view_result gives the same result.
In this case, I tried to load the entity through entity_load and EntityFieldQuery, but I can't figure out what type of entity to specify.
entity_load('search_api', array(1032), array(), FALSE) throws an error.
entity_load('search_api_index', array(1032), array(), FALSE) returns an empty array.
How to get field values ​​of indexed nodes?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
afi13, 2016-10-20
@ezhmkv

Search api view returns only entity IDs. In your case, the result is an array of objects. In each object:
'entity' - ID of your entity (nid if you search by nodes)
'_entity_properties' - an array with additional. info, the relevance value for this entity for this query, text fragments in which the search phrase was found. And "search_api_id" is the ID of the entity.
In order to get a node, it is enough to do node_load(); for each or better, collect all IDs in an array and do node_load_multiple();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question