Commit 083ee681 authored by alexpott's avatar alexpott

Issue #2015123 by Berdir, tim.plunkett: Expand NodeInterface to provide getters and setters.

parent e49b1294
......@@ -13,4 +13,123 @@
* Defines the node specific entity BC decorator.
*/
class NodeBCDecorator extends EntityBCDecorator implements NodeInterface {
/**
* {@inheritdoc}
*/
public function setTitle($title) {
$this->decorated->setTitle($title);
}
/**
* {@inheritdoc}
*/
public function getCreatedTime() {
return $this->decorated->getCreatedTime();
}
/**
* {@inheritdoc}
*/
public function setCreatedTime($timestamp) {
return $this->decorated->setCreatedTime($timestamp);
}
/**
* {@inheritdoc}
*/
public function getChangedTime() {
return $this->decorated->getChangedTime();
}
/**
* {@inheritdoc}
*/
public function isPromoted() {
return $this->decorated->isPromoted();
}
/**
* {@inheritdoc}
*/
public function setPromoted($promoted) {
$this->decorated->setPromoted($promoted);
}
/**
* {@inheritdoc}
*/
public function isSticky() {
return $this->decorated->isSticky();
}
/**
* {@inheritdoc}
*/
public function setSticky($sticky) {
$this->decorated->setSticky($sticky);
}
/**
* {@inheritdoc}
*/
public function getAuthor() {
return $this->decorated->getAuthor();
}
/**
* {@inheritdoc}
*/
public function getAuthorId() {
return $this->decorated->getAuthorId();
}
/**
* {@inheritdoc}
*/
public function setAuthorId($uid) {
$this->decorated->setAuthorId($uid);
}
/**
* {@inheritdoc}
*/
public function isPublished() {
return $this->decorated->isPublished();
}
/**
* {@inheritdoc}
*/
public function setPublished($published) {
$this->decorated->setPublished($published);
}
/**
* {@inheritdoc}
*/
public function getRevisionCreationTime() {
return $this->decorated->getRevisionCreationTime();
}
/**
* {@inheritdoc}
*/
public function setRevisionCreationTime($timestamp) {
return $this->decorated->setRevisionCreationTime($timestamp);
}
/**
* {@inheritdoc}
*/
public function getRevisionAuthor() {
return $this->decorated->getRevisionAuthor();
}
/**
* {@inheritdoc}
*/
public function setRevisionAuthorId($uid) {
return $this->decorated->setRevisionAuthorId($uid);
}
}
......@@ -8,10 +8,173 @@
namespace Drupal\node;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\user\UserInterface;
/**
* Provides an interface defining a node entity.
*/
interface NodeInterface extends ContentEntityInterface {
/**
* Sets the node title.
*
* @param string $title
* The node title.
*
* @return \Drupal\node\NodeInterface
* The called node entity.
*/
public function setTitle($title);
/**
* Returns the node creation timestamp.
*
* @return int
* Creation timestamp of the node.
*/
public function getCreatedTime();
/**
* Sets the node creation timestamp.
*
* @param int $timestamp
* The node creation timestamp.
*
* @return \Drupal\node\NodeInterface
* The called node entity.
*/
public function setCreatedTime($timestamp);
/**
* Returns the node modification timestamp.
*
* @return int
* Node creation timestamp.
*/
public function getChangedTime();
/**
* Returns the node promotion status.
*
* @return bool
* TRUE if the node is promoted.
*/
public function isPromoted();
/**
* Sets the node promoted status.
*
* @param bool $promoted
* TRUE to set this node to promoted, FALSE to set it to not promoted.
*
* @return \Drupal\node\NodeInterface
* The called node entity.
*/
public function setPromoted($promoted);
/**
* Returns the node sticky status.
*
* @return bool
* TRUE if the node is sticky.
*/
public function isSticky();
/**
* Sets the node sticky status.
*
* @param bool $sticky
* TRUE to set this node to sticky, FALSE to set it to not sticky.
*
* @return \Drupal\node\NodeInterface
* The called node entity.
*/
public function setSticky($sticky);
/**
* Returns the node author user entity.
*
* @return \Drupal\user\UserInterface
* The author user entity.
*/
public function getAuthor();
/**
* Returns the node author user ID.
*
* @return int
* The author user ID.
*/
public function getAuthorId();
/**
* Sets the node author user ID.
*
* @param int $uid
* The author user id.
*
* @return \Drupal\node\NodeInterface
* The called node entity.
*/
public function setAuthorId($uid);
/**
* Returns the node published status indicator.
*
* Unpublished nodes are only visible to their authors and to administrators.
*
* @return bool
* TRUE if the node is published.
*/
public function isPublished();
/**
* Sets the published status of a node..
*
* @param bool $published
* TRUE to set this node to published, FALSE to set it to unpublished.
*
* @return \Drupal\node\NodeInterface
* The called node entity.
*/
public function setPublished($published);
/**
* Returns the node revision creation timestamp.
*
* @return int
* The UNIX timestamp of when this revision was created.
*/
public function getRevisionCreationTime();
/**
* Sets the node revision creation timestamp.
*
* @param int $imestamp
* The UNIX timestamp of when this revision was created.
*
* @return \Drupal\node\NodeInterface
* The called node entity.
*/
public function setRevisionCreationTime($timestamp);
/**
* Returns the node revision author.
*
* @return \Drupal\user\UserInterface
* The user entity for the revision author.
*/
public function getRevisionAuthor();
/**
* Sets the node revision author.
*
* @param int $uid
* The user ID of the revision author.
*
* @return \Drupal\node\NodeInterface
* The called node entity.
*/
public function setRevisionAuthorId($uid);
}
......@@ -60,255 +60,217 @@
class Node extends EntityNG implements NodeInterface {
/**
* The node ID.
*
* @var \Drupal\Core\Entity\Field\FieldInterface
*/
public $nid;
/**
* The node revision ID.
*
* @var \Drupal\Core\Entity\Field\FieldInterface
* Implements Drupal\Core\Entity\EntityInterface::id().
*/
public $vid;
public function id() {
return $this->get('nid')->value;
}
/**
* The node UUID.
*
* @var \Drupal\Core\Entity\Field\FieldInterface
* Overrides Drupal\Core\Entity\Entity::getRevisionId().
*/
public $uuid;
public function getRevisionId() {
return $this->get('vid')->value;
}
/**
* The node content type (bundle).
*
* @var \Drupal\Core\Entity\Field\FieldInterface
* {@inheritdoc}
*/
public $type;
public function preSave(EntityStorageControllerInterface $storage_controller) {
// Before saving the node, set changed and revision times.
$this->changed->value = REQUEST_TIME;
}
/**
* The node language code.
*
* @var \Drupal\Core\Entity\Field\FieldInterface
* {@inheritdoc}
*/
public $langcode;
public function preSaveRevision(EntityStorageControllerInterface $storage_controller, \stdClass $record) {
if ($this->newRevision) {
// When inserting either a new node or a new node revision, $node->log
// must be set because {node_field_revision}.log is a text column and
// therefore cannot have a default value. However, it might not be set at
// this point (for example, if the user submitting a node form does not
// have permission to create revisions), so we ensure that it is at least
// an empty string in that case.
// @todo Make the {node_field_revision}.log column nullable so that we
// can remove this check.
if (!isset($record->log)) {
$record->log = '';
}
}
elseif (isset($this->original) && (!isset($record->log) || $record->log === '')) {
// If we are updating an existing node without adding a new revision, we
// need to make sure $entity->log is reset whenever it is empty.
// Therefore, this code allows us to avoid clobbering an existing log
// entry with an empty one.
$record->log = $this->original->log;
}
}
/**
* The node title.
*
* @var \Drupal\Core\Entity\Field\FieldInterface
* {@inheritdoc}
*/
public $title;
public function postSave(EntityStorageControllerInterface $storage_controller, $update = TRUE) {
// Update the node access table for this node, but only if it is the
// default revision. There's no need to delete existing records if the node
// is new.
if ($this->isDefaultRevision()) {
node_access_acquire_grants($this->getBCEntity(), $update);
}
}
/**
* The node owner's user ID.
*
* @var \Drupal\Core\Entity\Field\FieldInterface
* {@inheritdoc}
*/
public $uid;
public function getBCEntity() {
if (!isset($this->bcEntity)) {
$this->getPropertyDefinitions();
$this->bcEntity = new NodeBCDecorator($this, $this->fieldDefinitions);
}
return $this->bcEntity;
}
/**
* The node published status indicator.
*
* Unpublished nodes are only visible to their authors and to administrators.
* The value is either NODE_PUBLISHED or NODE_NOT_PUBLISHED.
*
* @var \Drupal\Core\Entity\Field\FieldInterface
* {@inheritdoc}
*/
public $status;
public static function preDelete(EntityStorageControllerInterface $storage_controller, array $entities) {
if (module_exists('search')) {
foreach ($entities as $id => $entity) {
search_reindex($entity->nid->value, 'node');
}
}
}
/**
* The node creation timestamp.
*
* @var \Drupal\Core\Entity\Field\FieldInterface
* {@inheritdoc}
*/
public $created;
public function setTitle($title) {
$this->set('title', $title);
return $this;
}
/**
* The node modification timestamp.
*
* @var \Drupal\Core\Entity\Field\FieldInterface
* {@inheritdoc}
*/
public $changed;
public function getCreatedTime() {
return $this->get('created')->value;
}
/**
* The node comment status indicator.
*
* COMMENT_NODE_HIDDEN => no comments
* COMMENT_NODE_CLOSED => comments are read-only
* COMMENT_NODE_OPEN => open (read/write)
*
* @var \Drupal\Core\Entity\Field\FieldInterface
*/
public $comment;
/**
* The node promotion status.
*
* Promoted nodes should be displayed on the front page of the site. The value
* is either NODE_PROMOTED or NODE_NOT_PROMOTED.
*
* @var \Drupal\Core\Entity\Field\FieldInterface
* {@inheritdoc}
*/
public $promote;
public function setCreatedTime($timestamp) {
$this->set('created', $timestamp);
return $this;
}
/**
* The node sticky status.
*
* Sticky nodes should be displayed at the top of lists in which they appear.
* The value is either NODE_STICKY or NODE_NOT_STICKY.
*
* @var \Drupal\Core\Entity\Field\FieldInterface
* {@inheritdoc}
*/
public $sticky;
public function getChangedTime() {
return $this->get('changed')->value;
}
/**
* The node translation set ID.
*
* Translations sets are based on the ID of the node containing the source
* text for the translation set.
*
* @var \Drupal\Core\Entity\Field\FieldInterface
* {@inheritdoc}
*/
public $tnid;
public function isPromoted() {
return (bool) $this->get('promote')->value;
}
/**
* The node translation status.
*
* If the translation page needs to be updated, the value is 1; otherwise 0.
*
* @var \Drupal\Core\Entity\Field\FieldInterface
* {@inheritdoc}
*/
public $translate;
public function setPromoted($promoted) {
$this->set('promoted', $promoted ? NODE_PROMOTED : NODE_NOT_PROMOTED);
return $this;
}
/**
* The node revision creation timestamp.
*
* @var \Drupal\Core\Entity\Field\FieldInterface
* {@inheritdoc}
*/
public $revision_timestamp;
public function isSticky() {
return (bool) $this->get('sticky')->value;
}
/**
* The node revision author's user ID.
*
* @var \Drupal\Core\Entity\Field\FieldInterface
* {@inheritdoc}
*/
public $revision_uid;
public function setSticky($sticky) {
$this->set('sticky', $sticky ? NODE_STICKY : NODE_NOT_STICKY);
return $this;
}
/**
* The node revision log message.
*
* @var \Drupal\Core\Entity\Field\FieldInterface
* {@inheritdoc}
*/
public $log;
public function isPublished() {
return (bool) $this->get('status')->value;
}
/**
* Overrides \Drupal\Core\Entity\EntityNG::init().
* {@inheritdoc}
*/
protected function init() {
parent::init();
// We unset all defined properties, so magic getters apply.
unset($this->nid);
unset($this->vid);
unset($this->uuid);
unset($this->type);
unset($this->title);
unset($this->uid);
unset($this->status);
unset($this->created);
unset($this->changed);
unset($this->comment);
unset($this->promote);
unset($this->sticky);
unset($this->tnid);
unset($this->translate);
unset($this->revision_timestamp);
unset($this->revision_uid);
unset($this->log);
public function setPublished($published) {
$this->set('status', $published ? NODE_PUBLISHED : NODE_NOT_PUBLISHED);
return $this;
}
/**
* Implements Drupal\Core\Entity\EntityInterface::id().
* {@inheritdoc}
*/
public function id() {
return $this->get('nid')->value;
public function getAuthor() {
$entity = $this->get('uid')->entity;
// If no user is given, default to the anonymous user.
if (!$entity) {
return user_load(0)->getBCentity();
}
return $entity->getBCEntity();
}
/**
* Overrides Drupal\Core\Entity\Entity::getRevisionId().
* {@inheritdoc}
*/
public function getRevisionId() {
return $this->get('vid')->value;
public function getAuthorId() {
return $this->get('uid')->target_id;
}
/**
* {@inheritdoc}
*/
public function preSave(EntityStorageControllerInterface $storage_controller) {
// Before saving the node, set changed and revision times.
$this->changed->value = REQUEST_TIME;
public function setAuthorId($uid) {
$this->set('uid', $uid);
return $this;
}
/**
* {@inheritdoc}
*/
public function preSaveRevision(EntityStorageControllerInterface $storage_controller