Hi,

I would like to sort a search API views by flag count, e.g. most flagged items first. Is this possible? I cannot see how to do this and have no option in my views or a flag_count in the fields on search api, just a flag_follow_user.

Any suggestions?

Thanks,
Paul.

Comments

joachim’s picture

I'm not familiar with how search API views work. There is at least one issue about this IIRC, with a patch that needs testing.

techypaul’s picture

I found a few issues that revolve around flag/search api functionality. I think this one is the one I need:

https://drupal.org/comment/6417498#comment-6417498

I will try this code out tomorrow and let you know what happens. I am wary of using this in production though and wondered if you had any thoughts on whether something like it might get committed in 7? The issue itself has been bumped to 8 now, but I think by the end of the issue it is more about the flag/unflag behaviour on search api results, than simply the count.

Thanks,
Paul.

joachim’s picture

I thought it was this #1362298: Independent views query backend flag links but I might be wrong.

It says lower down in the issue you linked to that the code snippet at https://drupal.org/comment/6417498#comment-6417498 was committed to Flag in #1315850: Add support for Entity API properties.

techypaul’s picture

Argh! I can't work out what is going on here.

I have 3.2 so therefore this patch should be committed as suggested, yet in my search api fields list, I get only:

Users who flagged the entity with flag follow

When I select this, I get a facet that shows the actual users who have flagged those nodes, e.g. I can drill down by user to see what they have flagged. The original patch suggests, as I want it to do, that it provides the count as an indexible field:

 // For a given item, add count of users that have flagged that item.
+      $info[$flag->content_type]['bundles'][$bundle]['properties']['flag_' . $flag->name . '_count'] = array(
+        'label' => t('Flag: @flag_name flag count', $vars),
+        'description' => t('Flag: The total number of @flag_name flags for this @bundle', $vars),
+        'type' => 'integer',
+        'getter callback' => 'flag_properties_get_count',
+        'computed' => TRUE,
+        'data' => array('flag' => $flag),
+      );

However, in a later patch by yourself (wip), this is missing. I cannot find this in the code of 3.2 either. I have a feeling that this got left behind?

The independent views issue is all about flagging/unflagging in views whereas I just want to sort my results by total count and/or allow a facet for the same thing.

Thanks,
Paul.

joachim’s picture

Version: 7.x-3.3 » 7.x-3.x-dev

The snippet you mention was only in one of the patches on #1315850: Add support for Entity API properties, at comment #15. It looks like things took a bit of a fork at that point, and it never made it into the patch I ended up making.

Looking at the code, there's no property for flag counts.

So if that's what you need, the best thing would be to file a new issue and work on a patch, taking that patch on #1315850: Add support for Entity API properties as a starting point.

techypaul’s picture

Thanks joachim, I will try to :)

Paul.

digitgopher’s picture

[Deleted]