entity.module 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. /**
  3. * @file
  4. * Provides expanded entity APIs.
  5. */
  6. use Drupal\Core\Entity\EntityTypeInterface;
  7. use Drupal\entity\BundlePlugin\BundlePluginHandler;
  8. /**
  9. * Gets the entity types which use bundle plugins.
  10. *
  11. * @return \Drupal\Core\Entity\EntityTypeInterface[]
  12. * The entity types.
  13. */
  14. function entity_get_bundle_plugin_entity_types() {
  15. $entity_types = \Drupal::entityTypeManager()->getDefinitions();
  16. $entity_types = array_filter($entity_types, function (EntityTypeInterface $entity_type) {
  17. return $entity_type->hasHandlerClass('bundle_plugin');
  18. });
  19. return $entity_types;
  20. }
  21. /**
  22. * Implements hook_entity_type_build().
  23. */
  24. function entity_entity_type_build(array &$entity_types) {
  25. foreach ($entity_types as $entity_type) {
  26. if ($entity_type->get('bundle_plugin_type')) {
  27. $entity_type->setHandlerClass('bundle_plugin', BundlePluginHandler::class);
  28. }
  29. }
  30. }
  31. /**
  32. * Implements hook_entity_bundle_info().
  33. */
  34. function entity_entity_bundle_info() {
  35. $bundles = [];
  36. foreach (entity_get_bundle_plugin_entity_types() as $entity_type) {
  37. /** @var \Drupal\entity\BundlePlugin\BundlePluginHandler $bundle_handler */
  38. $bundle_handler = \Drupal::entityTypeManager()->getHandler($entity_type->id(), 'bundle_plugin');
  39. $bundles[$entity_type->id()] = $bundle_handler->getBundleInfo();
  40. }
  41. return $bundles;
  42. }
  43. /**
  44. * Implements hook_entity_field_storage_info().
  45. */
  46. function entity_entity_field_storage_info(EntityTypeInterface $entity_type) {
  47. if ($entity_type->hasHandlerClass('bundle_plugin')) {
  48. /** @var \Drupal\entity\BundlePlugin\BundlePluginHandler $bundle_handler */
  49. $bundle_handler = \Drupal::entityTypeManager()->getHandler($entity_type->id(), 'bundle_plugin');
  50. return $bundle_handler->getFieldStorageDefinitions();
  51. }
  52. }
  53. /**
  54. * Implements hook_entity_bundle_field_info().
  55. */
  56. function entity_entity_bundle_field_info(EntityTypeInterface $entity_type, $bundle) {
  57. if ($entity_type->hasHandlerClass('bundle_plugin')) {
  58. /** @var \Drupal\entity\BundlePlugin\BundlePluginHandler $bundle_handler */
  59. $bundle_handler = \Drupal::entityTypeManager()->getHandler($entity_type->id(), 'bundle_plugin');
  60. return $bundle_handler->getFieldDefinitions($bundle);
  61. }
  62. }
  63. /**
  64. * Implements hook_modules_installed().
  65. */
  66. function entity_modules_installed($modules) {
  67. foreach (entity_get_bundle_plugin_entity_types() as $entity_type) {
  68. \Drupal::service('entity.bundle_plugin_installer')->installBundles($entity_type, $modules);
  69. }
  70. }
  71. /**
  72. * Implements hook_module_preuninstall().
  73. */
  74. function entity_module_preuninstall($module) {
  75. foreach (entity_get_bundle_plugin_entity_types() as $entity_type) {
  76. \Drupal::service('entity.bundle_plugin_installer')->uninstallBundles($entity_type, [$module]);
  77. }
  78. }