- Patch #1612014 by aspilicious, Berdir: create an interface for revisionable entities.

8.0.x
Dries 2012-06-17 09:27:32 -04:00
parent 4f62dcc38c
commit 619017f1c4
6 changed files with 61 additions and 0 deletions

View File

@ -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);

View File

@ -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;
}
}

View File

@ -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();
}

View File

@ -167,4 +167,11 @@ class Node extends Entity {
$duplicate->vid = NULL;
return $duplicate;
}
/**
* Overrides Drupal\entity\Entity::getRevisionId().
*/
public function getRevisionId() {
return $this->vid;
}
}

View File

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

View File

@ -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.',