0

Q: Can I combine two (geofield) field instances from different relationships into one aggregated Search API field?

So far I tried search_api_combined, which seems ok, but does only support the default field types, but not geofield LatLong for proximity search.

I was thinking that I could somehow craft a custom relationship that, for a given (product) node fetches one of the two profiles, or one of the two field instance values.

I guess all I need to know really is where the relationships in Search API are coming from.

Use case details:

  • Two profile2 profile types: Private person, merchant. Usually one person has only one such profile. In case of doubt, I would pick one and ignore the other.
  • Both contain a field instance of "field_coordinates" geofield.
  • Both contain a postal address field that acts as a geocoding source for the geofield, but with different granularity (private person only specifies postal code, merchant specifies full address). (This is one of the reasons why I'm using profile2) (This is not central to the question, just to give a better idea about the problem space.)
  • Search API and Facet API.
  • Product nodes with authors.
  • Profile2 geofield field_coordinates should act as the location for products.
  • Search index for products should combine the field_coordinates from different profiles into one aggregated field.
0

Oh, this turned out to be easy. Use hook_entity_property_info_alter() to define a new property.

/**
 * Implements hook_entity_property_info_alter().
 *
 * @see profile2_entity_property_info_alter()
 */
function MYMODULE_entity_property_info_alter(&$info) {
  // Add related profiles to the user object.
  $properties = &$info['user']['properties'];
  $properties['MYMODULE_profile2'] = array(
    'type' => 'profile2',
    'label' => 'Profile 2 (MYMODULE)',
    'description' => 'The first profile2 profile that can be found on the user.',
    /* @see _MYMODULE_get_profile2() */
    'getter callback' => '_MYMODULE_get_profile2',
    # 'bundle' => $type_name
  );
}

/**
 * Entity property callback.
 *
 * @param object $account
 * @param array $options
 * @param string $name
 *
 * @return object|null
 *
 * @see profile2_user_get_properties()
 */
function _MYMODULE_get_profile2($account, array $options, $name) {
  $profiles = profile2_load_by_user($account, NULL);
  return reset($profiles) ?: NULL;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.