diff --git a/core/lib/Drupal/Core/Entity/EntityPublishedTrait.php b/core/lib/Drupal/Core/Entity/EntityPublishedTrait.php index 2e9d4c2..05f4533 100644 --- a/core/lib/Drupal/Core/Entity/EntityPublishedTrait.php +++ b/core/lib/Drupal/Core/Entity/EntityPublishedTrait.php @@ -2,6 +2,7 @@ namespace Drupal\Core\Entity; +use Drupal\Core\Entity\Exception\InvalidEntityTypeDefinitionException; use Drupal\Core\Field\BaseFieldDefinition; use Drupal\Core\StringTranslation\TranslatableMarkup; @@ -17,11 +18,21 @@ * The entity type to add the publishing status field to. * * @return \Drupal\Core\Field\BaseFieldDefinition[] - * Array of base field definitions. + * An array of base field definitions. + * + * @throws \Drupal\Core\Entity\Exception\InvalidEntityTypeDefinitionException + * Thrown when the entity type does not implement EntityPublishedInterface + * or if it does not have a "published" entity key. */ public static function publishedBaseFieldDefinitions(EntityTypeInterface $entity_type) { - $key = static::getPublishedEntityKey($entity_type); - return [$key => BaseFieldDefinition::create('boolean') + if (!is_subclass_of($entity_type->getClass(), EntityPublishedInterface::class)) { + throw new InvalidEntityTypeDefinitionException('The entity type ' . $entity_type->id() . ' does not implement \Drupal\Core\Entity\EntityPublishedInterface.'); + } + if (!$entity_type->hasKey('published')) { + throw new InvalidEntityTypeDefinitionException('The entity type ' . $entity_type->id() . ' does not have a "published" entity key.'); + } + + return [$entity_type->getKey('published') => BaseFieldDefinition::create('boolean') ->setLabel(new TranslatableMarkup('Publishing status')) ->setDescription(new TranslatableMarkup('A boolean indicating the published state.')) ->setRevisionable(TRUE) @@ -33,7 +44,7 @@ public static function publishedBaseFieldDefinitions(EntityTypeInterface $entity * {@inheritdoc} */ public function isPublished() { - $key = static::getPublishedEntityKey($this->getEntityType()); + $key = $this->getEntityType()->getKey('published'); return (bool) $this->get($key)->value; } @@ -64,7 +75,7 @@ public function setPublished($published) { * {@inheritdoc} */ public function publish() { - $key = static::getPublishedEntityKey($this->getEntityType()); + $key = $this->getEntityType()->getKey('published'); $this->set($key, TRUE); return $this; @@ -74,26 +85,10 @@ public function publish() { * {@inheritdoc} */ public function unpublish() { - $key = static::getPublishedEntityKey($this->getEntityType()); + $key = $this->getEntityType()->getKey('published'); $this->set($key, FALSE); return $this; } - /** - * Used to determine which key is used to represent the published state. - * - * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type - * The entity type to find the published key for. - * - * @return string - * The configured entity type definition key for 'status', or defaults to - * 'status'. - * - * @see \Drupal\Core\Entity\Annotation\ContentEntityType - */ - protected static function getPublishedEntityKey(EntityTypeInterface $entity_type) { - return $entity_type->getKey('status') ?: 'status'; - } - } diff --git a/core/lib/Drupal/Core/Entity/Exception/InvalidEntityTypeDefinitionException.php b/core/lib/Drupal/Core/Entity/Exception/InvalidEntityTypeDefinitionException.php new file mode 100644 index 0000000..ad72a5b --- /dev/null +++ b/core/lib/Drupal/Core/Entity/Exception/InvalidEntityTypeDefinitionException.php @@ -0,0 +1,8 @@ +getEntityType('comment'); + $keys = $entity_type->getKeys(); + $keys['published'] = 'status'; + $entity_type->set('entity_keys', $keys); + $definition_update_manager->updateEntityType($entity_type); +} + +/** * @} End of "addtogroup updates-8.3.x". */ diff --git a/core/modules/comment/src/CommentInterface.php b/core/modules/comment/src/CommentInterface.php index a35b7ad..1f69e9a 100644 --- a/core/modules/comment/src/CommentInterface.php +++ b/core/modules/comment/src/CommentInterface.php @@ -193,18 +193,13 @@ public function getCreatedTime(); public function setCreatedTime($created); /** - * Checks if the comment is published. - * - * @return bool - * TRUE if the comment is published. - */ - public function isPublished(); - - /** * Returns the comment's status. * * @return int * One of CommentInterface::PUBLISHED or CommentInterface::NOT_PUBLISHED + * + * @deprecated in Drupal 8.3.0, will be removed before Drupal 9.0.0. Use + * \Drupal\Core\Entity\EntityPublishedInterface::isPublished() instead. */ public function getStatus(); diff --git a/core/modules/comment/src/Entity/Comment.php b/core/modules/comment/src/Entity/Comment.php index d37da4a..df15cc0 100644 --- a/core/modules/comment/src/Entity/Comment.php +++ b/core/modules/comment/src/Entity/Comment.php @@ -51,7 +51,8 @@ * "bundle" = "comment_type", * "label" = "subject", * "langcode" = "langcode", - * "uuid" = "uuid" + * "uuid" = "uuid", + * "published" = "status", * }, * links = { * "canonical" = "/comment/{comment}", diff --git a/core/modules/node/node.install b/core/modules/node/node.install index 720a7e0..2751b2c 100644 --- a/core/modules/node/node.install +++ b/core/modules/node/node.install @@ -222,6 +222,11 @@ function node_update_8003() { } /** + * @addtogroup updates-8.3.x + * @{ + */ + +/** * Change {node_access}.fallback from an int to a tinyint as it is a boolean. */ function node_update_8300() { @@ -234,3 +239,19 @@ function node_update_8300() { 'size' => 'tiny', ]); } + +/** + * Set the 'published' entity key. + */ +function node_update_8301() { + $definition_update_manager = \Drupal::entityDefinitionUpdateManager(); + $entity_type = $definition_update_manager->getEntityType('node'); + $keys = $entity_type->getKeys(); + $keys['published'] = 'status'; + $entity_type->set('entity_keys', $keys); + $definition_update_manager->updateEntityType($entity_type); +} + +/** + * @} End of "addtogroup updates-8.3.x". + */ diff --git a/core/modules/node/src/Entity/Node.php b/core/modules/node/src/Entity/Node.php index d4e4715..f034957 100644 --- a/core/modules/node/src/Entity/Node.php +++ b/core/modules/node/src/Entity/Node.php @@ -56,6 +56,7 @@ * "langcode" = "langcode", * "uuid" = "uuid", * "status" = "status", + * "published" = "status", * "uid" = "uid", * }, * bundle_entity_type = "node_type",