3 namespace Drupal\Core\Entity;
6 * Provides an implementation of a content entity type and its metadata.
8 class ContentEntityType extends EntityType implements ContentEntityTypeInterface {
11 * An array of entity revision metadata keys.
15 protected $revision_metadata_keys = [];
18 * The required revision metadata keys.
20 * This property should only be filled in the constructor. This ensures that
21 * only new instances get newly added required revision metadata keys.
22 * Unserialized objects will only retrieve the keys that they already have
27 protected $requiredRevisionMetadataKeys = [];
32 public function __construct($definition) {
33 parent::__construct($definition);
36 'storage' => 'Drupal\Core\Entity\Sql\SqlContentEntityStorage',
37 'view_builder' => 'Drupal\Core\Entity\EntityViewBuilder',
40 // Only new instances should provide the required revision metadata keys.
41 // The cached instances should return only what already has been stored
42 // under the property $revision_metadata_keys. The BC layer in
43 // ::getRevisionMetadataKeys() has to detect if the revision metadata keys
44 // have been provided by the entity type annotation, therefore we add keys
45 // to the property $requiredRevisionMetadataKeys only if those keys aren't
46 // set in the entity type annotation.
47 if (!isset($this->revision_metadata_keys['revision_default'])) {
48 $this->requiredRevisionMetadataKeys['revision_default'] = 'revision_default';
51 // Add the required revision metadata fields here instead in the getter
52 // method, so that they are serialized as part of the object even if the
53 // getter method doesn't get called. This allows the list to be further
54 // extended. Only new instances of the class will contain the new list,
55 // while the cached instances contain the previous version of the list.
56 $this->revision_metadata_keys += $this->requiredRevisionMetadataKeys;
62 public function getConfigDependencyKey() {
69 * @throws \InvalidArgumentException
70 * If the provided class does not implement
71 * \Drupal\Core\Entity\ContentEntityStorageInterface.
73 * @see \Drupal\Core\Entity\ContentEntityStorageInterface
75 protected function checkStorageClass($class) {
76 $required_interface = ContentEntityStorageInterface::class;
77 if (!is_subclass_of($class, $required_interface)) {
78 throw new \InvalidArgumentException("$class does not implement $required_interface");
85 public function getRevisionMetadataKeys($include_backwards_compatibility_field_names = TRUE) {
86 // Provide backwards compatibility in case the revision metadata keys are
87 // not defined in the entity annotation.
88 if ((!$this->revision_metadata_keys || ($this->revision_metadata_keys == $this->requiredRevisionMetadataKeys)) && $include_backwards_compatibility_field_names) {
89 $base_fields = \Drupal::service('entity_field.manager')->getBaseFieldDefinitions($this->id());
90 if ((isset($base_fields['revision_uid']) && $revision_user = 'revision_uid') || (isset($base_fields['revision_user']) && $revision_user = 'revision_user')) {
91 @trigger_error('The revision_user revision metadata key is not set.', E_USER_DEPRECATED);
92 $this->revision_metadata_keys['revision_user'] = $revision_user;
94 if ((isset($base_fields['revision_timestamp']) && $revision_timestamp = 'revision_timestamp') || (isset($base_fields['revision_created'])) && $revision_timestamp = 'revision_created') {
95 @trigger_error('The revision_created revision metadata key is not set.', E_USER_DEPRECATED);
96 $this->revision_metadata_keys['revision_created'] = $revision_timestamp;
98 if ((isset($base_fields['revision_log']) && $revision_log = 'revision_log') || (isset($base_fields['revision_log_message']) && $revision_log = 'revision_log_message')) {
99 @trigger_error('The revision_log_message revision metadata key is not set.', E_USER_DEPRECATED);
100 $this->revision_metadata_keys['revision_log_message'] = $revision_log;
103 return $this->revision_metadata_keys;
109 public function getRevisionMetadataKey($key) {
110 $keys = $this->getRevisionMetadataKeys();
111 return isset($keys[$key]) ? $keys[$key] : FALSE;
117 public function hasRevisionMetadataKey($key) {
118 $keys = $this->getRevisionMetadataKeys();
119 return isset($keys[$key]);