search_api_et.module 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. /**
  3. * @file
  4. * Offers simple support for search api for searching in multilingual nodes
  5. * translated with the entity translation module.
  6. *
  7. * ToDos:
  8. * - Make different language versions of a content distinguishable for search.
  9. */
  10. /**
  11. * Implements hook_entity_property_info_alter().
  12. *
  13. * Adds entity property for handling
  14. * field-translation based multi-language indexing
  15. */
  16. function search_api_et_entity_property_info_alter(&$info) {
  17. foreach (search_api_et_entity_types() as $etype => $einfo) {
  18. $info[$etype]['properties']['unified_fulltext'] = array(
  19. 'type' => 'text',
  20. 'label' => t('Multilingual full text (all languages via entity translation)'),
  21. 'sanitized' => TRUE,
  22. 'getter callback' => 'search_api_et_fulltext_get',
  23. );
  24. }
  25. }
  26. /**
  27. * Getter callback for fulltext property.
  28. *
  29. * Concatenate the entity rendering for all its language versions/translations.
  30. */
  31. function search_api_et_fulltext_get($item, $options, $name, $type, $info) {
  32. $view_mode = variable_get('search_api_et_fulltext_get_viewmode_' . $type, 'search_index');
  33. // If no translations are managed for this entity (i.e. ET is not activated),
  34. // do nothing.
  35. if (!is_array($item->translations->data)) {
  36. return;
  37. }
  38. // Iterate through the $item->translations to render the entity in each lang.
  39. $fulltext = '';
  40. foreach ($item->translations->data as $langcode => $translation) {
  41. if ($translation['status']) {
  42. $render = entity_view($type, array($item), $view_mode, $langcode);
  43. $context = array('item' => $item, 'options' => $options, 'name' => $name, 'type' => $type, 'view_mode' => $view_mode, 'language' => $langcode);
  44. // Invoke some hooks, before or after the rendering, for alterations.
  45. drupal_alter('search_api_et_fulltext_prerender', $render, $context);
  46. $render = drupal_render($render);
  47. drupal_alter('search_api_et_fulltext_postrender', $render, $context);
  48. $fulltext .= $render;
  49. }
  50. }
  51. // watchdog('search_api_et', $fulltext, null, WATCHDOG_INFO);
  52. return $fulltext;
  53. }
  54. /**
  55. * Implements hook_entity_translation_save().
  56. *
  57. * Trigger re-indexing of content when translation changes.
  58. */
  59. function search_api_et_entity_translation_save($entity_type, $entity, $langcode) {
  60. search_api_entity_update($entity, $entity_type);
  61. }
  62. /**
  63. * Implements hook_entity_info_alter().
  64. *
  65. * (re-) declare search_index view mode
  66. * (which is not available when core search is disabled (as it can be here)
  67. */
  68. function search_api_et_entity_info_alter(&$entity_info) {
  69. foreach (search_api_et_entity_types() as $etype => $einfo) {
  70. $entity_info[$etype]['view modes']['search_index']
  71. = array(
  72. 'label' => t('Search index'),
  73. 'custom settings' => FALSE,
  74. );
  75. }
  76. }
  77. /**
  78. * Helper function to list all entity types supported by entity translation.
  79. *
  80. */
  81. function search_api_et_entity_types() {
  82. $entity_types = array();
  83. foreach (entity_get_info() as $entity_type => $entity_data) {
  84. if (entity_translation_enabled($entity_type)) {
  85. $entity_types[$entity_type] = $entity_data;
  86. }
  87. }
  88. return $entity_types;
  89. }
  90. /* END OF FILE marker for fewer merge conflicts. */