Version 1
[yaffs-website] / web / core / modules / content_moderation / src / Entity / ContentModerationState.php
1 <?php
2
3 namespace Drupal\content_moderation\Entity;
4
5 use Drupal\content_moderation\ContentModerationStateInterface;
6 use Drupal\Core\Entity\ContentEntityBase;
7 use Drupal\Core\Entity\EntityTypeInterface;
8 use Drupal\Core\Field\BaseFieldDefinition;
9 use Drupal\Core\TypedData\TranslatableInterface;
10 use Drupal\user\UserInterface;
11
12 /**
13  * Defines the Content moderation state entity.
14  *
15  * @ContentEntityType(
16  *   id = "content_moderation_state",
17  *   label = @Translation("Content moderation state"),
18  *   label_singular = @Translation("content moderation state"),
19  *   label_plural = @Translation("content moderation states"),
20  *   label_count = @PluralTranslation(
21  *     singular = "@count content moderation state",
22  *     plural = "@count content moderation states"
23  *   ),
24  *   handlers = {
25  *     "storage_schema" = "Drupal\content_moderation\ContentModerationStateStorageSchema",
26  *     "views_data" = "\Drupal\views\EntityViewsData",
27  *     "access" = "Drupal\content_moderation\ContentModerationStateAccessControlHandler",
28  *   },
29  *   base_table = "content_moderation_state",
30  *   revision_table = "content_moderation_state_revision",
31  *   data_table = "content_moderation_state_field_data",
32  *   revision_data_table = "content_moderation_state_field_revision",
33  *   translatable = TRUE,
34  *   entity_keys = {
35  *     "id" = "id",
36  *     "revision" = "revision_id",
37  *     "uuid" = "uuid",
38  *     "uid" = "uid",
39  *     "langcode" = "langcode",
40  *   }
41  * )
42  */
43 class ContentModerationState extends ContentEntityBase implements ContentModerationStateInterface {
44
45   /**
46    * {@inheritdoc}
47    */
48   public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
49     $fields = parent::baseFieldDefinitions($entity_type);
50
51     $fields['uid'] = BaseFieldDefinition::create('entity_reference')
52       ->setLabel(t('User'))
53       ->setDescription(t('The username of the entity creator.'))
54       ->setSetting('target_type', 'user')
55       ->setDefaultValueCallback('Drupal\content_moderation\Entity\ContentModerationState::getCurrentUserId')
56       ->setTranslatable(TRUE)
57       ->setRevisionable(TRUE);
58
59     $fields['workflow'] = BaseFieldDefinition::create('entity_reference')
60       ->setLabel(t('Workflow'))
61       ->setDescription(t('The workflow the moderation state is in.'))
62       ->setSetting('target_type', 'workflow')
63       ->setRequired(TRUE)
64       ->setTranslatable(TRUE)
65       ->setRevisionable(TRUE);
66
67     $fields['moderation_state'] = BaseFieldDefinition::create('string')
68       ->setLabel(t('Moderation state'))
69       ->setDescription(t('The moderation state of the referenced content.'))
70       ->setRequired(TRUE)
71       ->setTranslatable(TRUE)
72       ->setRevisionable(TRUE);
73
74     $fields['content_entity_type_id'] = BaseFieldDefinition::create('string')
75       ->setLabel(t('Content entity type ID'))
76       ->setDescription(t('The ID of the content entity type this moderation state is for.'))
77       ->setRequired(TRUE)
78       ->setSetting('max_length', EntityTypeInterface::ID_MAX_LENGTH)
79       ->setRevisionable(TRUE);
80
81     $fields['content_entity_id'] = BaseFieldDefinition::create('integer')
82       ->setLabel(t('Content entity ID'))
83       ->setDescription(t('The ID of the content entity this moderation state is for.'))
84       ->setRequired(TRUE)
85       ->setRevisionable(TRUE);
86
87     $fields['content_entity_revision_id'] = BaseFieldDefinition::create('integer')
88       ->setLabel(t('Content entity revision ID'))
89       ->setDescription(t('The revision ID of the content entity this moderation state is for.'))
90       ->setRequired(TRUE)
91       ->setRevisionable(TRUE);
92
93     return $fields;
94   }
95
96   /**
97    * {@inheritdoc}
98    */
99   public function getOwner() {
100     return $this->get('uid')->entity;
101   }
102
103   /**
104    * {@inheritdoc}
105    */
106   public function getOwnerId() {
107     return $this->getEntityKey('uid');
108   }
109
110   /**
111    * {@inheritdoc}
112    */
113   public function setOwnerId($uid) {
114     $this->set('uid', $uid);
115     return $this;
116   }
117
118   /**
119    * {@inheritdoc}
120    */
121   public function setOwner(UserInterface $account) {
122     $this->set('uid', $account->id());
123     return $this;
124   }
125
126   /**
127    * Creates or updates an entity's moderation state whilst saving that entity.
128    *
129    * @param \Drupal\content_moderation\Entity\ContentModerationState $content_moderation_state
130    *   The content moderation entity content entity to create or save.
131    *
132    * @internal
133    *   This method should only be called as a result of saving the related
134    *   content entity.
135    */
136   public static function updateOrCreateFromEntity(ContentModerationState $content_moderation_state) {
137     $content_moderation_state->realSave();
138   }
139
140   /**
141    * Default value callback for the 'uid' base field definition.
142    *
143    * @see \Drupal\content_moderation\Entity\ContentModerationState::baseFieldDefinitions()
144    *
145    * @return array
146    *   An array of default values.
147    */
148   public static function getCurrentUserId() {
149     return [\Drupal::currentUser()->id()];
150   }
151
152   /**
153    * {@inheritdoc}
154    */
155   public function save() {
156     $related_entity = \Drupal::entityTypeManager()
157       ->getStorage($this->content_entity_type_id->value)
158       ->loadRevision($this->content_entity_revision_id->value);
159     if ($related_entity instanceof TranslatableInterface) {
160       $related_entity = $related_entity->getTranslation($this->activeLangcode);
161     }
162     $related_entity->moderation_state = $this->moderation_state;
163     return $related_entity->save();
164   }
165
166   /**
167    * Saves an entity permanently.
168    *
169    * When saving existing entities, the entity is assumed to be complete,
170    * partial updates of entities are not supported.
171    *
172    * @return int
173    *   Either SAVED_NEW or SAVED_UPDATED, depending on the operation performed.
174    *
175    * @throws \Drupal\Core\Entity\EntityStorageException
176    *   In case of failures an exception is thrown.
177    */
178   protected function realSave() {
179     return parent::save();
180   }
181
182 }