You are here

public function LocalTranslator::getSupportedLanguagePairs in Translation Management Tool 8

Default implementation that gets target languages for each remote language. This approach is ineffective and therefore it is advised that a plugin should provide own implementation.

Overrides TranslatorPluginBase::getSupportedLanguagePairs

File

translators/tmgmt_local/src/Plugin/tmgmt/Translator/LocalTranslator.php, line 84

Class

LocalTranslator
Drupal user provider.

Namespace

Drupal\tmgmt_local\Plugin\tmgmt\Translator

Code

public function getSupportedLanguagePairs(TranslatorInterface $translator) {
  if (!empty($this->language_pairs)) {
    return $this->language_pairs;
  }
  $roles = user_roles(TRUE, 'provide translation services');
  $query = \Drupal::database()
    ->select('field_data_tmgmt_translation_skills', 'ts');
  $query
    ->join('users', 'u', 'u.uid = ts.entity_id AND u.status = 1');
  $query
    ->addField('ts', 'tmgmt_translation_skills_language_from', 'source_language');
  $query
    ->addField('ts', 'tmgmt_translation_skills_language_to', 'target_language');
  $query
    ->condition('ts.deleted', 0);
  $query
    ->condition('ts.entity_type', 'user');
  if (!in_array(AccountInterface::AUTHENTICATED_ROLE, array_keys($roles))) {
    $query
      ->join('users_roles', 'ur', 'ur.uid = u.uid AND ur.rid');
    $or_conditions = (new Condition('OR'))
      ->condition('ur.rid', array_keys($roles), 'IN')
      ->condition('u.uid', 1);
    $query
      ->condition($or_conditions);
  }
  foreach ($query
    ->execute()
    ->fetchAll() as $item) {
    $this->language_pairs[] = array(
      'source_language' => $item->source_language,
      'target_language' => $item->target_language,
    );
  }
  return $this->language_pairs;
}