ConfigEntityBundleBase.php 3.7 KB

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