You are here

function synonyms_search_node_update_index in Synonyms 7

Implements hook_node_update_index().

File

synonyms_search/synonyms_search.module, line 51
Provides synonyms integration with searching.

Code

function synonyms_search_node_update_index($node) {
  $output = array();
  foreach (field_info_instances('node', $node->type) as $instance) {

    // We go a field by field looking for taxonomy term reference or entity
    // reference of taxonomy term type and if that vocabulary has enabled search
    // synonyms, we add them to the search index.
    // TODO: implement this through foreign keys information. See
    // term_merge_fields_with_foreign_key() function.
    $field_info = field_info_field($instance['field_name']);
    if ($field_info['type'] == 'taxonomy_term_reference') {
      $terms = field_get_items('node', $node, $instance['field_name']);
      if (is_array($terms) && !empty($terms)) {
        foreach ($terms as $v) {
          $output[] = $v['tid'];
        }
      }
    }
    if ($field_info['type'] == 'entityreference' && $field_info['settings']['target_type'] == 'taxonomy_term') {
      $terms = field_get_items('node', $node, $instance['field_name']);
      if (is_array($terms) && !empty($terms)) {
        foreach ($terms as $v) {
          $output[] = $v['target_id'];
        }
      }
    }
  }
  if (!empty($output)) {
    $terms = taxonomy_term_load_multiple($output);
    $output = array();
    foreach ($terms as $term) {
      $bundle = field_extract_bundle('taxonomy_term', $term);
      $behavior_implementations = synonyms_behavior_get('search', 'taxonomy_term', $bundle, TRUE);
      foreach ($behavior_implementations as $implementation) {
        $output = array_merge($output, $implementation['object']
          ->extractSynonyms($term));
      }
    }
  }
  return empty($output) ? '' : '<strong>' . implode($output, ', ') . '</strong>';
}