Skip to content
Snippets Groups Projects
Commit 0263c3e7 authored by Daniel Wehner's avatar Daniel Wehner
Browse files

some small fixes

parent 857dd902
No related branches found
No related tags found
No related merge requests found
......@@ -17,11 +17,15 @@ use Drupal\Core\Entity\EntityInterface;
trait RevisionControllerTrait {
/**
* Returns the entity type manager.
*
* @return \Drupal\Core\Entity\EntityTypeManagerInterface
*/
abstract protected function entityTypeManager();
/**
* Returns the langauge manager.
*
* @return \Drupal\Core\Language\LanguageManagerInterface
*/
public abstract function languageManager();
......@@ -54,9 +58,8 @@ trait RevisionControllerTrait {
* @param \Drupal\Core\Entity\EntityInterface $entity_revision
* The entity to build a revert revision link for.
*
* @return array A link render array.
* A link render array.
* @internal param int $revision_id The revision ID of the revert link.* The revision ID of the revert link.
* @return array
* A link render array.
*
*/
abstract protected function buildRevertRevisionLink(EntityInterface $entity_revision);
......@@ -67,10 +70,8 @@ trait RevisionControllerTrait {
* @param \Drupal\Core\Entity\EntityInterface $entity_revision
* The entity to build a delete revision link for.
*
* @return array A link render array.
* A link render array.
* @internal param int $revision_id The revision ID of the delete link.* The revision ID of the delete link.
*
* @return array
* A link render array.
*/
abstract protected function buildDeleteRevisionLink(EntityInterface $entity_revision);
......@@ -81,6 +82,7 @@ trait RevisionControllerTrait {
* non-current revision, it also provides a link to view that revision.
*
* @param \Drupal\Core\Entity\ContentEntityInterface $revision
* The entity revision.
* @param bool $is_current
* TRUE if the revision is the current revision.
*
......@@ -90,9 +92,12 @@ trait RevisionControllerTrait {
abstract protected function getRevisionDescription(ContentEntityInterface $revision, $is_current = FALSE);
/**
* Loads all revision IDs of an entity sorted by revision ID descending.
*
* @param \Drupal\Core\Entity\ContentEntityInterface $entity
* The entity.
*
* @return array
* @return mixed[]
*/
protected function revisionIds(ContentEntityInterface $entity) {
$entity_type = $entity->getEntityTypeId();
......@@ -111,13 +116,12 @@ trait RevisionControllerTrait {
* An entity object.
*
* @return array
* An array as expected by drupal_render().
* A render array.
*/
protected function revisionOverview(ContentEntityInterface $entity) {
$langcode = $this->languageManager()
->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)
->getId();
/** @var \Drupal\content_entity_base\Entity\Storage\RevisionableStorageInterface $entity_storage */
$entity_storage = $this->entityTypeManager()
->getStorage($entity->getEntityTypeId());
......@@ -181,13 +185,11 @@ trait RevisionControllerTrait {
*/
protected function getOperationLinks(EntityInterface $entity_revision) {
$links = [];
$revert_permission = $this->hasRevertRevisionAccess($entity_revision);
$delete_permission = $this->hasDeleteRevisionAccess($entity_revision);
if ($revert_permission) {
if ($this->hasRevertRevisionAccess($entity_revision)) {
$links['revert'] = $this->buildRevertRevisionLink($entity_revision);
}
if ($delete_permission) {
if ($this->hasDeleteRevisionAccess($entity_revision)) {
$links['delete'] = $this->buildDeleteRevisionLink($entity_revision);
}
......
......@@ -18,6 +18,13 @@ use Drupal\entity\Revision\EntityRevisionLogInterface;
use Drupal\user\EntityOwnerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides a controller which shows the revision history.
*
* This controller leverages the revision controller trait, which is agnostic to
* any entity type, by using the new interface
* \Drupal\entity\Revision\EntityRevisionLogInterface.
*/
class RevisionOverviewController extends ControllerBase {
use RevisionControllerTrait;
......@@ -77,6 +84,15 @@ class RevisionOverviewController extends ControllerBase {
}
}
/**
* 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')));
}
......@@ -87,23 +103,20 @@ class RevisionOverviewController extends ControllerBase {
protected function getRevisionDescription(ContentEntityInterface $revision, $is_default = FALSE) {
/** @var \Drupal\Core\Entity\ContentEntityInterface|\Drupal\user\EntityOwnerInterface|\Drupal\entity\Revision\EntityRevisionLogInterface $revision */
if ($revision instanceof EntityOwnerInterface) {
$username = [
'#theme' => 'username',
'#account' => $revision->getOwner(),
];
}
else {
$username = '';
}
if ($revision instanceof EntityRevisionLogInterface) {
// Use revision link to link to revisions that are not active.
$date = $this->dateFormatter->format($revision->getRevisionCreationTime(), 'short');
$link = $revision->toLink($date, 'revision');
$username = [
'#theme' => 'username',
'#account' => $revision->getRevisionUser(),
];
}
else {
$link = $revision->toLink($revision->label(), 'revision');
$username = '';
}
$markup = '';
......
......@@ -118,9 +118,7 @@ class RevisionRouteProvider implements EntityRouteProviderInterface {
'_controller' => '\Drupal\entity\Controller\RevisionOverviewController::revisionOverviewController',
'_title' => 'Revisions',
]);
$route->addRequirements([
'_entity_access_revision' => "$entity_type_id.list",
]);
$route->setRequirement('_entity_access_revision', "$entity_type_id.list");
$route->setOption('entity_type_id', $entity_type->id());
$route->setOption('parameters', [
$entity_type->id() => [
......
......@@ -43,6 +43,9 @@ class RevisionBasicUITest extends KernelTestBase {
\Drupal::service('router.builder')->rebuild();
}
/**
* Tests the revision history controller.
*/
public function testRevisionHistory() {
$entity = EnhancedEntity::create([
'name' => 'rev 1',
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment