diff --git a/core/lib/Drupal/Core/Entity/EntityPublishedTrait.php b/core/lib/Drupal/Core/Entity/EntityPublishedTrait.php index 8871dba..df321d7 100644 --- a/core/lib/Drupal/Core/Entity/EntityPublishedTrait.php +++ b/core/lib/Drupal/Core/Entity/EntityPublishedTrait.php @@ -5,34 +5,53 @@ 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(t('A boolean indicating the published state.')) + ->setDescription(new TranslatableMarkup('A boolean indicating the published state.')) ->setRevisionable(TRUE) ->setTranslatable(TRUE) ->setDefaultValue(TRUE)]; } /** - * {@inheritdoc} + * Returns the published status of the entity. + * + * @return bool + * The published status of the entity. */ public function isPublished() { - return $this->getEntityType()->hasKey('status') ? - (bool) $this->getEntityKey('status') : - (bool) $this->get('status')->value; + $status = $this->getEntityKey('status'); + return (bool) ($status ?: $this->get('status')->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) { /** @var \Drupal\Core\Entity\ContentEntityTypeInterface $entity_type */ - $entity_type = $this->getEntityType(); - $key = $entity_type->hasKey('status') ? $entity_type->getKey('status') : 'status'; + $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);