diff --git a/sites/all/modules/sandbox/entity_translation_search_api/README.txt b/sites/all/modules/sandbox/entity_translation_search_api/README.txt new file mode 100644 index 00000000..6f2a6c71 --- /dev/null +++ b/sites/all/modules/sandbox/entity_translation_search_api/README.txt @@ -0,0 +1,8 @@ +This module differs from this one: http://drupal.org/project/search_api_et. It +uses an alternative way of getting language aware search with entity translation: +have one index per language. This has some drawbacks like scalability and site +building usability. But when used with Solr as indexing backend, it allows to set +a different configuration for each language (by declaring one Search API server +per language, matching to a different Solr core). This especially makes sense if +the website only has two or three languages. +For more info on these considerations, see http://drupal.org/node/1393058. diff --git a/sites/all/modules/sandbox/entity_translation_search_api/entity_translation_search_api.info b/sites/all/modules/sandbox/entity_translation_search_api/entity_translation_search_api.info new file mode 100644 index 00000000..80022a7b --- /dev/null +++ b/sites/all/modules/sandbox/entity_translation_search_api/entity_translation_search_api.info @@ -0,0 +1,6 @@ +name = Entity translation search API +description = Allows to set several monolingual search indexes. +package = Search +core = 7.x + +files[] = includes/datasource_translated_entity.inc diff --git a/sites/all/modules/sandbox/entity_translation_search_api/entity_translation_search_api.module b/sites/all/modules/sandbox/entity_translation_search_api/entity_translation_search_api.module new file mode 100644 index 00000000..971f770e --- /dev/null +++ b/sites/all/modules/sandbox/entity_translation_search_api/entity_translation_search_api.module @@ -0,0 +1,61 @@ + $info) { + if (entity_get_info($type)) { + $infos[$type]['datasource controller'] = 'EntityTranslationDataSourceController'; + } + } +} + +/** + * Implements hook_form_alter(). + */ +function entity_translation_search_api_form_alter(&$form, &$form_state, $form_id) { + if (in_array($form_id, array( + 'search_api_admin_add_index', + 'search_api_admin_index_edit', + ))) { + if ($form_id == 'search_api_admin_index_edit') { + $index = $form_state['index']; + $default = isset($index->options['entity_translation_language']) ? $index->options['entity_translation_language'] : ENTITY_TRANSLATION_SEARCH_API_NO_TRANSLATION; + } + else { + $default = ENTITY_TRANSLATION_SEARCH_API_NO_TRANSLATION; + } + $languages = language_list('enabled'); + $languages = $languages[1]; + $names = array(ENTITY_TRANSLATION_SEARCH_API_NO_TRANSLATION => t('No translation')); + foreach ($languages as $langcode => $item) { + $name = t($item->name); + $names[$langcode] = $name . ($item->native != $name ? ' (' . $item->native . ')' : ''); + } + $form['options']['entity_translation_language'] = array( + '#type' => (count($names) <= 5 ? 'radios' : 'select'), + '#title' => t('Entity translation language'), + '#description' => t('To which language the entities should be translated before indexing.'), + '#default_value' => $default, + '#options' => $names, + ); + } +} + +/** + * Implements hook_search_api_index_items_alter(). + * For compatibility with language control data alteration. + */ +function entity_translation_search_api_search_api_index_items_alter(array &$items, SearchApiIndex $index) { + $target_language = $index->options['entity_translation_language']; + if ($target_language != ENTITY_TRANSLATION_SEARCH_API_NO_TRANSLATION) { + foreach ($items as $id => $item) { + if (isset($item->translations->data[$target_language])) { + $item->language = $target_language; + } + } + } +} diff --git a/sites/all/modules/sandbox/entity_translation_search_api/includes/datasource_translated_entity.inc b/sites/all/modules/sandbox/entity_translation_search_api/includes/datasource_translated_entity.inc new file mode 100644 index 00000000..91b904f0 --- /dev/null +++ b/sites/all/modules/sandbox/entity_translation_search_api/includes/datasource_translated_entity.inc @@ -0,0 +1,43 @@ +options['entity_translation_language']; + if ($target_language != ENTITY_TRANSLATION_SEARCH_API_NO_TRANSLATION) { + // This may not be really needed. + $info['langcode'] = $target_language; + $wrapper = parent::getMetadataWrapper($item, $info); + $wrapper->language($target_language); + + $properties = $wrapper->getPropertyInfo(); + foreach ($properties as $key=>$property) { + if (isset($property['type']) && $property['type'] == 'taxonomy_term') { + $wrapper->$key->language($target_language); + } + } + + return $wrapper; + } + } + return parent::getMetadataWrapper($item, $info); + } + +}