diff --git a/core/lib/Drupal/Core/Entity/EntityPublishedTrait.php b/core/lib/Drupal/Core/Entity/EntityPublishedTrait.php index 217ed2c..96b1720 100644 --- a/core/lib/Drupal/Core/Entity/EntityPublishedTrait.php +++ b/core/lib/Drupal/Core/Entity/EntityPublishedTrait.php @@ -13,27 +13,36 @@ /** * {@inheritdoc} */ - public static function publishedBaseFieldDefinitions(EntityTypeInterface $entity_type) { - $key = $entity_type->hasKey('status') ? $entity_type->getKey('status') : 'status'; - return [$key => BaseFieldDefinition::create('boolean') + public static function publishedBaseFieldDefinitions(EntityTypeInterface $entityType) { + $definitions = []; + + $key = self::getPublishedEntityKey($entityType); + $definitions[$key] = BaseFieldDefinition::create('boolean') ->setLabel(new TranslatableMarkup('Publishing status')) ->setDescription(new TranslatableMarkup('A boolean indicating the published state.')) ->setRevisionable(TRUE) ->setTranslatable(TRUE) - ->setDefaultValue(TRUE)]; + ->setDefaultValue(TRUE); + + return $definitions; } /** * {@inheritdoc} */ public function isPublished() { - $status = $this->getEntityKey('status'); - - return (bool) (isset($status) ? $status : $this->get('status')->value); + $key = $this->getPublishedEntityKey($this->getEntityType()); + return (bool) $this->get($key)->value; } /** - * {@inheritdoc} + * 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) { if ((bool) $published) { @@ -50,7 +59,7 @@ public function setPublished($published) { * {@inheritdoc} */ public function publish() { - $key = $this->getEntityType()->getKey('status') ?: 'status'; + $key = $this->getPublishedEntityKey($this->getEntityType()); $this->set($key, TRUE); return $this; @@ -60,10 +69,25 @@ public function publish() { * {@inheritdoc} */ public function unpublish() { - $key = $this->getEntityType()->getKey('status') ?: 'status'; + $key = $this->getPublishedEntityKey($this->getEntityType()); $this->set($key, FALSE); return $this; } + /** + * Used to determine which key is used to represent the published state. + * + * @param EntityTypeInterface $entityType + * 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 $entityType) { + return $entityType->getKey('status') ?: 'status'; + } + }