You are here

class RevisionOverviewController in Entity API 8

Same name and namespace in other branches
  1. 8.0 src/Controller/RevisionOverviewController.php \Drupal\entity\Controller\RevisionOverviewController

Provides a controller which shows the revision history.

This controller leverages the revision controller trait, which is agnostic to any entity type, by using \Drupal\Core\Entity\RevisionLogInterface.

Hierarchy

Expanded class hierarchy of RevisionOverviewController

File

src/Controller/RevisionOverviewController.php, line 22

Namespace

Drupal\entity\Controller
View source
class RevisionOverviewController extends ControllerBase {
  use RevisionControllerTrait;

  /**
   * The date formatter.
   *
   * @var \Drupal\Core\Datetime\DateFormatterInterface
   */
  protected $dateFormatter;

  /**
   * The renderer.
   *
   * @var \Drupal\Core\Render\RendererInterface
   */
  protected $renderer;

  /**
   * Creates a new RevisionOverviewController instance.
   *
   * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
   *   The date formatter.
   */
  public function __construct(DateFormatterInterface $date_formatter, RendererInterface $renderer) {
    $this->dateFormatter = $date_formatter;
    $this->renderer = $renderer;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('date.formatter'), $container
      ->get('renderer'));
  }

  /**
   * {@inheritdoc}
   */
  protected function hasDeleteRevisionAccess(EntityInterface $entity) {
    return $this
      ->currentUser()
      ->hasPermission("delete all {$entity->getEntityTypeId()} revisions");
  }

  /**
   * {@inheritdoc}
   */
  protected function buildRevertRevisionLink(EntityInterface $entity_revision) {
    if ($entity_revision
      ->hasLinkTemplate('revision-revert-form')) {
      return [
        'title' => $this
          ->t('Revert'),
        'url' => $entity_revision
          ->toUrl('revision-revert-form'),
      ];
    }
  }

  /**
   * {@inheritdoc}
   */
  protected function buildDeleteRevisionLink(EntityInterface $entity_revision) {
    if ($entity_revision
      ->hasLinkTemplate('revision-delete-form')) {
      return [
        'title' => $this
          ->t('Delete'),
        'url' => $entity_revision
          ->toUrl('revision-delete-form'),
      ];
    }
  }

  /**
   * Generates an overview table of older revisions of an entity.
   *
   * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
   *   The route match.
   *
   * @return array
   *   A render array.
   */
  public function revisionOverviewController(RouteMatchInterface $route_match) {
    return $this
      ->revisionOverview($route_match
      ->getParameter($route_match
      ->getRouteObject()
      ->getOption('entity_type_id')));
  }

  /**
   * {@inheritdoc}
   */
  protected function getRevisionDescription(ContentEntityInterface $revision, $is_default = FALSE) {

    /** @var \Drupal\Core\Entity\ContentEntityInterface|\Drupal\user\EntityOwnerInterface|\Drupal\Core\Entity\RevisionLogInterface $revision */
    if ($revision instanceof RevisionLogInterface) {

      // Use revision link to link to revisions that are not active.
      $date = $this->dateFormatter
        ->format($revision
        ->getRevisionCreationTime(), 'short');
      $link = $revision
        ->toLink($date, 'revision');

      // @todo: Simplify this when https://www.drupal.org/node/2334319 lands.
      $username = [
        '#theme' => 'username',
        '#account' => $revision
          ->getRevisionUser(),
      ];
      $username = $this->renderer
        ->render($username);
    }
    else {
      $link = $revision
        ->toLink($revision
        ->label(), 'revision');
      $username = '';
    }
    $markup = '';
    if ($revision instanceof RevisionLogInterface) {
      $markup = $revision
        ->getRevisionLogMessage();
    }
    if ($username) {
      $template = '{% trans %}{{ date }} by {{ username }}{% endtrans %}{% if message %}<p class="revision-log">{{ message }}</p>{% endif %}';
    }
    else {
      $template = '{% trans %} {{ date }} {% endtrans %}{% if message %}<p class="revision-log">{{ message }}</p>{% endif %}';
    }
    $column = [
      'data' => [
        '#type' => 'inline_template',
        '#template' => $template,
        '#context' => [
          'date' => $link
            ->toString(),
          'username' => $username,
          'message' => [
            '#markup' => $markup,
            '#allowed_tags' => Xss::getHtmlTagList(),
          ],
        ],
      ],
    ];
    return $column;
  }

  /**
   * {@inheritdoc}
   */
  protected function hasRevertRevisionAccess(EntityInterface $entity) {
    return AccessResult::allowedIfHasPermission($this
      ->currentUser(), "revert all {$entity->getEntityTypeId()} revisions")
      ->orIf(AccessResult::allowedIfHasPermission($this
      ->currentUser(), "revert {$entity->bundle()} {$entity->getEntityTypeId()} revisions"));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ControllerBase::$configFactory protected property The configuration factory.
ControllerBase::$currentUser protected property The current user service. 1
ControllerBase::$entityFormBuilder protected property The entity form builder.
ControllerBase::$entityManager protected property The entity manager.
ControllerBase::$entityTypeManager protected property The entity type manager.
ControllerBase::$formBuilder protected property The form builder. 2
ControllerBase::$keyValue protected property The key-value storage. 1
ControllerBase::$languageManager protected property The language manager. 1
ControllerBase::$moduleHandler protected property The module handler. 2
ControllerBase::$stateService protected property The state service.
ControllerBase::cache protected function Returns the requested cache bin.
ControllerBase::config protected function Retrieves a configuration object.
ControllerBase::container private function Returns the service container.
ControllerBase::currentUser protected function Returns the current user. 1
ControllerBase::entityFormBuilder protected function Retrieves the entity form builder.
ControllerBase::entityManager Deprecated protected function Retrieves the entity manager service.
ControllerBase::formBuilder protected function Returns the form builder service. 2
ControllerBase::keyValue protected function Returns a key/value storage collection. 1
ControllerBase::moduleHandler protected function Returns the module handler. 2
ControllerBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
ControllerBase::state protected function Returns the state storage service.
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
RevisionControllerTrait::entityTypeManager abstract protected function Returns the entity type manager.
RevisionControllerTrait::getOperationLinks protected function Get the links of the operations for an entity revision.
RevisionControllerTrait::languageManager abstract public function Returns the langauge manager.
RevisionControllerTrait::revisionIds protected function Loads all revision IDs of an entity sorted by revision ID descending.
RevisionControllerTrait::revisionOverview protected function Generates an overview table of older revisions of an entity.
RevisionOverviewController::$dateFormatter protected property The date formatter.
RevisionOverviewController::$renderer protected property The renderer.
RevisionOverviewController::buildDeleteRevisionLink protected function Builds a link to delete an entity revision. Overrides RevisionControllerTrait::buildDeleteRevisionLink
RevisionOverviewController::buildRevertRevisionLink protected function Builds a link to revert an entity revision. Overrides RevisionControllerTrait::buildRevertRevisionLink
RevisionOverviewController::create public static function Instantiates a new instance of this class. Overrides ControllerBase::create
RevisionOverviewController::getRevisionDescription protected function Returns a string providing details of the revision. Overrides RevisionControllerTrait::getRevisionDescription
RevisionOverviewController::hasDeleteRevisionAccess protected function Determines if the user has permission to delete revisions. Overrides RevisionControllerTrait::hasDeleteRevisionAccess
RevisionOverviewController::hasRevertRevisionAccess protected function Determines if the user has permission to revert revisions. Overrides RevisionControllerTrait::hasRevertRevisionAccess
RevisionOverviewController::revisionOverviewController public function Generates an overview table of older revisions of an entity.
RevisionOverviewController::__construct public function Creates a new RevisionOverviewController instance.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.