You are here

public function TranslationModerationState::render in Translation Views 8

Renders the field.

Parameters

\Drupal\views\ResultRow $values: The values retrieved from a single row of a view's query result.

Return value

string|\Drupal\Component\Render\MarkupInterface The rendered output. If the output is safe it will be wrapped in an object that implements MarkupInterface. If it is empty or unsafe it will be a string.

Overrides FieldPluginBase::render

File

src/Plugin/views/field/TranslationModerationState.php, line 122

Class

TranslationModerationState
Provides a field that adds moderation state.

Namespace

Drupal\translation_views\Plugin\views\field

Code

public function render(ResultRow $values) {
  if ($values->_entity
    ->id()) {

    /** @var \Drupal\Core\Entity\ContentEntityStorageInterface $storage */
    $entityTypeId = $values->_entity
      ->getEntityTypeId();
    $storage = $this->entityTypeManager
      ->getStorage($entityTypeId);
    $entity = $storage
      ->load($values->_entity
      ->id());
    $target_langcode = $this
      ->getTargetLangcode();
    $pending_revision_enabled = ContentTranslationManager::isPendingRevisionSupportEnabled($entityTypeId);
    if (empty($target_langcode) || $target_langcode == '***LANGUAGE_site_default***') {
      $target_langcode = $this->languageManager
        ->getCurrentLanguage()
        ->getId();
    }
    $translation_has_revision = $storage
      ->getLatestTranslationAffectedRevisionId($values->_entity
      ->id(), $target_langcode);
    if ($entity && $pending_revision_enabled && $target_langcode && $translation_has_revision) {
      $latest_revision = $storage
        ->loadRevision($translation_has_revision);
      if ($entity && $latest_revision
        ->hasTranslation($target_langcode)) {
        $workflow = $this->moderationInfo
          ->getWorkflowForEntity($latest_revision);
        $translation = $latest_revision
          ->getTranslation($target_langcode);
        $translation_state = $translation->moderation_state->value;
        if (!empty($this->options['display_name']) && $workflow !== NULL) {
          if ($this->options['display_name'] == 'state') {
            $translation_label = $workflow
              ->getTypePlugin()
              ->getState($translation_state)
              ->label();
            $values->{$this->field_alias} = $translation_label ? $translation_label : NULL;
          }
          else {
            $values->{$this->field_alias} = $translation_state ? $translation_state : NULL;
          }
        }
      }
    }
  }
  return parent::render($values);
}