- 🇩🇪Germany mkalkbrenner 🇩🇪
Meanwhile, there's a module:
https://www.drupal.org/project/search_api_clir →
In a project, we have the requirement that users enter a search term in any language and then have matching results displayed in the selected interface language.
Concrete example:
1) We assume there is a record, e.g. a book that has the title "African Artworks" tagged with the keyword "Africa".
2) We further assume that this record is translated, e.g. in french, with the title "Oeuvres Africaines" and the keyword "Afrique".
3) We assume that a user has set the interface language to "english" and is now searching for the term "Afrique".
Desired behavior:
4.1) The record from 1) is returned. It is found due to the fact that "Afrique" is the translation of "Africa".
Furthermore, the user is shown the record in the english version (not french!), since this is the current interface language, i.e. the result contains the book with title "African Artworks" and keyword "Africa".
Current behavior:
4.2.) The search returns an empty result set, because no matches were found in the records for the currently set interface language (english).
We realized the desired behaviour (4.1.) by modifying the function "addFieldValues()" in the RenderedItem.php (search_api/src/Plugin/search_api/processor) as follows:
public function addFieldValues(ItemInterface $item) {
[...]
$datasource_id = $item->getDatasourceId();
$datasource = $item->getDatasource();
$bundle = $datasource->getItemBundle($item->getOriginalObject());
// When no view mode has been set for the bundle, or it has been set to
// "Don't include the rendered item", skip this item.
if (empty($configuration['view_mode'][$datasource_id][$bundle])) {
// If it was really not set, also notify the user through the log.
if (!isset($configuration['view_mode'][$datasource_id][$bundle])) {
$unset_view_modes[$field->getFieldIdentifier()] = $field->getLabel();
}
continue;
}
$view_mode = (string) $configuration['view_mode'][$datasource_id][$bundle];
<strong>// from here, the suggested modification begins
try {
// we consider only two languages in our scenario; TODO: the available languages have to be identified automatically
$languages = ['en', 'fr'];
$value = "";
foreach($languages as $language){
$build = $datasource->viewItem($item->getOriginalObject(), $view_mode, $language);
if ($build) {
// Add the excerpt to the render array to allow adding it to view modes.
$build['#search_api_excerpt'] = $item->getExcerpt();
$value .= (string) $this->getRenderer()->renderPlain($build);
}
if (!empty($value)) {
$field->addValue($value);
}
}
}</strong>
catch (\Exception $e) {[...]}
[...]
}
Is there another way to implement my requirement? Or in case it is already implemented somehow: Which configuration do I miss in the module?
Thank you so much in advance!
Active
1.0
General code
Not all content is available!
It's likely this issue predates Contrib.social: some issue and comment data are missing.
Meanwhile, there's a module:
https://www.drupal.org/project/search_api_clir →