public function ContentEntitySourcePluginUi::overviewRow

Builds a table row for overview form.

Parameters

array ContentEntityInterface $entity: Data needed to build the list row.

array $bundles: The array of bundles.

Return value

array

1 call to ContentEntitySourcePluginUi::overviewRow()
ContentEntitySourcePluginUi::overviewForm in sources/content/src/ContentEntitySourcePluginUi.php
Builds the overview form for the source entities.

File

sources/content/src/ContentEntitySourcePluginUi.php, line 138

Class

ContentEntitySourcePluginUi
Content entity source plugin UI.

Namespace

Drupal\tmgmt_content

Code

public function overviewRow(ContentEntityInterface $entity, array $bundles) {
  $entity_label = $entity
    ->label();
  $storage = \Drupal::entityTypeManager()
    ->getStorage($entity
    ->getEntityTypeId());
  $use_latest_revisions = $entity
    ->getEntityType()
    ->isRevisionable() && ContentTranslationManager::isPendingRevisionSupportEnabled($entity
    ->getEntityTypeId(), $entity
    ->bundle());

  // Get the default revision.
  $default_revision = $use_latest_revisions ? $storage
    ->load($entity
    ->id()) : $entity;

  // Get existing translations and current job items for the entity
  // to determine translation statuses
  $translations = $entity
    ->getTranslationLanguages();
  $source_lang = $entity
    ->language()
    ->getId();
  $current_job_items = tmgmt_job_item_load_latest('content', $entity
    ->getEntityTypeId(), $entity
    ->id(), $source_lang);
  $row = array(
    'id' => $entity
      ->id(),
  );
  if (count($bundles) > 1) {
    $row['bundle'] = isset($bundles[$entity
      ->bundle()]) ? $bundles[$entity
      ->bundle()] : t('Unknown');
  }

  // Load entity translation specific data.
  $manager = \Drupal::service('content_translation.manager');
  foreach (\Drupal::languageManager()
    ->getLanguages() as $langcode => $language) {

    // @see Drupal\content_translation\Controller\ContentTranslationController::overview()
    // If the entity type is revisionable, we may have pending revisions
    // with translations not available yet in the default revision. Thus we
    // need to load the latest translation-affecting revision for each
    // language to be sure we are listing all available translations.
    if ($use_latest_revisions) {
      $entity = $default_revision;
      $latest_revision_id = $storage
        ->getLatestTranslationAffectedRevisionId($entity
        ->id(), $langcode);
      if ($latest_revision_id) {

        /** @var \Drupal\Core\Entity\ContentEntityInterface $latest_revision */
        $latest_revision = $storage
          ->loadRevision($latest_revision_id);

        // Make sure we do not list removed translations, i.e. translations
        // that have been part of a default revision but no longer are.
        if (!$latest_revision
          ->wasDefaultRevision() || $default_revision
          ->hasTranslation($langcode)) {
          $entity = $latest_revision;

          // Update the label if we are dealing with the source language.
          if ($langcode === $source_lang) {
            $entity_label = $entity
              ->label();
          }
        }
      }
      $translations = $entity
        ->getTranslationLanguages();
    }
    $translation_status = 'current';
    if ($langcode == $source_lang) {
      $translation_status = 'original';
    }
    elseif (!isset($translations[$langcode])) {
      $translation_status = 'missing';
    }
    elseif ($translation = $entity
      ->getTranslation($langcode)) {
      $metadata = $manager
        ->getTranslationMetadata($translation);
      if ($metadata
        ->isOutdated()) {
        $translation_status = 'outofdate';
      }
    }
    $build = $this
      ->buildTranslationStatus($translation_status, isset($current_job_items[$langcode]) ? $current_job_items[$langcode] : NULL);
    if ($translation_status != 'missing' && $entity
      ->hasLinkTemplate('canonical')) {
      $build['source'] = [
        '#type' => 'link',
        '#url' => $entity
          ->toUrl('canonical', [
          'language' => $language,
        ]),
        '#title' => $build['source'],
      ];
    }
    $row['langcode-' . $langcode] = [
      'data' => \Drupal::service('renderer')
        ->render($build),
      'class' => array(
        'langstatus-' . $langcode,
      ),
    ];
  }
  $label = $entity_label ?: $this
    ->t('@type: @id', [
    '@type' => $entity
      ->getEntityTypeId(),
    '@id' => $entity
      ->id(),
  ]);
  $row['title'] = $entity
    ->hasLinkTemplate('canonical') ? $entity
    ->toLink($label, 'canonical')
    ->toString() : ($entity_label ?: $entity
    ->id());
  return $row;
}