Security update for Core, with self-updated composer
[yaffs-website] / web / core / lib / Drupal / Core / Config / Entity / ConfigEntityBundleBase.php
1 <?php
2
3 namespace Drupal\Core\Config\Entity;
4
5 use Drupal\Core\Config\ConfigNameException;
6 use Drupal\Core\Entity\EntityStorageInterface;
7
8 /**
9  * A base class for config entity types that act as bundles.
10  *
11  * Entity types that want to use this base class must use bundle_of in their
12  * annotation to specify for which entity type they are providing bundles for.
13  */
14 abstract class ConfigEntityBundleBase extends ConfigEntityBase {
15
16   /**
17    * Deletes display if a bundle is deleted.
18    */
19   protected function deleteDisplays() {
20     // Remove entity displays of the deleted bundle.
21     if ($displays = $this->loadDisplays('entity_view_display')) {
22       $storage = $this->entityManager()->getStorage('entity_view_display');
23       $storage->delete($displays);
24     }
25
26     // Remove entity form displays of the deleted bundle.
27     if ($displays = $this->loadDisplays('entity_form_display')) {
28       $storage = $this->entityManager()->getStorage('entity_form_display');
29       $storage->delete($displays);
30     }
31   }
32
33   /**
34    * {@inheritdoc}
35    */
36   public function postSave(EntityStorageInterface $storage, $update = TRUE) {
37     parent::postSave($storage, $update);
38
39     $entity_manager = $this->entityManager();
40     $bundle_of = $this->getEntityType()->getBundleOf();
41     if (!$update) {
42       $entity_manager->onBundleCreate($this->id(), $bundle_of);
43     }
44     else {
45       // Invalidate the render cache of entities for which this entity
46       // is a bundle.
47       if ($entity_manager->hasHandler($bundle_of, 'view_builder')) {
48         $entity_manager->getViewBuilder($bundle_of)->resetCache();
49       }
50       // Entity bundle field definitions may depend on bundle settings.
51       $entity_manager->clearCachedFieldDefinitions();
52       $entity_manager->clearCachedBundles();
53     }
54   }
55
56   /**
57    * {@inheritdoc}
58    */
59   public static function postDelete(EntityStorageInterface $storage, array $entities) {
60     parent::postDelete($storage, $entities);
61
62     foreach ($entities as $entity) {
63       $entity->deleteDisplays();
64       \Drupal::entityManager()->onBundleDelete($entity->id(), $entity->getEntityType()->getBundleOf());
65     }
66   }
67
68   /**
69    * Acts on an entity before the presave hook is invoked.
70    *
71    * Used before the entity is saved and before invoking the presave hook.
72    *
73    * Ensure that config entities which are bundles of other entities cannot have
74    * their ID changed.
75    *
76    * @param \Drupal\Core\Entity\EntityStorageInterface $storage
77    *   The entity storage object.
78    *
79    * @throws \Drupal\Core\Config\ConfigNameException
80    *   Thrown when attempting to rename a bundle entity.
81    */
82   public function preSave(EntityStorageInterface $storage) {
83     parent::preSave($storage);
84
85     // Only handle renames, not creations.
86     if (!$this->isNew() && $this->getOriginalId() !== $this->id()) {
87       $bundle_type = $this->getEntityType();
88       $bundle_of = $bundle_type->getBundleOf();
89       if (!empty($bundle_of)) {
90         throw new ConfigNameException("The machine name of the '{$bundle_type->getLabel()}' bundle cannot be changed.");
91       }
92     }
93   }
94
95   /**
96    * Returns view or form displays for this bundle.
97    *
98    * @param string $entity_type_id
99    *   The entity type ID of the display type to load.
100    *
101    * @return \Drupal\Core\Entity\Display\EntityDisplayInterface[]
102    *   A list of matching displays.
103    */
104   protected function loadDisplays($entity_type_id) {
105     $ids = \Drupal::entityQuery($entity_type_id)
106       ->condition('id', $this->getEntityType()->getBundleOf() . '.' . $this->getOriginalId() . '.', 'STARTS_WITH')
107       ->execute();
108     if ($ids) {
109       $storage = $this->entityManager()->getStorage($entity_type_id);
110       return $storage->loadMultiple($ids);
111     }
112     return [];
113   }
114
115 }