I'm currently using Search API on my site. This means that I'm using the views integration for Search API for some of my sites (specifically with SOLR Search.) When I go into a normal view, I can create relationships using flags. However, when I go into a Search API view and look at the indexed node options, there is nothing there for Flags. I cannot actually reference the flag in any way, so I cannot use the Flags with the views module (as far as I can tell.)

Does anyone have a work around for this?

Does anyone have any suggestions?

http://drupal.org/project/search_api
http://drupal.org/project/search_api_solr

Comments

webroru’s picture

This topic interests me as well. Did you manage to find a solution?

mikemadison’s picture

I haven't heard anything back, and to the best of my knowledge it is still not functioning.

mikl’s picture

The same request, in reverse, on the Search API issue queue: #1634620: Flag module integration?.

mgriebe’s picture

+1

joachim’s picture

Version: 7.x-2.0-beta6 » 7.x-3.x-dev
Component: Views integration » Flag core

Upping the version.

mikemadison’s picture

Status: Active » Closed (duplicate)

There is some code over in the SearchAPI side of this request that works, but not as I was hoping. See - http://drupal.org/node/1634620#comment-6417498

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.

I'm going to mark this as closed a dup of http://drupal.org/node/1634620 since for my purposes, it is effectively "ok" given the API call. Feel free to reopen if someone wants to take the request in a different direction.

joachim’s picture

Using views_php is not an adequate fix IMO -- it's a hack, not a solution.

However, the code on that other issue seems to show this can be fixed with #1315850: Add support for Entity API properties.

mikemadison’s picture

Agree that it's not adequate, but given no real progress in a few months, it was better than the cobwebs I had =)

grasmash’s picture

To get flag count 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'm considering writing something that would also store the UIDs of 'flaggers' on the entity as well, but that may simply be too inefficient. The goal is to be able to search for nodes flagged by current user, or user x.

joachim’s picture

;
}

/**
* Implements hook_entity_property_info_alter().
*/
function mymodule_entity_property_info_alter(&$info) {

Please see the issue I linked to and help there if you can.

grasmash’s picture

@joachim

I'll work on an updated version of the patch, with your suggestions included, and flag count snippet from #1634620: Flag module integration?.

user654’s picture

.