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

ralf.strobel’s picture

Title: Inetrage with Entity / Search API » Integrate with Entity / Search API
Priority: Normal » Minor

Fixing typo in title

olragon’s picture

Issue tags: +Flag, +addition extensions

I 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
+ ...

EndEd’s picture

Subscribe

mrsinguyen’s picture

+1

Shadlington’s picture

Subscribing

ralf.strobel’s picture

I just figured out that you can implement the getter callback in a much more general fashion that should make things a lot easier:

function MYMODULE_entity_get_properties($entity, $options, $name, $entity_type) {
  return $entity->$name; //in the simplest case
}

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.

sammyd56’s picture

Priority: Minor » Normal

I'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...

mrfelton’s picture

The 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?

ralf.strobel’s picture

It'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.

jaxxham’s picture

Hi there,

I'm getting an indexing error after using this code. Any ideas?

Dean Reilly’s picture

Hmm, 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.

dasjo’s picture

ralf.strobel’s picture

@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.

Dean Reilly’s picture

Thanks, 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.

joachim’s picture

bennos’s picture

Version: 7.x-2.x-dev » 7.x-2.0-beta8

Flag is supports now every entity. looking for an easy way to use flag in the search api.

joachim’s picture

Version: 7.x-2.0-beta8 » 7.x-3.x-dev

Upping the version.

joachim’s picture

Status: Active » Closed (duplicate)
Issue tags: -Flag, -addition extensions

Closing as a duplicate of #1315850: Add support for Entity API properties. Please reopen if there's functionality we still need for this.

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.