Adding filtering by Nodequeue name in Search API Views

On some of the projects, I worked on I had to build a quite powerful search page made with Views. In order to improve performance, I decided to use the well-known Search API module. Of course, this module helped to achieve great results and everything was good before the moment I needed to integrate Nodequeue in my View. Search API doesn’t have such an integration yet and I had to figure something out with this. A solution was found under the hood of Entity API. Search API can index all entity properties, so the idea was to alter entity properties of my node by adding a custom dynamic property which will return node queues that this node belongs to. Here is an implementation of hook_entity_property_info_alter():

/**
* Implements hook_entity_property_info_alter().
*/
function my_module_entity_property_info_alter(&$info) {
 $info['node']['bundles']['node_bundle']['properties']['queue_name'] = array(
   'type' => 'list<string>',
   'options list' => '_my_module_nodequeue_options_list',
   'label' => 'Nodequeue name',
   'getter callback' => '_my_module_get_nodequeue_name',
   'description' => t('A name of a nodequeue that node is attached.'),
   'entity views field' => TRUE,
 );
}

The following function, ‘options list’ callback, returns all node queue names. This options list will be shown in a views filter settings:

/**
* Returns all node queues which works with property nodes.
*/
function _queue_name_nodequeue_options_list() {
 $nq_names = array();
 $node_queues = nodequeue_load_queues(array_keys(nodequeue_get_all_qids()));

 foreach ($node_queues as $key => &$node_queue) {
   $nq_names[$node_queue->name] = $node_queue->title;
 }

 return $nq_names;
}

'getter callback' function returns nodequeue’s machine name that node belongs to.

/**
* Get queue name for node.
*/
function _queue_name_get_nodequeue_name($node, $options, $name, $entity_type, $info) {
 $nq_names = array();
 $node_queues = nodequeue_load_queues(array_keys(nodequeue_get_all_qids()));

 foreach ($node_queues as $key => &$node_queue) {
   if (nodequeue_queue_position($node_queue->qid, $node->nid)) {
     $nq_names[] = $node_queue->name;
   }
 }

 return $nq_names;
}

After clearing cache, on index View Index page tab Fields you’ll see a new field ‘Nodequeue name’, check a checkbox to add to index and select type in the drop-down list as “String”.

Click ‘Save Changes’ and do re-index of the content. After this new filter Nodequeue Name will be available in your View. One useful thing that this filter supports multiple values.

You might also like