ContentEntityType.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. namespace Drupal\Core\Entity;
  3. /**
  4. * Provides an implementation of a content entity type and its metadata.
  5. */
  6. class ContentEntityType extends EntityType implements ContentEntityTypeInterface {
  7. /**
  8. * An array of entity revision metadata keys.
  9. *
  10. * @var array
  11. */
  12. protected $revision_metadata_keys = [];
  13. /**
  14. * The required revision metadata keys.
  15. *
  16. * This property should only be filled in the constructor. This ensures that
  17. * only new instances get newly added required revision metadata keys.
  18. * Unserialized objects will only retrieve the keys that they already have
  19. * been cached with.
  20. *
  21. * @var array
  22. */
  23. protected $requiredRevisionMetadataKeys = [];
  24. /**
  25. * {@inheritdoc}
  26. */
  27. public function __construct($definition) {
  28. parent::__construct($definition);
  29. $this->handlers += [
  30. 'storage' => 'Drupal\Core\Entity\Sql\SqlContentEntityStorage',
  31. 'view_builder' => 'Drupal\Core\Entity\EntityViewBuilder',
  32. ];
  33. // Only new instances should provide the required revision metadata keys.
  34. // The cached instances should return only what already has been stored
  35. // under the property $revision_metadata_keys. The BC layer in
  36. // ::getRevisionMetadataKeys() has to detect if the revision metadata keys
  37. // have been provided by the entity type annotation, therefore we add keys
  38. // to the property $requiredRevisionMetadataKeys only if those keys aren't
  39. // set in the entity type annotation.
  40. if (!isset($this->revision_metadata_keys['revision_default'])) {
  41. $this->requiredRevisionMetadataKeys['revision_default'] = 'revision_default';
  42. }
  43. // Add the required revision metadata fields here instead in the getter
  44. // method, so that they are serialized as part of the object even if the
  45. // getter method doesn't get called. This allows the list to be further
  46. // extended. Only new instances of the class will contain the new list,
  47. // while the cached instances contain the previous version of the list.
  48. $this->revision_metadata_keys += $this->requiredRevisionMetadataKeys;
  49. }
  50. /**
  51. * {@inheritdoc}
  52. */
  53. public function getConfigDependencyKey() {
  54. return 'content';
  55. }
  56. /**
  57. * {@inheritdoc}
  58. *
  59. * @throws \InvalidArgumentException
  60. * If the provided class does not implement
  61. * \Drupal\Core\Entity\ContentEntityStorageInterface.
  62. *
  63. * @see \Drupal\Core\Entity\ContentEntityStorageInterface
  64. */
  65. protected function checkStorageClass($class) {
  66. $required_interface = ContentEntityStorageInterface::class;
  67. if (!is_subclass_of($class, $required_interface)) {
  68. throw new \InvalidArgumentException("$class does not implement $required_interface");
  69. }
  70. }
  71. /**
  72. * {@inheritdoc}
  73. */
  74. public function getRevisionMetadataKeys($include_backwards_compatibility_field_names = TRUE) {
  75. // Provide backwards compatibility in case the revision metadata keys are
  76. // not defined in the entity annotation.
  77. if ((!$this->revision_metadata_keys || ($this->revision_metadata_keys == $this->requiredRevisionMetadataKeys)) && $include_backwards_compatibility_field_names) {
  78. $base_fields = \Drupal::service('entity_field.manager')->getBaseFieldDefinitions($this->id());
  79. if ((isset($base_fields['revision_uid']) && $revision_user = 'revision_uid') || (isset($base_fields['revision_user']) && $revision_user = 'revision_user')) {
  80. @trigger_error('The revision_user revision metadata key is not set.', E_USER_DEPRECATED);
  81. $this->revision_metadata_keys['revision_user'] = $revision_user;
  82. }
  83. if ((isset($base_fields['revision_timestamp']) && $revision_timestamp = 'revision_timestamp') || (isset($base_fields['revision_created'])) && $revision_timestamp = 'revision_created') {
  84. @trigger_error('The revision_created revision metadata key is not set.', E_USER_DEPRECATED);
  85. $this->revision_metadata_keys['revision_created'] = $revision_timestamp;
  86. }
  87. if ((isset($base_fields['revision_log']) && $revision_log = 'revision_log') || (isset($base_fields['revision_log_message']) && $revision_log = 'revision_log_message')) {
  88. @trigger_error('The revision_log_message revision metadata key is not set.', E_USER_DEPRECATED);
  89. $this->revision_metadata_keys['revision_log_message'] = $revision_log;
  90. }
  91. }
  92. return $this->revision_metadata_keys;
  93. }
  94. /**
  95. * {@inheritdoc}
  96. */
  97. public function getRevisionMetadataKey($key) {
  98. $keys = $this->getRevisionMetadataKeys();
  99. return isset($keys[$key]) ? $keys[$key] : FALSE;
  100. }
  101. /**
  102. * {@inheritdoc}
  103. */
  104. public function hasRevisionMetadataKey($key) {
  105. $keys = $this->getRevisionMetadataKeys();
  106. return isset($keys[$key]);
  107. }
  108. }