From 619017f1c4b0b8e781bdfe3b8025acdff63746eb Mon Sep 17 00:00:00 2001 From: Dries Date: Sun, 17 Jun 2012 09:27:32 -0400 Subject: [PATCH] - Patch #1612014 by aspilicious, Berdir: create an interface for revisionable entities. --- .../entity/DatabaseStorageController.php | 4 ++++ .../entity/lib/Drupal/entity/Entity.php | 22 +++++++++++++++++++ .../lib/Drupal/entity/EntityInterface.php | 18 +++++++++++++++ core/modules/node/lib/Drupal/node/Node.php | 7 ++++++ .../lib/Drupal/node/NodeStorageController.php | 3 +++ .../Drupal/node/Tests/NodeRevisionsTest.php | 7 ++++++ 6 files changed, 61 insertions(+) diff --git a/core/modules/entity/lib/Drupal/entity/DatabaseStorageController.php b/core/modules/entity/lib/Drupal/entity/DatabaseStorageController.php index 77834a19f3a..8d893419be7 100644 --- a/core/modules/entity/lib/Drupal/entity/DatabaseStorageController.php +++ b/core/modules/entity/lib/Drupal/entity/DatabaseStorageController.php @@ -252,6 +252,10 @@ class DatabaseStorageController implements EntityStorageControllerInterface { } } $query->fields('revision', $entity_revision_fields); + + // Compare revision id of the base and revision table, if equal then this + // is the current revision. + $query->addExpression('base.' . $this->revisionKey . ' = revision.' . $this->revisionKey, 'isCurrentRevision'); } $query->fields('base', $entity_fields); diff --git a/core/modules/entity/lib/Drupal/entity/Entity.php b/core/modules/entity/lib/Drupal/entity/Entity.php index d17e57d9f7e..eeaeee88717 100644 --- a/core/modules/entity/lib/Drupal/entity/Entity.php +++ b/core/modules/entity/lib/Drupal/entity/Entity.php @@ -38,6 +38,13 @@ class Entity implements EntityInterface { */ protected $enforceIsNew; + /** + * Indicates whether this is the current revision. + * + * @var bool + */ + public $isCurrentRevision = TRUE; + /** * Constructs a new entity object. */ @@ -248,4 +255,19 @@ class Entity implements EntityInterface { public function entityInfo() { return entity_get_info($this->entityType); } + + /** + * Implements Drupal\entity\EntityInterface::getRevisionId(). + */ + public function getRevisionId() { + return NULL; + } + + /** + * Implements Drupal\entity\EntityInterface::isCurrentRevision(). + */ + public function isCurrentRevision() { + return $this->isCurrentRevision; + } + } diff --git a/core/modules/entity/lib/Drupal/entity/EntityInterface.php b/core/modules/entity/lib/Drupal/entity/EntityInterface.php index f720181c052..699c424f986 100644 --- a/core/modules/entity/lib/Drupal/entity/EntityInterface.php +++ b/core/modules/entity/lib/Drupal/entity/EntityInterface.php @@ -187,4 +187,22 @@ interface EntityInterface { * @see entity_get_info() */ public function entityInfo(); + + /** + * Returns the revision identifier of the entity. + * + * @return + * The revision identifier of the entity, or NULL if the entity does not + * have a revision identifier. + */ + public function getRevisionId(); + + /** + * Checks if this entity is the current revision. + * + * @return bool + * TRUE if the entity is the current revision, FALSE otherwise. + */ + public function isCurrentRevision(); + } diff --git a/core/modules/node/lib/Drupal/node/Node.php b/core/modules/node/lib/Drupal/node/Node.php index 1aac6fb6382..cee6967b0c1 100644 --- a/core/modules/node/lib/Drupal/node/Node.php +++ b/core/modules/node/lib/Drupal/node/Node.php @@ -167,4 +167,11 @@ class Node extends Entity { $duplicate->vid = NULL; return $duplicate; } + + /** + * Overrides Drupal\entity\Entity::getRevisionId(). + */ + public function getRevisionId() { + return $this->vid; + } } diff --git a/core/modules/node/lib/Drupal/node/NodeStorageController.php b/core/modules/node/lib/Drupal/node/NodeStorageController.php index aa525828017..0fa7b3387dc 100644 --- a/core/modules/node/lib/Drupal/node/NodeStorageController.php +++ b/core/modules/node/lib/Drupal/node/NodeStorageController.php @@ -149,6 +149,9 @@ class NodeStorageController extends DatabaseStorageController { } // Make sure to update the new revision key for the entity. $entity->{$this->revisionKey} = $record->{$this->revisionKey}; + + // Mark this revision as the current one. + $entity->isCurrentRevision = TRUE; } /** diff --git a/core/modules/node/lib/Drupal/node/Tests/NodeRevisionsTest.php b/core/modules/node/lib/Drupal/node/Tests/NodeRevisionsTest.php index c7f29faac66..3cba15d075d 100644 --- a/core/modules/node/lib/Drupal/node/Tests/NodeRevisionsTest.php +++ b/core/modules/node/lib/Drupal/node/Tests/NodeRevisionsTest.php @@ -75,6 +75,9 @@ class NodeRevisionsTest extends NodeTestBase { $this->assertText($log, t('Log message found.')); } + // Confirm that this is the current revision. + $this->assertTrue($node->isCurrentRevision(), 'Third node revision is the current one.'); + // Confirm that revisions revert properly. $this->drupalPost("node/$node->nid/revisions/{$nodes[1]->vid}/revert", array(), t('Revert')); $this->assertRaw(t('@type %title has been reverted back to the revision from %revision-date.', @@ -83,6 +86,10 @@ class NodeRevisionsTest extends NodeTestBase { $reverted_node = node_load($node->nid); $this->assertTrue(($nodes[1]->body[LANGUAGE_NOT_SPECIFIED][0]['value'] == $reverted_node->body[LANGUAGE_NOT_SPECIFIED][0]['value']), t('Node reverted correctly.')); + // Confirm that this is not the current version. + $node = node_load($node->nid, $node->vid); + $this->assertFalse($node->isCurrentRevision(), 'Third node revision is not the current one.'); + // Confirm revisions delete properly. $this->drupalPost("node/$node->nid/revisions/{$nodes[1]->vid}/delete", array(), t('Delete')); $this->assertRaw(t('Revision from %revision-date of @type %title has been deleted.',