1

I have a field that concatenates multiple taxonomomy terms into a single text field. I have altered the existing module concat_field.

This all works fine but I cannot get search API to recognize the field (it worked with an older version of search api)

I have added this to the module:

function concat_field_property_info_callback(&$info, $entity_type, $field, $instance, $field_type) {
  $property = &$info[$entity_type]['bundles'][$instance['bundle']]['properties'][$field['field_name']];

  $property['type'] ='text'; //tried concat_field as well which is the type in concat_field_field_info
  $property['getter callback'] = 'entity_metadata_field_verbatim_get';
  $property['setter callback'] = 'entity_metadata_field_verbatim_set';
  $property['label'] = 'Concat field';
  $property['description'] = 'concatenated field';   
  unset($property['query callback']);
}

Any clue what i am doing wrong?

0

I think you need to follow following approach:

 /**
 * Implements hook_entity_property_info_alter().
 */
function modulename_property_info_alter(&$info) {
  $info['node']['properties']['concat_field'] = array(
    'type' => 'text',
    'label' => t('Concatenated form of all the taxonomies that has been added to the node'),
    'sanitized' => TRUE,
    'getter callback' => 'modulename_search_api_property_concat_field_callback',
  );
}

/**
 * Getter callback for concatenate terms field.
 */
function modulename_search_api_property_concat_field_callback($item) {
  /** Do all your processing/functionality here. **/
  return <resultant concatenated value>;
}
| improve this answer | |
  • I actually tried that, I also found in the search.api.php that its possible to register a new type. As described here: *** Define new data types for indexed properties. function concat_field_search_api_data_type_info() { return array( 'concat_field' => array( 'name' => t('Concat Field'), 'fallback' => 'string', ), ); } However still no luck, and it really seems that that is the way? – justme777 Nov 26 '15 at 9:18
  • Please check this: dropbucket.org/node/614 . And you need to use hook_property_info_alter(). – Dev Nov 26 '15 at 9:33
  • yea that one doesn't work anymore (I had this working with old search api) – justme777 Nov 26 '15 at 10:25
0

I actually tried that, I also found in the search.api.php that its possible to register a new type.

As described here:

 *** Define new data types for indexed properties.

 function concat_field_search_api_data_type_info() {
     return array(
       'concat_field' => array(
       'name' => t('Concat Field'),
       'fallback' => 'string',
       ),
   );
 }

However still no luck, and it really seems that that is the way?

| improve this answer | |
0

Ok, I should have looked better at contrib modules, If anyone needs to achieve something similar, look into the computed field module. I just made a small custom module that creates a computed concatenated field. I used taxonomy terms, but you can concatenate any field.

My code:

 /**
computed_address is field name used by computed_field module, you can use any field that is created with computed_field module
*/

function computed_field_field_computed_address_compute(&$entity_field, $entity_type, $entity, $field, $instance, $langcode, $items) {

  $taxonomyterms = array('field_province', 'field_city', 'field_street'); //fields machinenames to concatenate
  $result = array();

  foreach ($taxonomyterms as $taxonomyterm) {

      $field_myterms = field_get_items($entity_type, $entity, $taxonomyterm);

            if (!empty($field_myterms)) {
                $term = taxonomy_term_load($field_myterms[0]['tid']);
            }

      $result[] = $term->name;
    }

    $entity_field[0]['value'] = implode(', ', $result);
}

function computed_field_computed_address_display($field, $entity_field_item, $entity_lang, $langcode, $entity) {
  return $entity_field_item['value'];
}

Maybe this will help anyone else...

| improve this answer | |

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.