You are here

public static function EntityPublishedTrait::publishedBaseFieldDefinitions in Drupal 10

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Entity/EntityPublishedTrait.php \Drupal\Core\Entity\EntityPublishedTrait::publishedBaseFieldDefinitions()
  2. 9 core/lib/Drupal/Core/Entity/EntityPublishedTrait.php \Drupal\Core\Entity\EntityPublishedTrait::publishedBaseFieldDefinitions()

Returns an array of base field definitions for publishing status.

Parameters

\Drupal\Core\Entity\EntityTypeInterface $entity_type: The entity type to add the publishing status field to.

Return value

\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.

5 calls to EntityPublishedTrait::publishedBaseFieldDefinitions()
EntityTestMulRevPub::baseFieldDefinitions in core/modules/system/tests/modules/entity_test/src/Entity/EntityTestMulRevPub.php
Provides base field definitions for an entity type.
EntityTestMulWithRevisionLogPub::baseFieldDefinitions in core/modules/system/tests/modules/entity_test_revlog/src/Entity/EntityTestMulWithRevisionLogPub.php
Provides base field definitions for an entity type.
EntityTestRevPub::baseFieldDefinitions in core/modules/system/tests/modules/entity_test/src/Entity/EntityTestRevPub.php
Provides base field definitions for an entity type.
MenuLinkContent::baseFieldDefinitions in core/modules/menu_link_content/src/Entity/MenuLinkContent.php
Provides base field definitions for an entity type.
PathAlias::baseFieldDefinitions in core/modules/path_alias/src/Entity/PathAlias.php
Provides base field definitions for an entity type.

File

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

Class

EntityPublishedTrait
Provides a trait for published status.

Namespace

Drupal\Core\Entity

Code

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),
  ];
}