merged entity_translation_search_api submodule

This commit is contained in:
Bachir Soussi Chiadmi 2015-04-19 16:29:28 +02:00
commit 7b6bd36fea
4 changed files with 118 additions and 0 deletions

View File

@ -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.

View File

@ -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

View File

@ -0,0 +1,61 @@
<?php
define('ENTITY_TRANSLATION_SEARCH_API_NO_TRANSLATION', 'none');
/**
* Implements hook_search_api_search_api_item_type_info_alter().
*/
function entity_translation_search_api_search_api_item_type_info_alter(array &$infos) {
foreach ($infos as $type => $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;
}
}
}
}

View File

@ -0,0 +1,43 @@
<?php
class EntityTranslationDataSourceController extends SearchApiEntityDataSourceController {
/**
* Get a metadata wrapper for the item type of this data source controller.
*
* @param $item
* Unless NULL, an item of the item type for this controller to be wrapped.
* @param array $info
* Optionally, additional information that should be used for creating the
* wrapper. Uses the same format as entity_metadata_wrapper().
*
* @return EntityMetadataWrapper
* A wrapper for the item type of this data source controller, according to
* the info array, and optionally loaded with the given data.
*
* @see entity_metadata_wrapper()
*/
public function getMetadataWrapper($item = NULL, array $info = array()) {
if (is_array($info['property info alter'])) {
$index = reset($info['property info alter']);
$target_language = $index->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);
}
}