38714051762d844f3bac24406e2a5d9ddd91f2bb
[yaffs-website] / web / core / lib / Drupal / Core / Entity / ContentEntityType.php
1 <?php
2
3 namespace Drupal\Core\Entity;
4
5 /**
6  * Provides an implementation of a content entity type and its metadata.
7  */
8 class ContentEntityType extends EntityType implements ContentEntityTypeInterface {
9
10   /**
11    * An array of entity revision metadata keys.
12    *
13    * @var array
14    */
15   protected $revision_metadata_keys = [];
16
17   /**
18    * The required revision metadata keys.
19    *
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
23    * been cached with.
24    *
25    * @var array
26    */
27   protected $requiredRevisionMetadataKeys = [];
28
29   /**
30    * {@inheritdoc}
31    */
32   public function __construct($definition) {
33     parent::__construct($definition);
34
35     $this->handlers += [
36       'storage' => 'Drupal\Core\Entity\Sql\SqlContentEntityStorage',
37       'view_builder' => 'Drupal\Core\Entity\EntityViewBuilder',
38     ];
39
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';
49     }
50
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;
57   }
58
59   /**
60    * {@inheritdoc}
61    */
62   public function getConfigDependencyKey() {
63     return 'content';
64   }
65
66   /**
67    * {@inheritdoc}
68    *
69    * @throws \InvalidArgumentException
70    *   If the provided class does not implement
71    *   \Drupal\Core\Entity\ContentEntityStorageInterface.
72    *
73    * @see \Drupal\Core\Entity\ContentEntityStorageInterface
74    */
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");
79     }
80   }
81
82   /**
83    * {@inheritdoc}
84    */
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;
93       }
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;
97       }
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;
101       }
102     }
103     return $this->revision_metadata_keys;
104   }
105
106   /**
107    * {@inheritdoc}
108    */
109   public function getRevisionMetadataKey($key) {
110     $keys = $this->getRevisionMetadataKeys();
111     return isset($keys[$key]) ? $keys[$key] : FALSE;
112   }
113
114   /**
115    * {@inheritdoc}
116    */
117   public function hasRevisionMetadataKey($key) {
118     $keys = $this->getRevisionMetadataKeys();
119     return isset($keys[$key]);
120   }
121
122 }