Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
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
Comment #1
bennos CreditAttribution: bennos commentedalso very interested. +1
Comment #2
mgriebe CreditAttribution: mgriebe commented+1
Comment #3
grasmash CreditAttribution: grasmash commentedI'm currently working on a solution for this. It shouldn't be too hard. To get the data into the index, try this:
I'll post something again once I figure out the best way to retrieve the data.
Comment #4
eggthing CreditAttribution: eggthing commented+1
Very interested indeed!
Comment #5
grasmash CreditAttribution: grasmash commentedChange 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.
Comment #6
mikemadison CreditAttribution: mikemadison commentedSo, 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?
Comment #7
grasmash CreditAttribution: grasmash commentedComment #8
eggthing CreditAttribution: eggthing commentedHere'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.
Comment #9
mikemadison CreditAttribution: mikemadison commentedSo, 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.
Comment #10
joachim CreditAttribution: joachim commentedUsing Views PHP is a hack.
Perhaps the code from comment #3 could find its way into #1315850: Add support for Entity API properties?
Comment #11
grasmash CreditAttribution: grasmash commented@joachim #1315850: Add support for Entity API properties looks very interesting. I was just sitting down to write something like it.
Comment #12
AllanTheDream CreditAttribution: AllanTheDream commentedThis 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?
Comment #13
gthing CreditAttribution: gthing commented(Comment deleted) derp.
Comment #14
zilla CreditAttribution: zilla commentedi 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
Comment #15
pinkonomy CreditAttribution: pinkonomy commented@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
Comment #16
zilla CreditAttribution: zilla commentedif 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:
i left the value code blank and it seemed to just inherit the flag settings correctly
Comment #17
gthing CreditAttribution: gthing commentedI 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?
Comment #18
gthing CreditAttribution: gthing commentedOkay 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...
Comment #19
gthing CreditAttribution: gthing commentedI 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.
Comment #20
speed6ump CreditAttribution: speed6ump commentedI 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.
Comment #21
Nick_vhBumping this to D8
Comment #22
drunken monkeyThere 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.
Comment #24
criznach CreditAttribution: criznach commented#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.