entity.module 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. /**
  3. * @file
  4. * Provides expanded entity APIs.
  5. */
  6. use Drupal\Core\Database\Query\SelectInterface;
  7. use Drupal\Core\Entity\EntityTypeInterface;
  8. use Drupal\entity\BundlePlugin\BundlePluginHandler;
  9. use Drupal\entity\QueryAccess\EntityQueryAlter;
  10. use Drupal\entity\QueryAccess\ViewsQueryAlter;
  11. use Drupal\views\Plugin\views\query\QueryPluginBase;
  12. use Drupal\views\Plugin\views\query\Sql;
  13. use Drupal\views\ViewExecutable;
  14. /**
  15. * Gets the entity types which use bundle plugins.
  16. *
  17. * @return \Drupal\Core\Entity\EntityTypeInterface[]
  18. * The entity types.
  19. */
  20. function entity_get_bundle_plugin_entity_types() {
  21. $entity_types = \Drupal::entityTypeManager()->getDefinitions();
  22. $entity_types = array_filter($entity_types, function (EntityTypeInterface $entity_type) {
  23. return $entity_type->hasHandlerClass('bundle_plugin');
  24. });
  25. return $entity_types;
  26. }
  27. /**
  28. * Implements hook_entity_type_build().
  29. */
  30. function entity_entity_type_build(array &$entity_types) {
  31. foreach ($entity_types as $entity_type) {
  32. if ($entity_type->get('bundle_plugin_type')) {
  33. $entity_type->setHandlerClass('bundle_plugin', BundlePluginHandler::class);
  34. }
  35. }
  36. }
  37. /**
  38. * Implements hook_entity_bundle_info().
  39. */
  40. function entity_entity_bundle_info() {
  41. $bundles = [];
  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();
  46. }
  47. return $bundles;
  48. }
  49. /**
  50. * Implements hook_entity_field_storage_info().
  51. */
  52. function entity_entity_field_storage_info(EntityTypeInterface $entity_type) {
  53. if ($entity_type->hasHandlerClass('bundle_plugin')) {
  54. /** @var \Drupal\entity\BundlePlugin\BundlePluginHandler $bundle_handler */
  55. $bundle_handler = \Drupal::entityTypeManager()->getHandler($entity_type->id(), 'bundle_plugin');
  56. return $bundle_handler->getFieldStorageDefinitions();
  57. }
  58. }
  59. /**
  60. * Implements hook_entity_bundle_field_info().
  61. */
  62. function entity_entity_bundle_field_info(EntityTypeInterface $entity_type, $bundle) {
  63. if ($entity_type->hasHandlerClass('bundle_plugin')) {
  64. /** @var \Drupal\entity\BundlePlugin\BundlePluginHandler $bundle_handler */
  65. $bundle_handler = \Drupal::entityTypeManager()->getHandler($entity_type->id(), 'bundle_plugin');
  66. return $bundle_handler->getFieldDefinitions($bundle);
  67. }
  68. }
  69. /**
  70. * Implements hook_modules_installed().
  71. */
  72. function entity_modules_installed($modules) {
  73. foreach (entity_get_bundle_plugin_entity_types() as $entity_type) {
  74. \Drupal::service('entity.bundle_plugin_installer')->installBundles($entity_type, $modules);
  75. }
  76. }
  77. /**
  78. * Implements hook_module_preuninstall().
  79. */
  80. function entity_module_preuninstall($module) {
  81. foreach (entity_get_bundle_plugin_entity_types() as $entity_type) {
  82. \Drupal::service('entity.bundle_plugin_installer')->uninstallBundles($entity_type, [$module]);
  83. }
  84. }
  85. /**
  86. * Implements hook_query_TAG_alter().
  87. */
  88. function entity_query_entity_query_alter(SelectInterface $query) {
  89. $entity_type_id = $query->getMetaData('entity_type');
  90. if ($query->hasTag($entity_type_id . '_access')) {
  91. $entity_type_manager = \Drupal::entityTypeManager();
  92. $entity_type = $entity_type_manager->getDefinition($entity_type_id);
  93. \Drupal::service('class_resolver')
  94. ->getInstanceFromDefinition(EntityQueryAlter::class)
  95. ->alter($query, $entity_type);
  96. }
  97. }
  98. /**
  99. * Implements hook_views_query_alter().
  100. */
  101. function entity_views_query_alter(ViewExecutable $view, QueryPluginBase $query) {
  102. if ($query instanceof Sql) {
  103. \Drupal::service('class_resolver')
  104. ->getInstanceFromDefinition(ViewsQueryAlter::class)
  105. ->alter($query, $view);
  106. }
  107. }