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