You are here

protected function language_hierarchy_i18n_string_textgroup_default::multiple_translation_load in Language Hierarchy 7

Load multiple translations from db

@todo Optimize when we've already got the source object

Parameters

$conditions: Array of field values to use as query conditions.

$langcode: Language code to search.

$index: Field to use as index for the result.

Return value

array Array of string objects with translation set.

Overrides i18n_string_textgroup_default::multiple_translation_load

File

includes/language_hierarchy_i18n_string_textgroup_default.class.inc, line 56

Class

language_hierarchy_i18n_string_textgroup_default
Extends the default i18n string textgroup class providing language fallback.

Code

protected function multiple_translation_load($conditions, $langcode) {
  $conditions += array(
    'language' => $langcode,
    'textgroup' => $this->textgroup,
  );
  if (is_array($conditions['language'])) {
    if (count($conditions['language']) !== 1) {

      // This should never happen, but if it does, we don't know what to do.
      return parent::multiple_translation_load($conditions, $langcode);
    }
    $conditions['language'] = reset($conditions['language']);
  }
  $chain = language_hierarchy_get_ancestors($conditions['language']);
  if (empty($chain)) {
    return parent::multiple_translation_load($conditions, $langcode);
  }
  $conditions['language'] = array_values(array_merge(array(
    $conditions['language'],
  ), array_keys($chain)));
  $query = db_select('i18n_string', 's')
    ->fields('s');
  $query
    ->leftJoin('locales_target', 't', 's.lid = t.lid');
  $query
    ->fields('t', array(
    'translation',
    'language',
    'i18n_status',
  ));
  foreach ($conditions as $field => $value) {
    if (is_array($value) && count($value) == 1) {
      $value = reset($value);
    }
    if ($value === '*') {
      continue;
    }
    elseif ($field == 'language') {
      $query
        ->condition('t.language', $value);
    }
    else {
      $query
        ->condition('s.' . $field, $value);
    }
  }

  // The i18n module can save empty strings as translations. In this case, it
  // behaves as there is no translation and uses the original strings.
  // Handle this case by simple excluding of empty translations.
  $query
    ->condition('t.translation', '', '<>');

  // Sort results according to the language chain order.
  $order = '(CASE ';
  foreach ($conditions['language'] as $index => $lang) {
    $order .= "WHEN t.language = '{$lang}' THEN {$index} ";
  }
  $order .= 'END)';
  $query
    ->orderBy($order);

  // The query now is ordered by language, but we will need to use the GROUP
  // BY operator which is applied *before* the ORDER BY, so in the end we may
  // have wrong translations selected. To ovid this, do the trick with
  // subquery, so we have the correct grouping.
  $main_query = db_select($query, 'main')
    ->fields('main')
    ->groupBy('main.lid');
  return $this
    ->multiple_translation_build($main_query
    ->execute()
    ->fetchAll(), $langcode);
}