ConfigEntityBundleBase.php in Zircon Profile 8
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
->entityManager()
->getStorage('entity_view_display');
$storage
->delete($displays);
}
if ($displays = $this
->loadDisplays('entity_form_display')) {
$storage = $this
->entityManager()
->getStorage('entity_form_display');
$storage
->delete($displays);
}
}
public function postSave(EntityStorageInterface $storage, $update = TRUE) {
parent::postSave($storage, $update);
$entity_manager = $this
->entityManager();
$bundle_of = $this
->getEntityType()
->getBundleOf();
if (!$update) {
$entity_manager
->onBundleCreate($this
->id(), $bundle_of);
}
else {
if ($entity_manager
->hasHandler($bundle_of, 'view_builder')) {
$entity_manager
->getViewBuilder($bundle_of)
->resetCache();
}
$entity_manager
->clearCachedFieldDefinitions();
}
}
public static function postDelete(EntityStorageInterface $storage, array $entities) {
parent::postDelete($storage, $entities);
foreach ($entities as $entity) {
$entity
->deleteDisplays();
\Drupal::entityManager()
->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) {
$ids = \Drupal::entityQuery($entity_type_id)
->condition('id', $this
->getEntityType()
->getBundleOf() . '.' . $this
->getOriginalId() . '.', 'STARTS_WITH')
->execute();
if ($ids) {
$storage = $this
->entityManager()
->getStorage($entity_type_id);
return $storage
->loadMultiple($ids);
}
return array();
}
}