One of the most awesome new modules for Drupal I know is Search API. I'm using it to have full-text content search with sorting by flag count, all displayed by Views.
However, since flag is currently not integrated with Entity API (which Search API builds on), I had to do the integration myself...
/**
* Implements hook_entity_property_info_alter().
*/
function mymodule_entity_property_info_alter(&$info) {
if (isset($info['node'])){
$info['node']['properties'] += array(
'count_like' => array(
'type' => 'integer',
'label' => t('User Rating'),
'getter callback' => 'mymodule_get_node_like_count',
),
);
}
}
function mymodule_get_node_like_count($node) {
return (int) flag_get_flag('like_button')->get_count($node->nid);
}
Since that is really all there is to do, I would strongly suggest finding some way to do it automatically for every created flag.
Comments
Comment #1
ralf.strobel commentedFixing typo in title
Comment #2
olragon commentedI am working on the flag_entity module, extend default flag module. Can I merge this feature to flag_entity?
With flag_entity you can flag any entity, not just node/comment/user.
Work on the go, some task has been finished:
+ setting ui
+ field flag_entity: flag_entity reply on field api not hook_view to show flag link
Need work:
+ integrate with views
+ ...
Comment #3
EndEd commentedSubscribe
Comment #4
mrsinguyen commented+1
Comment #5
Shadlington commentedSubscribing
Comment #6
ralf.strobel commentedI just figured out that you can implement the getter callback in a much more general fashion that should make things a lot easier:
It's not a hook though. It still has to be defined as the "getter callback" value in every property info array. The corresponding array key will be passed as $name.
Comment #7
sammyd56 commentedI'd say this is more than a "minor" feature request. At the moment there is no way of adding flag links to a view without custom code if you are using Search API...
Comment #8
mrfelton commentedThe code in the original post is working perfectly for me, thanks. I'm not quite sure I understand the comments in #6 though. What exactly does that achieve, and how are you using that in conjunction with the original code?
Comment #9
ralf.strobel commentedIt's a generalized replacement for my initial mymodule_get_node_like_count. This solution comes in handy if you have more than one property you want to define a getter method for, because now you only have to define one function which can return different values based on the $entity and $name arguments.
Comment #10
jaxxham commentedHi there,
I'm getting an indexing error after using this code. Any ideas?
Comment #11
Dean Reilly commentedHmm, doesn't the code in #6 require that the like count is already a property of the entity? This doesn't seem like default behaviour as far as I can tell.
Comment #12
dasjoalso see #1035410: Flag any entity, #1362298: Independent views query backend flag links
Comment #13
ralf.strobel commented@10/11: The code I have posted above is only a very simple example to illustrate the principle. If you don't know how to customize it for yourself, don't use it. It will not work out of the box.
If you register a general getter function like I did in #6, it will be called with all the arguments you need to return any value related to an entity. But of course you still have to insert the custom code to retrieve it.
Comment #14
Dean Reilly commentedThanks, Ralf, that wasn't clear in your original message.
This issue is probably also relevant: #1315850: Add support for Entity API properties . It's not concerned with flag counts but exposes related entities in a fairly nice manner.
Comment #15
joachim commented#1315850: Add support for Entity API properties may well be a duplicate.
Comment #16
bennos commentedFlag is supports now every entity. looking for an easy way to use flag in the search api.
Comment #17
joachim commentedUpping the version.
Comment #18
joachim commentedClosing as a duplicate of #1315850: Add support for Entity API properties. Please reopen if there's functionality we still need for this.
Comment #19
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.