Same filename and directory in other branches
- 8.9.x core/lib/Drupal/Core/Config/Entity/ConfigEntityBundleBase.php
- 9 core/lib/Drupal/Core/Config/Entity/ConfigEntityBundleBase.php
Namespace
Drupal\Core\Config\Entity
File
core/lib/Drupal/Core/Config/Entity/ConfigEntityBundleBase.php
View source
<?php
namespace Drupal\Core\Config\Entity;
use Drupal\Core\Config\ConfigNameException;
use Drupal\Core\Entity\EntityStorageInterface;
abstract class ConfigEntityBundleBase extends ConfigEntityBase {
protected function deleteDisplays() {
if ($displays = $this
->loadDisplays('entity_view_display')) {
$storage = $this
->entityTypeManager()
->getStorage('entity_view_display');
$storage
->delete($displays);
}
if ($displays = $this
->loadDisplays('entity_form_display')) {
$storage = $this
->entityTypeManager()
->getStorage('entity_form_display');
$storage
->delete($displays);
}
}
public function postSave(EntityStorageInterface $storage, $update = TRUE) {
parent::postSave($storage, $update);
$entity_type_manager = $this
->entityTypeManager();
$bundle_of = $this
->getEntityType()
->getBundleOf();
if (!$update) {
\Drupal::service('entity_bundle.listener')
->onBundleCreate($this
->id(), $bundle_of);
}
else {
if ($entity_type_manager
->hasHandler($bundle_of, 'view_builder')) {
$entity_type_manager
->getViewBuilder($bundle_of)
->resetCache();
}
\Drupal::service('entity_field.manager')
->clearCachedFieldDefinitions();
$this
->entityTypeBundleInfo()
->clearCachedBundles();
}
}
public static function postDelete(EntityStorageInterface $storage, array $entities) {
parent::postDelete($storage, $entities);
foreach ($entities as $entity) {
$entity
->deleteDisplays();
\Drupal::service('entity_bundle.listener')
->onBundleDelete($entity
->id(), $entity
->getEntityType()
->getBundleOf());
}
}
public function preSave(EntityStorageInterface $storage) {
parent::preSave($storage);
if (!$this
->isNew() && $this
->getOriginalId() !== $this
->id()) {
$bundle_type = $this
->getEntityType();
$bundle_of = $bundle_type
->getBundleOf();
if (!empty($bundle_of)) {
throw new ConfigNameException("The machine name of the '{$bundle_type->getLabel()}' bundle cannot be changed.");
}
}
}
protected function loadDisplays($entity_type_id) {
$storage = $this
->entityTypeManager()
->getStorage($entity_type_id);
$ids = $storage
->getQuery()
->condition('id', $this
->getEntityType()
->getBundleOf() . '.' . $this
->getOriginalId() . '.', 'STARTS_WITH')
->execute();
if ($ids) {
$storage = $this
->entityTypeManager()
->getStorage($entity_type_id);
return $storage
->loadMultiple($ids);
}
return [];
}
}
Classes