You are here

public function ContentEntitySource::getData in Translation Management Tool 8

Implements TMGMTEntitySourcePluginController::getData().

Returns the data from the fields as a structure that can be processed by the Translation Management system.

Overrides SourcePluginInterface::getData

File

sources/content/src/Plugin/tmgmt/Source/ContentEntitySource.php, line 165

Class

ContentEntitySource
Content entity source plugin controller.

Namespace

Drupal\tmgmt_content\Plugin\tmgmt\Source

Code

public function getData(JobItemInterface $job_item) {
  $langcode = $job_item
    ->getJob() ? $job_item
    ->getJob()
    ->getSourceLangcode() : NULL;
  $entity = static::load($job_item
    ->getItemType(), $job_item
    ->getItemId(), $langcode);
  if (!$entity) {
    throw new TMGMTException(t('Unable to load entity %type with id %id', array(
      '%type' => $job_item
        ->getItemType(),
      '%id' => $job_item
        ->getItemId(),
    )));
  }
  $languages = \Drupal::languageManager()
    ->getLanguages();
  $id = $entity
    ->language()
    ->getId();
  if (!isset($languages[$id])) {
    throw new TMGMTException(t('Entity %entity could not be translated because the language %language is not applicable', array(
      '%entity' => $entity
        ->language()
        ->getId(),
      '%language' => $entity
        ->language()
        ->getName(),
    )));
  }
  if (!$entity
    ->hasTranslation($job_item
    ->getJob()
    ->getSourceLangcode())) {
    throw new TMGMTException(t('The %type entity %id with translation %lang does not exist.', array(
      '%type' => $entity
        ->getEntityTypeId(),
      '%id' => $entity
        ->id(),
      '%lang' => $job_item
        ->getJob()
        ->getSourceLangcode(),
    )));
  }
  $translation = $entity
    ->getTranslation($job_item
    ->getJob()
    ->getSourceLangcode());
  $data = $this
    ->extractTranslatableData($translation);
  $entity_form_display = \Drupal::service('entity_display.repository')
    ->getFormDisplay($job_item
    ->getItemType(), $entity
    ->bundle(), 'default');
  uksort($data, function ($a, $b) use ($entity_form_display) {
    $a_weight = NULL;
    $b_weight = NULL;

    // Get the weights.
    if ($entity_form_display
      ->getComponent($a) && !is_null($entity_form_display
      ->getComponent($a)['weight'])) {
      $a_weight = (int) $entity_form_display
        ->getComponent($a)['weight'];
    }
    if ($entity_form_display
      ->getComponent($b) && !is_null($entity_form_display
      ->getComponent($b)['weight'])) {
      $b_weight = (int) $entity_form_display
        ->getComponent($b)['weight'];
    }

    // If neither field has a weight, sort alphabetically.
    if ($a_weight === NULL && $b_weight === NULL) {
      return $a > $b ? 1 : -1;
    }
    elseif ($a_weight === NULL) {
      return 1;
    }
    elseif ($b_weight === NULL) {
      return -1;
    }
    elseif ($a_weight == $b_weight) {
      return 0;
    }
    else {
      return $a_weight > $b_weight ? 1 : -1;
    }
  });
  return $data;
}