Security update for Core, with self-updated composer
[yaffs-website] / web / core / lib / Drupal / Core / Entity / EntityPublishedTrait.php
1 <?php
2
3 namespace Drupal\Core\Entity;
4
5 use Drupal\Core\Entity\Exception\UnsupportedEntityTypeDefinitionException;
6 use Drupal\Core\Field\BaseFieldDefinition;
7 use Drupal\Core\StringTranslation\TranslatableMarkup;
8
9 /**
10  * Provides a trait for published status.
11  */
12 trait EntityPublishedTrait {
13
14   /**
15    * Returns an array of base field definitions for publishing status.
16    *
17    * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
18    *   The entity type to add the publishing status field to.
19    *
20    * @return \Drupal\Core\Field\BaseFieldDefinition[]
21    *   An array of base field definitions.
22    *
23    * @throws \Drupal\Core\Entity\Exception\UnsupportedEntityTypeDefinitionException
24    *   Thrown when the entity type does not implement EntityPublishedInterface
25    *   or if it does not have a "published" entity key.
26    */
27   public static function publishedBaseFieldDefinitions(EntityTypeInterface $entity_type) {
28     if (!is_subclass_of($entity_type->getClass(), EntityPublishedInterface::class)) {
29       throw new UnsupportedEntityTypeDefinitionException('The entity type ' . $entity_type->id() . ' does not implement \Drupal\Core\Entity\EntityPublishedInterface.');
30     }
31     if (!$entity_type->hasKey('published')) {
32       throw new UnsupportedEntityTypeDefinitionException('The entity type ' . $entity_type->id() . ' does not have a "published" entity key.');
33     }
34
35     return [
36       $entity_type->getKey('published') => BaseFieldDefinition::create('boolean')
37         ->setLabel(new TranslatableMarkup('Published'))
38         ->setRevisionable(TRUE)
39         ->setTranslatable(TRUE)
40         ->setDefaultValue(TRUE),
41     ];
42   }
43
44   /**
45    * {@inheritdoc}
46    */
47   public function isPublished() {
48     $key = $this->getEntityType()->getKey('published');
49     return (bool) $this->get($key)->value;
50   }
51
52   /**
53    * {@inheritdoc}
54    */
55   public function setPublished($published = NULL) {
56     if ($published !== NULL) {
57       @trigger_error('The $published parameter is deprecated since version 8.3.x and will be removed in 9.0.0.', E_USER_DEPRECATED);
58       $value = (bool) $published;
59     }
60     else {
61       $value = TRUE;
62     }
63     $key = $this->getEntityType()->getKey('published');
64     $this->set($key, $value);
65
66     return $this;
67   }
68
69   /**
70    * {@inheritdoc}
71    */
72   public function setUnpublished() {
73     $key = $this->getEntityType()->getKey('published');
74     $this->set($key, FALSE);
75
76     return $this;
77   }
78
79 }