You are here

public function LinkToLatestVersion::render in Moderation Dashboard 2.0.x

Same name and namespace in other branches
  1. 8 src/Plugin/views/field/LinkToLatestVersion.php \Drupal\moderation_dashboard\Plugin\views\field\LinkToLatestVersion::render()

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/LinkToLatestVersion.php, line 19

Class

LinkToLatestVersion
A Views field which provides a link to the latest version of an Entity.

Namespace

Drupal\moderation_dashboard\Plugin\views\field

Code

public function render(ResultRow $values) {

  /** @var \Drupal\content_moderation\ModerationInformation $information */
  $moderation_information = \Drupal::service('content_moderation.moderation_information');

  /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
  $entity = $this
    ->getEntity($values);
  $entity_type_id = $entity
    ->getEntityTypeId();
  if ($moderation_information
    ->isModeratedEntity($entity) && $moderation_information
    ->hasPendingRevision($entity)) {
    $entity = $moderation_information
      ->getLatestRevision($entity_type_id, $entity
      ->id());
    $build = [
      '#title' => $entity
        ->label(),
      '#type' => 'link',
      '#url' => Url::fromRoute("entity.{$entity_type_id}.latest_version", [
        $entity_type_id => $entity
          ->id(),
      ]),
    ];
  }
  else {
    $build = [
      '#title' => $entity
        ->label(),
      '#type' => 'link',
      '#url' => $entity
        ->toLink()
        ->getUrl(),
    ];
  }
  return $build;
}