5 * Provides expanded entity APIs.
8 use Drupal\Core\Entity\EntityTypeInterface;
9 use Drupal\entity\BundlePlugin\BundlePluginHandler;
12 * Gets the entity types which use bundle plugins.
14 * @return \Drupal\Core\Entity\EntityTypeInterface[]
17 function entity_get_bundle_plugin_entity_types() {
18 $entity_types = \Drupal::entityTypeManager()->getDefinitions();
19 $entity_types = array_filter($entity_types, function (EntityTypeInterface $entity_type) {
20 return $entity_type->hasHandlerClass('bundle_plugin');
27 * Implements hook_entity_type_build().
29 function entity_entity_type_build(array &$entity_types) {
30 foreach ($entity_types as $entity_type) {
31 if ($entity_type->get('bundle_plugin_type')) {
32 $entity_type->setHandlerClass('bundle_plugin', BundlePluginHandler::class);
38 * Implements hook_entity_bundle_info().
40 function entity_entity_bundle_info() {
42 foreach (entity_get_bundle_plugin_entity_types() as $entity_type) {
43 /** @var \Drupal\entity\BundlePlugin\BundlePluginHandler $bundle_handler */
44 $bundle_handler = \Drupal::entityTypeManager()->getHandler($entity_type->id(), 'bundle_plugin');
45 $bundles[$entity_type->id()] = $bundle_handler->getBundleInfo();
51 * Implements hook_entity_field_storage_info().
53 function entity_entity_field_storage_info(EntityTypeInterface $entity_type) {
54 if ($entity_type->hasHandlerClass('bundle_plugin')) {
55 /** @var \Drupal\entity\BundlePlugin\BundlePluginHandler $bundle_handler */
56 $bundle_handler = \Drupal::entityTypeManager()->getHandler($entity_type->id(), 'bundle_plugin');
57 return $bundle_handler->getFieldStorageDefinitions();
62 * Implements hook_entity_bundle_field_info().
64 function entity_entity_bundle_field_info(EntityTypeInterface $entity_type, $bundle) {
65 if ($entity_type->hasHandlerClass('bundle_plugin')) {
66 /** @var \Drupal\entity\BundlePlugin\BundlePluginHandler $bundle_handler */
67 $bundle_handler = \Drupal::entityTypeManager()->getHandler($entity_type->id(), 'bundle_plugin');
68 return $bundle_handler->getFieldDefinitions($bundle);
73 * Implements hook_modules_installed().
75 function entity_modules_installed($modules) {
76 foreach (entity_get_bundle_plugin_entity_types() as $entity_type) {
77 \Drupal::service('entity.bundle_plugin_installer')->installBundles($entity_type, $modules);
82 * Implements hook_module_preuninstall().
84 function entity_module_preuninstall($module) {
85 foreach (entity_get_bundle_plugin_entity_types() as $entity_type) {
86 \Drupal::service('entity.bundle_plugin_installer')->uninstallBundles($entity_type, [$module]);