ConfigEntityType.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. namespace Drupal\Core\Config\Entity;
  3. use Drupal\Core\Config\Entity\Exception\ConfigEntityStorageClassException;
  4. use Drupal\Core\Entity\EntityType;
  5. use Drupal\Core\Config\ConfigPrefixLengthException;
  6. /**
  7. * Provides an implementation of a configuration entity type and its metadata.
  8. */
  9. class ConfigEntityType extends EntityType implements ConfigEntityTypeInterface {
  10. /**
  11. * The config prefix set in the configuration entity type annotation.
  12. *
  13. * @see \Drupal\Core\Config\Entity\ConfigEntityTypeInterface::getConfigPrefix()
  14. */
  15. protected $config_prefix;
  16. /**
  17. * {@inheritdoc}
  18. */
  19. protected $static_cache = FALSE;
  20. /**
  21. * Keys that are stored key value store for fast lookup.
  22. *
  23. * @var array
  24. */
  25. protected $lookup_keys = [];
  26. /**
  27. * The list of configuration entity properties to export from the annotation.
  28. *
  29. * @var array
  30. */
  31. protected $config_export = [];
  32. /**
  33. * The result of merging config_export annotation with the defaults.
  34. *
  35. * This is stored on the class so that it does not have to be recalculated.
  36. *
  37. * @var array
  38. */
  39. protected $mergedConfigExport = [];
  40. /**
  41. * {@inheritdoc}
  42. *
  43. * @throws \Drupal\Core\Config\Entity\Exception\ConfigEntityStorageClassException
  44. * Exception thrown when the provided class is not an instance of
  45. * \Drupal\Core\Config\Entity\ConfigEntityStorage.
  46. */
  47. public function __construct($definition) {
  48. // Ensure a default list cache tag is set; do this before calling the parent
  49. // constructor, because we want "Configuration System style" cache tags.
  50. if (empty($this->list_cache_tags)) {
  51. $this->list_cache_tags = ['config:' . $definition['id'] . '_list'];
  52. }
  53. parent::__construct($definition);
  54. // Always add a default 'uuid' key.
  55. $this->entity_keys['uuid'] = 'uuid';
  56. $this->entity_keys['langcode'] = 'langcode';
  57. $this->handlers += [
  58. 'storage' => 'Drupal\Core\Config\Entity\ConfigEntityStorage',
  59. ];
  60. $this->lookup_keys[] = 'uuid';
  61. }
  62. /**
  63. * {@inheritdoc}
  64. */
  65. public function getConfigPrefix() {
  66. // Ensure that all configuration entities are prefixed by the name of the
  67. // module that provides the configuration entity type.
  68. if (isset($this->config_prefix)) {
  69. $config_prefix = $this->provider . '.' . $this->config_prefix;
  70. }
  71. else {
  72. $config_prefix = $this->provider . '.' . $this->id();
  73. }
  74. if (strlen($config_prefix) > static::PREFIX_LENGTH) {
  75. throw new ConfigPrefixLengthException("The configuration file name prefix $config_prefix exceeds the maximum character limit of " . static::PREFIX_LENGTH);
  76. }
  77. return $config_prefix;
  78. }
  79. /**
  80. * {@inheritdoc}
  81. */
  82. public function getBaseTable() {
  83. return FALSE;
  84. }
  85. /**
  86. * {@inheritdoc}
  87. */
  88. public function getRevisionDataTable() {
  89. return FALSE;
  90. }
  91. /**
  92. * {@inheritdoc}
  93. */
  94. public function getRevisionTable() {
  95. return FALSE;
  96. }
  97. /**
  98. * {@inheritdoc}
  99. */
  100. public function getDataTable() {
  101. return FALSE;
  102. }
  103. /**
  104. * {@inheritdoc}
  105. */
  106. public function getConfigDependencyKey() {
  107. return 'config';
  108. }
  109. /**
  110. * {@inheritdoc}
  111. *
  112. * @throws \Drupal\Core\Config\Entity\Exception\ConfigEntityStorageClassException
  113. * Exception thrown when the provided class is not an instance of
  114. * \Drupal\Core\Config\Entity\ConfigEntityStorage.
  115. *
  116. * @see \Drupal\Core\Config\Entity\ConfigEntityStorage
  117. */
  118. protected function checkStorageClass($class) {
  119. if (!is_a($class, 'Drupal\Core\Config\Entity\ConfigEntityStorage', TRUE)) {
  120. throw new ConfigEntityStorageClassException("$class is not \\Drupal\\Core\\Config\\Entity\\ConfigEntityStorage or it does not extend it");
  121. }
  122. }
  123. /**
  124. * {@inheritdoc}
  125. */
  126. public function getPropertiesToExport() {
  127. if (!empty($this->config_export)) {
  128. if (empty($this->mergedConfigExport)) {
  129. // Always add default properties to be exported.
  130. $this->mergedConfigExport = [
  131. 'uuid' => 'uuid',
  132. 'langcode' => 'langcode',
  133. 'status' => 'status',
  134. 'dependencies' => 'dependencies',
  135. 'third_party_settings' => 'third_party_settings',
  136. '_core' => '_core',
  137. ];
  138. foreach ($this->config_export as $property => $name) {
  139. if (is_numeric($property)) {
  140. $this->mergedConfigExport[$name] = $name;
  141. }
  142. else {
  143. $this->mergedConfigExport[$property] = $name;
  144. }
  145. }
  146. }
  147. return $this->mergedConfigExport;
  148. }
  149. return NULL;
  150. }
  151. /**
  152. * {@inheritdoc}
  153. */
  154. public function getLookupKeys() {
  155. return $this->lookup_keys;
  156. }
  157. }