protected function SearchApiEtDatasourceController::setLanguage in Search API Entity Translation 7.2
Sets language of specific fields on an EntityMetadataWrapper object.
This is essentially a copy of search_api_extract_fields(), just slightly adapted to set language on the wrapper fields instead of extracting them.
Parameters
EntityMetadataWrapper $wrapper: The wrapper on which fields to set language on.
array $fields: The fields to set language on, as stored in an index. I.e., the array keys are field names, the values are arrays with at least a "type" key present.
array $langcode: A code of the language to set to wrapper fields.
Return value
array The $fields array with additional "value" and "original_type" keys set.
See also
SearchApiEtDatasourceController::getMetadataWrapper()
SearchApiEtDatasourceController::setLanguage()
1 call to SearchApiEtDatasourceController::setLanguage()
- SearchApiEtDatasourceController::getMetadataWrapper in includes/
SearchApiEtDatasourceController.php - Creates a metadata wrapper for this datasource controller's type.
File
- includes/
SearchApiEtDatasourceController.php, line 147 - Contains the SearchApiEtDatasourceController class.
Class
- SearchApiEtDatasourceController
- Provides multilingual versions of all entity types.
Code
protected function setLanguage($wrapper, $fields, $langcode) {
// If $wrapper is a list of entities, we have to aggregate their field values.
$wrapper_info = $wrapper
->info();
if (search_api_is_list_type($wrapper_info['type'])) {
foreach ($fields as &$info) {
$info['value'] = array();
$info['original_type'] = $info['type'];
}
unset($info);
try {
foreach ($wrapper as $w) {
$nested_fields = $this
->setLanguage($w, $fields, $langcode);
foreach ($nested_fields as $field => $info) {
if (isset($info['value'])) {
$fields[$field]['value'][] = $info['value'];
}
if (isset($info['original_type'])) {
$fields[$field]['original_type'] = $info['original_type'];
}
}
}
} catch (EntityMetadataWrapperException $e) {
// Catch exceptions caused by not set list values.
}
return $fields;
}
$nested = array();
foreach ($fields as $field => $info) {
$pos = strpos($field, ':');
if ($pos === FALSE) {
if (isset($wrapper->{$field}) && method_exists($wrapper->{$field}, 'language')) {
$wrapper->{$field}
->language($langcode);
}
}
else {
list($prefix, $key) = explode(':', $field, 2);
$nested[$prefix][$key] = $info;
}
}
foreach ($nested as $prefix => $nested_fields) {
if (isset($wrapper->{$prefix})) {
$nested_fields = $this
->setLanguage($wrapper->{$prefix}, $nested_fields, $langcode);
foreach ($nested_fields as $field => $info) {
$fields["{$prefix}:{$field}"] = $info;
}
}
else {
foreach ($nested_fields as &$info) {
$info['value'] = NULL;
$info['original_type'] = $info['type'];
}
}
}
return $fields;
}