Currently, it seems that Search API has no support for Flag module – that is, you cannot index the flag state of a node.

This is used on many sites for promoting content and similar things, so it would be really helpful if this could be supported.

A similar issue has been filed against flag module, but I'm not sure on which project that needs to do the work, but here it is, for reference: #1508738: Search API Integration.

Comments

bennos’s picture

also very interested. +1

mgriebe’s picture

+1

grasmash’s picture

I'm currently working on a solution for this. It shouldn't be too hard. To get the data into the index, try this:

/**
 * Get the flag count for a given node.
 */
function mymodule_get_count($entity, $options, $name, $entity_type, &$info) {
  // Requiring type node since we're relying on $entity->nid,
  // but this could be used for user objects too.
  if ($entity_type == 'node') {
    $query = db_select('flag_counts' ,'fc');
    $query->fields('fc', array('count'));
    $query->condition('fc.fid', $info['data']['flag']->fid);
    $query->condition('fc.content_type', 'node');
    $query->condition('fc.content_id', $entity->nid);
    $count = $query->execute()->fetchColumn();
  }
  return !empty($count) ? $count : 0;
}
 
/**
* Implements hook_entity_property_info_alter().
*/
function mymodule_entity_property_info_alter(&$info) {
  if (isset($info['node']['bundles'])) {
    // For each content type.
    foreach ($info['node']['bundles'] as $bundle_type => $bundle) {
      // Find all applicable flags for this content type.
      $flags = flag_get_flags('node', $bundle_type);
      // For each applicable flag.
      foreach ($flags as $fid => $flag) {
        $info['node']['bundles'][$bundle_type]['properties']['flag_' . $flag->name . '_count'] = array(
          'label' => t('@title Flag Count', array('@title' => $flag->title)),
          'description' => t('The total number of @title flags for this node.', array('@title' => $flag->title)),
          'type' => 'integer',
          'getter callback' => 'mymodule_get_count',
          'computed' => TRUE,
          'data' => array('flag' => $flag),
        );
      }
    }
  }
}

I'll post something again once I figure out the best way to retrieve the data.

eggthing’s picture

+1
Very interested indeed!

grasmash’s picture

Change my mind on this. The snippet that I posted works well for indexing. Display the data is as simple as selecting a field from search_api_views.

mikemadison’s picture

So, it looks like your snippet goes into a custom module, then I just reindex my content and add the flag in the view? Does that sound right?

grasmash’s picture

  1. Add snippet to custom module
  2. Enable module
  3. Enable new flag count field on your index
  4. Re-index site
  5. Add new flag count field to your view
eggthing’s picture

Here's the error I got with #3 in a custom module;

Notice: Undefined index: label in entity_views_field_definition() (line 191 of /var/aegir/platforms/drupal-7.15/sites/all/modules/entity/views/entity.views.inc)

I am able to select flags as fields in Search api and the facets do show up ok but the "Empty facet behavior" isn't working correctly under "Display settings" in "Configure facet display". Even though "do not display facet" is selected, all the flag blocks show up in searches whether there are flagged nodes or not.

mikemadison’s picture

So, the code in number 3 works fine for me, but it doesn't really give me what I was looking for.

My solution was to use the http://drupal.org/project/views_php module to add a php field, and then just used the flag api to pass in the link. So, the contents of my php field were:

print flag_create_link('bookmark', $data->entity);

bookmark being the type of flag, and $data->entity being the node id. In this case, I would never have anything but a nid in the entity itself.

joachim’s picture

Using Views PHP is a hack.

Perhaps the code from comment #3 could find its way into #1315850: Add support for Entity API properties?

grasmash’s picture

@joachim #1315850: Add support for Entity API properties looks very interesting. I was just sitting down to write something like it.

AllanTheDream’s picture

This case mentions the flag count, I would add a flag link to a search index view as one of the fields. What's the way around that, please?

gthing’s picture

(Comment deleted) derp.

zilla’s picture

i used the snippet from #9 up above in my output field and it worked like a charm, though i know it's a hack - just be sure to double check the machine name of the flag that you intend to use, and it will inherit the flag link settings correctly.

i'm using this in a search api index (solr on back) to allow users to quickly add items to a collection (bookmarks) while viewing search results - including layering on exposed filters and facets and so on....it's very, very useful in my scenario

pinkonomy’s picture

@zilla:How did you make this work?
I have the same situation,I want to be able users to Bookmark content from the search results.
I used this code : print flag_create_link('favourite', $data->entity);
where favourite is the machine name of my flag.
What should I put on Value code and what on Output code?
thanks

zilla’s picture

if you're using views php: https://drupal.org/project/views_php

then you put the code snippet from above into the output code of a field of type php, in your case:

<?php
print flag_create_link('favourite', $data->entity); 
?>

i left the value code blank and it seemed to just inherit the flag settings correctly

gthing’s picture

I have been using the solution from #9 on a site and recently after updating several modules the pager on my view stopped appearing. I can replicate this by creating new view of a particular search index, enabling the pager, and adding a php field with the code given in #9. The pager will disappear, even from the views preview. Can anyone else replicate this?

gthing’s picture

Okay I just found that my pager disappears just by having a views php field present, regardless of whether or not it has anything in it. Hmmm...

gthing’s picture

I found a solution using the patch from post #5 here: #2123315: Pager does not appear if fields use Views PHP (7.x-1.x)

Turns out the latest version of views_php introduced the issue.

speed6ump’s picture

I recently figured out how to integrate flag into search api on my website. I needed to let users search for products and be able to flag them so they could easily find them again at a later time. I clicked on "Edit search page" and scrolled down to "View mode" and changed it from "Themed as search results" to Commerce Product: Attribute View" and saved my changes. I also made sure to have the correct fields enabled and my flag fields are as follows:

Creator » Flagged commerce_product with flag student_product_flags
Creator » Flagged node with flag bookmarks

When I did this it showed the title of the product, pricing info, add to cart and my flag/unflag link. I hope this helps.

Nick_vh’s picture

Version: 7.x-1.x-dev » 8.x-1.x-dev
Issue summary: View changes

Bumping this to D8

drunken monkey’s picture

Version: 8.x-1.x-dev » 7.x-1.x-dev
Category: Feature request » Support request
Status: Active » Fixed

There is nothing to do in this module, and also #1315850: Add support for Entity API properties looks like this was fixed over a year ago in the Flag module, actually. With newer Flag module versions, you should be able to index and display flags like any other field.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

criznach’s picture

#3 works for me, but the column names in mymodule_get_count() are incorrect for the current release of flag. I fixed it by changing content_type to entity_type, and content_id to entity_id to match the actual field names in your flag_counts database table.