BundlePluginInstaller.php in Entity API 8
File
src/BundlePlugin/BundlePluginInstaller.php
View source
<?php
namespace Drupal\entity\BundlePlugin;
use Drupal\Core\Entity\EntityBundleListenerInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Field\FieldDefinitionListenerInterface;
use Drupal\Core\Field\FieldStorageDefinitionListenerInterface;
class BundlePluginInstaller implements BundlePluginInstallerInterface {
protected $entityTypeManager;
protected $entityBundleListener;
protected $fieldStorageDefinitionListener;
protected $fieldDefinitionListener;
public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityBundleListenerInterface $entity_bundle_listener, FieldStorageDefinitionListenerInterface $field_storage_definition_listener, FieldDefinitionListenerInterface $field_definition_listener) {
$this->entityTypeManager = $entity_type_manager;
$this->entityBundleListener = $entity_bundle_listener;
$this->fieldStorageDefinitionListener = $field_storage_definition_listener;
$this->fieldDefinitionListener = $field_definition_listener;
}
public function installBundles(EntityTypeInterface $entity_type, array $modules) {
$bundle_handler = $this->entityTypeManager
->getHandler($entity_type
->id(), 'bundle_plugin');
$bundles = array_filter($bundle_handler
->getBundleInfo(), function ($bundle_info) use ($modules) {
return in_array($bundle_info['provider'], $modules, TRUE);
});
foreach (array_keys($bundles) as $bundle) {
$this->entityBundleListener
->onBundleCreate($bundle, $entity_type
->id());
foreach ($bundle_handler
->getFieldDefinitions($bundle) as $definition) {
$this->fieldStorageDefinitionListener
->onFieldStorageDefinitionCreate($definition);
$this->fieldDefinitionListener
->onFieldDefinitionCreate($definition);
}
}
}
public function uninstallBundles(EntityTypeInterface $entity_type, array $modules) {
$bundle_handler = $this->entityTypeManager
->getHandler($entity_type
->id(), 'bundle_plugin');
$bundles = array_filter($bundle_handler
->getBundleInfo(), function ($bundle_info) use ($modules) {
return in_array($bundle_info['provider'], $modules, TRUE);
});
$field_storage_definitions = [];
foreach (array_keys($bundles) as $bundle) {
$this->entityBundleListener
->onBundleDelete($bundle, $entity_type
->id());
foreach ($bundle_handler
->getFieldDefinitions($bundle) as $definition) {
$this->fieldDefinitionListener
->onFieldDefinitionDelete($definition);
$field_storage_definitions[$definition
->getName()] = $definition;
}
}
foreach ($field_storage_definitions as $definition) {
$this->fieldStorageDefinitionListener
->onFieldStorageDefinitionDelete($definition);
}
}
}