ConfigEntityType.php 5.3 KB

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