Commit 6ddd0ba5 authored by catch's avatar catch

Issue #2810381 by timmillwood, plach: Add generic status field to ContentEntityBase

parent aed10c0f
<?php
namespace Drupal\Core\Entity;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\StringTranslation\TranslatableMarkup;
/**
* Provides a trait for published status.
*/
trait EntityPublishedTrait {
/**
* Returns an array of base field definitions for publishing status.
*
* @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
* The entity type to add the publishing status field to.
*
* @return \Drupal\Core\Field\BaseFieldDefinition[]
* Array of base field definitions.
*/
public static function publishedBaseFieldDefinitions(EntityTypeInterface $entity_type) {
$key = $entity_type->hasKey('status') ? $entity_type->getKey('status') : 'status';
return [$key => BaseFieldDefinition::create('boolean')
->setLabel(new TranslatableMarkup('Publishing status'))
->setDescription(new TranslatableMarkup('A boolean indicating the published state.'))
->setRevisionable(TRUE)
->setTranslatable(TRUE)
->setDefaultValue(TRUE)];
}
/**
* Returns the published status of the entity.
*
* @return bool
* The published status of the entity.
*/
public function isPublished() {
$status = $this->getEntityKey('status');
return (bool) (isset($status) ? $status : $this->get('status')->value);
}
/**
* Sets the entity as published or not published.
*
* @param bool $published
* A boolean value denoting the published status.
*
* @return \Drupal\Core\Entity\ContentEntityInterface $this
* The Content Entity object.
*/
public function setPublished($published) {
/** @var \Drupal\Core\Entity\ContentEntityTypeInterface $entity_type */
$key = $this->getEntityType()->getKey('status') ?: 'status';
// @todo: Replace values with constants from EntityPublishedInterface or
// similar when introduced. https://www.drupal.org/node/2811667
$this->set($key, $published ? 1 : 0);
return $this;
}
}
......@@ -179,3 +179,20 @@ function comment_update_8200() {
/**
* @} End of "addtogroup updates-8.2.x".
*/
/**
* @addtogroup updates-8.3.x
* @{
*/
/**
* Update status field.
*/
function comment_update_8300() {
$field_definitions = \Drupal::service('entity_field.manager')->getBaseFieldDefinitions('comment');
\Drupal::service('entity.definition_update_manager')->updateFieldStorageDefinition($field_definitions['status']);
}
/**
* @} End of "addtogroup updates-8.3.x".
*/
......@@ -7,6 +7,7 @@
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\comment\CommentInterface;
use Drupal\Core\Entity\EntityChangedTrait;
use Drupal\Core\Entity\EntityPublishedTrait;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
......@@ -66,6 +67,7 @@
class Comment extends ContentEntityBase implements CommentInterface {
use EntityChangedTrait;
use EntityPublishedTrait;
/**
* The thread for which a lock was acquired.
......@@ -216,6 +218,7 @@ public function permalink() {
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
/** @var \Drupal\Core\Field\BaseFieldDefinition[] $fields */
$fields = parent::baseFieldDefinitions($entity_type);
$fields += static::publishedBaseFieldDefinitions($entity_type);
$fields['cid']->setLabel(t('Comment ID'))
->setDescription(t('The comment ID.'));
......@@ -291,12 +294,6 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
->setDescription(t('The time that the comment was last edited.'))
->setTranslatable(TRUE);
$fields['status'] = BaseFieldDefinition::create('boolean')
->setLabel(t('Publishing status'))
->setDescription(t('A boolean indicating whether the comment is published.'))
->setTranslatable(TRUE)
->setDefaultValue(TRUE);
$fields['thread'] = BaseFieldDefinition::create('string')
->setLabel(t('Thread place'))
->setDescription(t("The alphadecimal representation of the comment's place in a thread, consisting of a base 36 string prefixed by an integer indicating its length."))
......@@ -473,13 +470,6 @@ public function setCreatedTime($created) {
return $this;
}
/**
* {@inheritdoc}
*/
public function isPublished() {
return $this->get('status')->value == CommentInterface::PUBLISHED;
}
/**
* {@inheritdoc}
*/
......@@ -487,14 +477,6 @@ public function getStatus() {
return $this->get('status')->value;
}
/**
* {@inheritdoc}
*/
public function setPublished($status) {
$this->set('status', $status ? CommentInterface::PUBLISHED : CommentInterface::NOT_PUBLISHED);
return $this;
}
/**
* {@inheritdoc}
*/
......
......@@ -4,6 +4,7 @@
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityChangedTrait;
use Drupal\Core\Entity\EntityPublishedTrait;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
......@@ -73,6 +74,7 @@
class Node extends ContentEntityBase implements NodeInterface {
use EntityChangedTrait;
use EntityPublishedTrait;
/**
* Whether the node is being previewed or not.
......@@ -242,20 +244,6 @@ public function setSticky($sticky) {
$this->set('sticky', $sticky ? NODE_STICKY : NODE_NOT_STICKY);
return $this;
}
/**
* {@inheritdoc}
*/
public function isPublished() {
return (bool) $this->getEntityKey('status');
}
/**
* {@inheritdoc}
*/
public function setPublished($published) {
$this->set('status', $published ? NODE_PUBLISHED : NODE_NOT_PUBLISHED);
return $this;
}
/**
* {@inheritdoc}
......@@ -367,6 +355,7 @@ public function setRevisionLogMessage($revision_log_message) {
*/
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$fields = parent::baseFieldDefinitions($entity_type);
$fields += static::publishedBaseFieldDefinitions($entity_type);
$fields['title'] = BaseFieldDefinition::create('string')
->setLabel(t('Title'))
......@@ -408,13 +397,6 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
))
->setDisplayConfigurable('form', TRUE);
$fields['status'] = BaseFieldDefinition::create('boolean')
->setLabel(t('Publishing status'))
->setDescription(t('A boolean indicating whether the node is published.'))
->setRevisionable(TRUE)
->setTranslatable(TRUE)
->setDefaultValue(TRUE);
$fields['created'] = BaseFieldDefinition::create('created')
->setLabel(t('Authored on'))
->setDescription(t('The time that the node was created.'))
......
......@@ -292,10 +292,10 @@ public function testUpdateComment() {
$this->pass('Test case 1: PATCH comment using HAL+JSON.');
$comment->setSubject('Initial subject')->save();
$read_only_fields = [
'status',
'name',
'created',
'changed',
'status',
'thread',
'entity_type',
'field_name',
......@@ -311,6 +311,7 @@ public function testUpdateComment() {
$this->pass('Test case 1: PATCH comment using JSON.');
$comment->setSubject('Initial subject')->save();
$read_only_fields = [
'status',
'pid', // Extra compared to HAL+JSON.
'entity_id',
'uid',
......@@ -318,7 +319,6 @@ public function testUpdateComment() {
'homepage', // Extra compared to HAL+JSON.
'created',
'changed',
'status',
'thread',
'entity_type',
'field_name',
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment