You are here

protected function StringDatabaseStorageDecorator::dbStringSelect in Language Hierarchy 2.x

Builds a SELECT query with multiple conditions and fields.

The query uses both 'locales_source' and 'locales_target' tables. Note that by default, as we are selecting both translated and untranslated strings target field's conditions will be modified to match NULL rows too.

Parameters

array $conditions: An associative array with field => value conditions that may include NULL values. If a language condition is included it will be used for joining the 'locales_target' table.

array $options: An associative array of additional options. It may contain any of the options used by Drupal\locale\StringStorageInterface::getStrings() and these additional ones:

  • 'translation', Whether to include translation fields too. Defaults to FALSE.

Return value

\Drupal\Core\Database\Query\Select Query object with all the tables, fields and conditions.

Overrides StringDatabaseStorage::dbStringSelect

File

src/StringDatabaseStorageDecorator.php, line 90

Class

StringDatabaseStorageDecorator
Decorates the locale.storage service.

Namespace

Drupal\language_hierarchy

Code

protected function dbStringSelect(array $conditions, array $options = []) {
  $query = $this->stringStorage
    ->dbStringSelect($conditions, $options);

  // The 'translated' meta-condition bypasses fallbacks, since it is used for
  // querying specifically for a given language.
  if (!isset($conditions['translated'])) {
    $tables =& $query
      ->getTables();
    if (isset($tables['t']['arguments'][':langcode'])) {
      $attempted_langcode = $tables['t']['arguments'][':langcode'];

      // Retrieve the language fallback list.
      $candidates = array_values($this->languageManager
        ->getFallbackCandidates([
        'langcode' => $attempted_langcode,
        'operation' => 'locale_lookup',
      ]));
      if ($candidates) {
        $candidates = array_merge([
          $attempted_langcode,
        ], $candidates);

        // Modify langcode to be an array instead of a single value.
        $tables['t']['condition'] = "t.lid = s.lid AND t.language IN (:langcode[])";
        unset($tables['t']['arguments'][':langcode']);
        $tables['t']['arguments'][':langcode[]'] = $candidates;

        // Build custom sort criteria.
        $query
          ->addJoin('LEFT', 'language_hierarchy_priority', 'lhp', 't.language = lhp.langcode');
        $query
          ->orderBy('lhp.priority', 'DESC');
      }
    }
  }
  return $query;
}