protected function LocaleSource::getLocaleObject

Helper function to obtain a locale object for given job item.

Parameters

\Drupal\tmgmt\JobItemInterface $job_item:

Return value

locale object

1 call to LocaleSource::getLocaleObject()
LocaleSource::getData in sources/locale/src/Plugin/tmgmt/Source/LocaleSource.php
Returns an array with the data structured for translation.

File

sources/locale/src/Plugin/tmgmt/Source/LocaleSource.php, line 93

Class

LocaleSource
Translation Source plugin for locale strings.

Namespace

Drupal\tmgmt_locale\Plugin\tmgmt\Source

Code

protected function getLocaleObject(JobItemInterface $job_item) {
  $locale_lid = $job_item
    ->getItemId();

  // Check existence of assigned lid.
  $exists = \Drupal::database()
    ->query("SELECT COUNT(lid) FROM {locales_source} WHERE lid = :lid", array(
    ':lid' => $locale_lid,
  ))
    ->fetchField();
  if (!$exists) {
    throw new TMGMTException(t('Unable to load locale with id %id', array(
      '%id' => $job_item
        ->getItemId(),
    )));
  }

  // This is necessary as the method is also used in the getLabel() callback
  // and for that case the job is not available in the cart.
  if ($job_item
    ->getJobId()) {
    $source_language = $job_item
      ->getJob()
      ->getSourceLangcode();
  }
  else {
    $source_language = $job_item
      ->getSourceLangCode();
  }
  if ($source_language == 'en') {
    $query = \Drupal::database()
      ->select('locales_source', 'ls');
    $query
      ->fields('ls')
      ->condition('ls.lid', $locale_lid);
    $locale_object = $query
      ->execute()
      ->fetchObject();
    $locale_object->language = 'en';
    if (empty($locale_object)) {
      return NULL;
    }
    $locale_object->origin = 'source';
  }
  else {
    $query = \Drupal::database()
      ->select('locales_target', 'lt');
    $query
      ->join('locales_source', 'ls', 'ls.lid = lt.lid');
    $query
      ->fields('lt')
      ->fields('ls')
      ->condition('lt.lid', $locale_lid)
      ->condition('lt.language', $source_language);
    $locale_object = $query
      ->execute()
      ->fetchObject();
    if (empty($locale_object)) {
      return NULL;
    }
    $locale_object->origin = 'target';
  }
  return $locale_object;
}