trait EntityPublishedTrait

Same name and namespace in other branches
  1. 8.9.x core/lib/Drupal/Core/Entity/EntityPublishedTrait.php \Drupal\Core\Entity\EntityPublishedTrait
  2. 10 core/lib/Drupal/Core/Entity/EntityPublishedTrait.php \Drupal\Core\Entity\EntityPublishedTrait
  3. 11.x core/lib/Drupal/Core/Entity/EntityPublishedTrait.php \Drupal\Core\Entity\EntityPublishedTrait

Provides a trait for published status.

Hierarchy

5 files declare their use of EntityPublishedTrait
Comment.php in core/modules/comment/src/Entity/Comment.php
EntityTestMulRevPub.php in core/modules/system/tests/modules/entity_test/src/Entity/EntityTestMulRevPub.php
EntityTestMulWithRevisionLogPub.php in core/modules/system/tests/modules/entity_test_revlog/src/Entity/EntityTestMulWithRevisionLogPub.php
EntityTestRevPub.php in core/modules/system/tests/modules/entity_test/src/Entity/EntityTestRevPub.php
PathAlias.php in core/modules/path_alias/src/Entity/PathAlias.php

File

core/lib/Drupal/Core/Entity/EntityPublishedTrait.php, line 12

Namespace

Drupal\Core\Entity
View source
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[]
     *   An array of base field definitions.
     *
     * @throws \Drupal\Core\Entity\Exception\UnsupportedEntityTypeDefinitionException
     *   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) {
        if (!is_subclass_of($entity_type->getClass(), EntityPublishedInterface::class)) {
            throw new UnsupportedEntityTypeDefinitionException('The entity type ' . $entity_type->id() . ' does not implement \\Drupal\\Core\\Entity\\EntityPublishedInterface.');
        }
        if (!$entity_type->hasKey('published')) {
            throw new UnsupportedEntityTypeDefinitionException('The entity type ' . $entity_type->id() . ' does not have a "published" entity key.');
        }
        return [
            $entity_type->getKey('published') => BaseFieldDefinition::create('boolean')->setLabel(new TranslatableMarkup('Published'))
                ->setRevisionable(TRUE)
                ->setTranslatable(TRUE)
                ->setDefaultValue(TRUE),
        ];
    }
    
    /**
     * {@inheritdoc}
     */
    public function isPublished() {
        return (bool) $this->getEntityKey('published');
    }
    
    /**
     * {@inheritdoc}
     */
    public function setPublished() {
        $key = $this->getEntityType()
            ->getKey('published');
        $this->set($key, TRUE);
        return $this;
    }
    
    /**
     * {@inheritdoc}
     */
    public function setUnpublished() {
        $key = $this->getEntityType()
            ->getKey('published');
        $this->set($key, FALSE);
        return $this;
    }

}

Members

Title Sort descending Modifiers Object type Summary
EntityPublishedTrait::isPublished public function
EntityPublishedTrait::publishedBaseFieldDefinitions public static function Returns an array of base field definitions for publishing status.
EntityPublishedTrait::setPublished public function
EntityPublishedTrait::setUnpublished public function

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.