Problem/Motivation

If you have Search API views using Profile2 indexes, your indexed data won't be refreshed if you only update the user basic account data ("user" entity). For example, this automatic update could be useful for sites that have some legal/permission/visibility checkboxes as user account fields.

Proposed resolution

Implement the hook_entity_update and notify Search API about changes on "profile2" entity when a "user" entity gets updated. For example this is the hook I have just implemented on my site to fix this:

/**
 * Implements hook_entity_update().
 *
 * On "user" update we notify Search API about a "profile2" update.
 *
 * @see search_api_entity_update
 */
function views_custom_filter_entity_update($entity, $type) {
  if ($type == 'user') {
    if (!entity_get_property_info($type)) {
      return;
    }

    list($id, , $new_bundle) = entity_extract_ids($type, $entity);

    if (isset($id)) {
      $account = user_load($id);
      $profile = profile2_load_by_user($account, 'alumni');

      // Profile of type "alumni" not empty.
      if (!empty($profile)) {
        $ptype = 'profile2';
        $pid = $profile->pid;

        search_api_track_item_change($ptype, array($pid));
        $combined_id = $ptype . '/' . $pid;
        search_api_track_item_change('multiple', array($combined_id));
      }
    }
  }
}

Comments

rcodina created an issue. See original summary.

rcodina’s picture

Issue summary: View changes
rickj’s picture

Issue summary: View changes
Status: Active » Closed (won't fix)

Thanks for posting this information, but as it's quite specific, and in relation to a different module, I don't think it's appropriate to include this code in profile2.

For someone, such as yourself, that needs this functionality, it can easily be achieved in a custom module, as you've done. Alternatively, it would be more appropriate as a patch to Search API (i.e. adding special-case treatment for profile2) rather than building it into profile2 itself.

rcodina’s picture

Thank you for the quick response! I understand your point of view, but let me share my thoughts:

1) I read somewhere that if a module provides a feature (like improved profiles) this module is the responsible to provide the code to integrate with other ones (in this case, the integration with Search API).

2) Profile 2 integrates with Search API easily because Profile 2 are entities! However, there is the little problem I explained on this issue.

3) I think Search API combined with Profile2 is not specific scenario. Maybe, there are lots of users that combine both modules given the potential of Search API module to improve the search results.

4) The little code I shared is only my specific fix but it could be modified to be more generic. So, in the future it could be included on Profile 2 in the form of a submodule.

5) I think that if you just change the issue status to "Postponed" it may be easy for other users in a similar problem to find the fix.