entity_translation_search_api.module 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. define('ENTITY_TRANSLATION_SEARCH_API_NO_TRANSLATION', 'none');
  3. /**
  4. * Implements hook_search_api_search_api_item_type_info_alter().
  5. */
  6. function entity_translation_search_api_search_api_item_type_info_alter(array &$infos) {
  7. foreach ($infos as $type => $info) {
  8. if (entity_get_info($type)) {
  9. $infos[$type]['datasource controller'] = 'EntityTranslationDataSourceController';
  10. }
  11. }
  12. }
  13. /**
  14. * Implements hook_form_alter().
  15. */
  16. function entity_translation_search_api_form_alter(&$form, &$form_state, $form_id) {
  17. if (in_array($form_id, array(
  18. 'search_api_admin_add_index',
  19. 'search_api_admin_index_edit',
  20. ))) {
  21. if ($form_id == 'search_api_admin_index_edit') {
  22. $index = $form_state['index'];
  23. $default = isset($index->options['entity_translation_language']) ? $index->options['entity_translation_language'] : ENTITY_TRANSLATION_SEARCH_API_NO_TRANSLATION;
  24. }
  25. else {
  26. $default = ENTITY_TRANSLATION_SEARCH_API_NO_TRANSLATION;
  27. }
  28. $languages = language_list('enabled');
  29. $languages = $languages[1];
  30. $names = array(ENTITY_TRANSLATION_SEARCH_API_NO_TRANSLATION => t('No translation'));
  31. foreach ($languages as $langcode => $item) {
  32. $name = t($item->name);
  33. $names[$langcode] = $name . ($item->native != $name ? ' (' . $item->native . ')' : '');
  34. }
  35. $form['options']['entity_translation_language'] = array(
  36. '#type' => (count($names) <= 5 ? 'radios' : 'select'),
  37. '#title' => t('Entity translation language'),
  38. '#description' => t('To which language the entities should be translated before indexing.'),
  39. '#default_value' => $default,
  40. '#options' => $names,
  41. );
  42. }
  43. }
  44. /**
  45. * Implements hook_search_api_index_items_alter().
  46. * For compatibility with language control data alteration.
  47. */
  48. function entity_translation_search_api_search_api_index_items_alter(array &$items, SearchApiIndex $index) {
  49. $target_language = $index->options['entity_translation_language'];
  50. if ($target_language != ENTITY_TRANSLATION_SEARCH_API_NO_TRANSLATION) {
  51. foreach ($items as $id => $item) {
  52. if (isset($item->translations->data[$target_language])) {
  53. $item->language = $target_language;
  54. }
  55. }
  56. }
  57. }