PageRenderTime 50ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/core/lib/Drupal/Core/Entity/ContentEntityType.php

http://github.com/drupal/drupal
PHP | 138 lines | 62 code | 17 blank | 59 comment | 20 complexity | afb42288d4d22c97c9e7319512da7a93 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  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. * @var array
  17. */
  18. protected $requiredRevisionMetadataKeys = [];
  19. /**
  20. * {@inheritdoc}
  21. */
  22. public function __construct($definition) {
  23. parent::__construct($definition);
  24. $this->handlers += [
  25. 'storage' => 'Drupal\Core\Entity\Sql\SqlContentEntityStorage',
  26. 'view_builder' => 'Drupal\Core\Entity\EntityViewBuilder',
  27. ];
  28. // Only new instances should provide the required revision metadata keys.
  29. // The cached instances should return only what already has been stored
  30. // under the property $revision_metadata_keys. The BC layer in
  31. // ::getRevisionMetadataKeys() has to detect if the revision metadata keys
  32. // have been provided by the entity type annotation, therefore we add keys
  33. // to the property $requiredRevisionMetadataKeys only if those keys aren't
  34. // set in the entity type annotation.
  35. if (!isset($this->revision_metadata_keys['revision_default'])) {
  36. $this->requiredRevisionMetadataKeys['revision_default'] = 'revision_default';
  37. }
  38. // Add the required revision metadata fields here instead in the getter
  39. // method, so that they are serialized as part of the object even if the
  40. // getter method doesn't get called. This allows the list to be further
  41. // extended. Only new instances of the class will contain the new list,
  42. // while the cached instances contain the previous version of the list.
  43. $this->revision_metadata_keys += $this->requiredRevisionMetadataKeys;
  44. }
  45. /**
  46. * {@inheritdoc}
  47. */
  48. public function getConfigDependencyKey() {
  49. return 'content';
  50. }
  51. /**
  52. * {@inheritdoc}
  53. *
  54. * @throws \InvalidArgumentException
  55. * If the provided class does not implement
  56. * \Drupal\Core\Entity\ContentEntityStorageInterface.
  57. *
  58. * @see \Drupal\Core\Entity\ContentEntityStorageInterface
  59. */
  60. protected function checkStorageClass($class) {
  61. $required_interface = ContentEntityStorageInterface::class;
  62. if (!is_subclass_of($class, $required_interface)) {
  63. throw new \InvalidArgumentException("$class does not implement $required_interface");
  64. }
  65. }
  66. /**
  67. * {@inheritdoc}
  68. */
  69. public function getRevisionMetadataKeys($include_backwards_compatibility_field_names = TRUE) {
  70. // Provide backwards compatibility in case the revision metadata keys are
  71. // not defined in the entity annotation.
  72. if ((!$this->revision_metadata_keys || ($this->revision_metadata_keys == $this->requiredRevisionMetadataKeys)) && $include_backwards_compatibility_field_names) {
  73. $base_fields = \Drupal::service('entity_field.manager')->getBaseFieldDefinitions($this->id());
  74. if ((isset($base_fields['revision_uid']) && $revision_user = 'revision_uid') || (isset($base_fields['revision_user']) && $revision_user = 'revision_user')) {
  75. @trigger_error('The revision_user revision metadata key is not set for entity type: ' . $this->id . ' See: https://www.drupal.org/node/2831499', E_USER_DEPRECATED);
  76. $this->revision_metadata_keys['revision_user'] = $revision_user;
  77. }
  78. if ((isset($base_fields['revision_timestamp']) && $revision_timestamp = 'revision_timestamp') || (isset($base_fields['revision_created'])) && $revision_timestamp = 'revision_created') {
  79. @trigger_error('The revision_created revision metadata key is not set for entity type: ' . $this->id . ' See: https://www.drupal.org/node/2831499', E_USER_DEPRECATED);
  80. $this->revision_metadata_keys['revision_created'] = $revision_timestamp;
  81. }
  82. if ((isset($base_fields['revision_log']) && $revision_log = 'revision_log') || (isset($base_fields['revision_log_message']) && $revision_log = 'revision_log_message')) {
  83. @trigger_error('The revision_log_message revision metadata key is not set for entity type: ' . $this->id . ' See: https://www.drupal.org/node/2831499', E_USER_DEPRECATED);
  84. $this->revision_metadata_keys['revision_log_message'] = $revision_log;
  85. }
  86. }
  87. return $this->revision_metadata_keys;
  88. }
  89. /**
  90. * {@inheritdoc}
  91. */
  92. public function getRevisionMetadataKey($key) {
  93. $keys = $this->getRevisionMetadataKeys();
  94. return isset($keys[$key]) ? $keys[$key] : FALSE;
  95. }
  96. /**
  97. * {@inheritdoc}
  98. */
  99. public function hasRevisionMetadataKey($key) {
  100. $keys = $this->getRevisionMetadataKeys();
  101. return isset($keys[$key]);
  102. }
  103. /**
  104. * {@inheritdoc}
  105. */
  106. public function setRevisionMetadataKey($key, $field_name) {
  107. if ($field_name !== NULL) {
  108. // Update the property holding the required revision metadata keys,
  109. // which is used by the BC layer for retrieving the revision metadata
  110. // keys.
  111. // @see \Drupal\Core\Entity\ContentEntityType::getRevisionMetadataKeys()
  112. $this->requiredRevisionMetadataKeys[$key] = $field_name;
  113. // Add the new revision metadata key.
  114. $this->revision_metadata_keys[$key] = $field_name;
  115. }
  116. else {
  117. unset($this->requiredRevisionMetadataKeys[$key], $this->revision_metadata_keys[$key]);
  118. }
  119. return $this;
  120. }
  121. }