Adding Flag Count field to Search API Solr index
Just a quick snippet!
Dropping this in a custom module will allow you to easily index values from the Flag module's {flag_count} table in a Search API Solr index.
<span style="color: #000000"><span style="color: #0000BB"><?php<br></span><span style="color: #FF8000">/**<br> * Get the flag count for a given node.<br> */<br></span><span style="color: #007700">function </span><span style="color: #0000BB">mymodule_get_count</span><span style="color: #007700">(</span><span style="color: #0000BB">$entity</span><span style="color: #007700">, </span><span style="color: #0000BB">$options</span><span style="color: #007700">, </span><span style="color: #0000BB">$name</span><span style="color: #007700">, </span><span style="color: #0000BB">$entity_type</span><span style="color: #007700">, &</span><span style="color: #0000BB">$info</span><span style="color: #007700">) {<br> </span><span style="color: #FF8000">// Requiring type node since we're relying on $entity->nid,<br> // but this could be used for user objects too.<br> </span><span style="color: #007700">if (</span><span style="color: #0000BB">$entity_type </span><span style="color: #007700">== </span><span style="color: #DD0000">'node'</span><span style="color: #007700">) {<br> </span><span style="color: #0000BB">$query </span><span style="color: #007700">= </span><span style="color: #0000BB">db_select</span><span style="color: #007700">(</span><span style="color: #DD0000">'flag_counts' </span><span style="color: #007700">,</span><span style="color: #DD0000">'fc'</span><span style="color: #007700">);<br> </span><span style="color: #0000BB">$query</span><span style="color: #007700">-></span><span style="color: #0000BB">fields</span><span style="color: #007700">(</span><span style="color: #DD0000">'fc'</span><span style="color: #007700">, array(</span><span style="color: #DD0000">'count'</span><span style="color: #007700">));<br> </span><span style="color: #0000BB">$query</span><span style="color: #007700">-></span><span style="color: #0000BB">condition</span><span style="color: #007700">(</span><span style="color: #DD0000">'fc.fid'</span><span style="color: #007700">, </span><span style="color: #0000BB">$info</span><span style="color: #007700">[</span><span style="color: #DD0000">'data'</span><span style="color: #007700">][</span><span style="color: #DD0000">'flag'</span><span style="color: #007700">]-></span><span style="color: #0000BB">fid</span><span style="color: #007700">);<br> </span><span style="color: #0000BB">$query</span><span style="color: #007700">-></span><span style="color: #0000BB">condition</span><span style="color: #007700">(</span><span style="color: #DD0000">'fc.content_type'</span><span style="color: #007700">, </span><span style="color: #DD0000">'node'</span><span style="color: #007700">);<br> </span><span style="color: #0000BB">$query</span><span style="color: #007700">-></span><span style="color: #0000BB">condition</span><span style="color: #007700">(</span><span style="color: #DD0000">'fc.content_id'</span><span style="color: #007700">, </span><span style="color: #0000BB">$entity</span><span style="color: #007700">-></span><span style="color: #0000BB">nid</span><span style="color: #007700">);<br> </span><span style="color: #0000BB">$count </span><span style="color: #007700">= </span><span style="color: #0000BB">$query</span><span style="color: #007700">-></span><span style="color: #0000BB">execute</span><span style="color: #007700">()-></span><span style="color: #0000BB">fetchColumn</span><span style="color: #007700">();<br> }<br> return !empty(</span><span style="color: #0000BB">$count</span><span style="color: #007700">) ? </span><span style="color: #0000BB">$count </span><span style="color: #007700">: </span><span style="color: #0000BB">0</span><span style="color: #007700">;<br>}<p></p></span><span style="color: #FF8000">/**<br>* Implements hook_entity_property_info_alter().<br>*/<br></span><span style="color: #007700">function </span><span style="color: #0000BB">mymodule_entity_property_info_alter</span><span style="color: #007700">(&</span><span style="color: #0000BB">$info</span><span style="color: #007700">) {<br> if (isset(</span><span style="color: #0000BB">$info</span><span style="color: #007700">[</span><span style="color: #DD0000">'node'</span><span style="color: #007700">][</span><span style="color: #DD0000">'bundles'</span><span style="color: #007700">])) {<br> </span><span style="color: #FF8000">// For each content type.<br> </span><span style="color: #007700">foreach (</span><span style="color: #0000BB">$info</span><span style="color: #007700">[</span><span style="color: #DD0000">'node'</span><span style="color: #007700">][</span><span style="color: #DD0000">'bundles'</span><span style="color: #007700">] as </span><span style="color: #0000BB">$bundle_type </span><span style="color: #007700">=> </span><span style="color: #0000BB">$bundle</span><span style="color: #007700">) {<br> </span><span style="color: #FF8000">// Find all applicable flags for this content type.<br> </span><span style="color: #0000BB">$flags </span><span style="color: #007700">= </span><span style="color: #0000BB">flag_get_flags</span><span style="color: #007700">(</span><span style="color: #DD0000">'node'</span><span style="color: #007700">, </span><span style="color: #0000BB">$bundle_type</span><span style="color: #007700">);<br> </span><span style="color: #FF8000">// For each applicable flag.<br> </span><span style="color: #007700">foreach (</span><span style="color: #0000BB">$flags </span><span style="color: #007700">as </span><span style="color: #0000BB">$fid </span><span style="color: #007700">=> </span><span style="color: #0000BB">$flag</span><span style="color: #007700">) {<br> </span><span style="color: #0000BB">$info</span><span style="color: #007700">[</span><span style="color: #DD0000">'node'</span><span style="color: #007700">][</span><span style="color: #DD0000">'bundles'</span><span style="color: #007700">][</span><span style="color: #0000BB">$bundle_type</span><span style="color: #007700">][</span><span style="color: #DD0000">'properties'</span><span style="color: #007700">][</span><span style="color: #DD0000">'flag_' </span><span style="color: #007700">. </span><span style="color: #0000BB">$flag</span><span style="color: #007700">-></span><span style="color: #0000BB">name </span><span style="color: #007700">. </span><span style="color: #DD0000">'_count'</span><span style="color: #007700">] = array(<br> </span><span style="color: #DD0000">'label' </span><span style="color: #007700">=> </span><span style="color: #0000BB">t</span><span style="color: #007700">(</span><span style="color: #DD0000">'@title Flag Count'</span><span style="color: #007700">, array(</span><span style="color: #DD0000">'@title' </span><span style="color: #007700">=> </span><span style="color: #0000BB">$flag</span><span style="color: #007700">-></span><span style="color: #0000BB">title</span><span style="color: #007700">)),<br> </span><span style="color: #DD0000">'description' </span><span style="color: #007700">=> </span><span style="color: #0000BB">t</span><span style="color: #007700">(</span><span style="color: #DD0000">'The total number of @title flags for this node.'</span><span style="color: #007700">, array(</span><span style="color: #DD0000">'@title' </span><span style="color: #007700">=> </span><span style="color: #0000BB">$flag</span><span style="color: #007700">-></span><span style="color: #0000BB">title</span><span style="color: #007700">)),<br> </span><span style="color: #DD0000">'type' </span><span style="color: #007700">=> </span><span style="color: #DD0000">'integer'</span><span style="color: #007700">,<br> </span><span style="color: #DD0000">'getter callback' </span><span style="color: #007700">=> </span><span style="color: #DD0000">'mymodule_get_count'</span><span style="color: #007700">,<br> </span><span style="color: #DD0000">'computed' </span><span style="color: #007700">=> </span><span style="color: #0000BB">TRUE</span><span style="color: #007700">,<br> </span><span style="color: #DD0000">'data' </span><span style="color: #007700">=> array(</span><span style="color: #DD0000">'flag' </span><span style="color: #007700">=> </span><span style="color: #0000BB">$flag</span><span style="color: #007700">),<br> );<br> }<br> }<br> }<br>}<br></span><span style="color: #0000BB">?></span></span>
After placing this in a custom module enabling, just go to the 'fields' tab on the desired Search API Solr index and select the 'flag_type Flag Count' field with type integer.
7.x,
drupal, search api, flag, solr, search, snippet