You are here

LinkToLatestVersion.php in Moderation Dashboard 2.0.x

Same filename and directory in other branches
  1. 8 src/Plugin/views/field/LinkToLatestVersion.php

File

src/Plugin/views/field/LinkToLatestVersion.php
View source
<?php

namespace Drupal\moderation_dashboard\Plugin\views\field;

use Drupal\Core\Url;
use Drupal\views\Plugin\views\field\FieldPluginBase;
use Drupal\views\ResultRow;

/**
 * A Views field which provides a link to the latest version of an Entity.
 *
 * @ViewsField("link_to_latest_version")
 */
class LinkToLatestVersion extends FieldPluginBase {

  /**
   * {@inheritdoc}
   */
  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;
  }

}

Classes

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