5 * Provides expanded entity APIs.
8 use Drupal\Core\Database\Query\SelectInterface;
9 use Drupal\Core\Entity\EntityTypeInterface;
10 use Drupal\entity\BundlePlugin\BundlePluginHandler;
11 use Drupal\entity\QueryAccess\EntityQueryAlter;
12 use Drupal\entity\QueryAccess\ViewsQueryAlter;
13 use Drupal\views\Plugin\views\query\QueryPluginBase;
14 use Drupal\views\Plugin\views\query\Sql;
15 use Drupal\views\ViewExecutable;
18 * Gets the entity types which use bundle plugins.
20 * @return \Drupal\Core\Entity\EntityTypeInterface[]
23 function entity_get_bundle_plugin_entity_types() {
24 $entity_types = \Drupal::entityTypeManager()->getDefinitions();
25 $entity_types = array_filter($entity_types, function (EntityTypeInterface $entity_type) {
26 return $entity_type->hasHandlerClass('bundle_plugin');
33 * Implements hook_entity_type_build().
35 function entity_entity_type_build(array &$entity_types) {
36 foreach ($entity_types as $entity_type) {
37 if ($entity_type->get('bundle_plugin_type')) {
38 $entity_type->setHandlerClass('bundle_plugin', BundlePluginHandler::class);
44 * Implements hook_entity_bundle_info().
46 function entity_entity_bundle_info() {
48 foreach (entity_get_bundle_plugin_entity_types() as $entity_type) {
49 /** @var \Drupal\entity\BundlePlugin\BundlePluginHandler $bundle_handler */
50 $bundle_handler = \Drupal::entityTypeManager()->getHandler($entity_type->id(), 'bundle_plugin');
51 $bundles[$entity_type->id()] = $bundle_handler->getBundleInfo();
57 * Implements hook_entity_field_storage_info().
59 function entity_entity_field_storage_info(EntityTypeInterface $entity_type) {
60 if ($entity_type->hasHandlerClass('bundle_plugin')) {
61 /** @var \Drupal\entity\BundlePlugin\BundlePluginHandler $bundle_handler */
62 $bundle_handler = \Drupal::entityTypeManager()->getHandler($entity_type->id(), 'bundle_plugin');
63 return $bundle_handler->getFieldStorageDefinitions();
68 * Implements hook_entity_bundle_field_info().
70 function entity_entity_bundle_field_info(EntityTypeInterface $entity_type, $bundle) {
71 if ($entity_type->hasHandlerClass('bundle_plugin')) {
72 /** @var \Drupal\entity\BundlePlugin\BundlePluginHandler $bundle_handler */
73 $bundle_handler = \Drupal::entityTypeManager()->getHandler($entity_type->id(), 'bundle_plugin');
74 return $bundle_handler->getFieldDefinitions($bundle);
79 * Implements hook_modules_installed().
81 function entity_modules_installed($modules) {
82 foreach (entity_get_bundle_plugin_entity_types() as $entity_type) {
83 \Drupal::service('entity.bundle_plugin_installer')->installBundles($entity_type, $modules);
88 * Implements hook_module_preuninstall().
90 function entity_module_preuninstall($module) {
91 foreach (entity_get_bundle_plugin_entity_types() as $entity_type) {
92 \Drupal::service('entity.bundle_plugin_installer')->uninstallBundles($entity_type, [$module]);
97 * Implements hook_query_TAG_alter().
99 function entity_query_entity_query_alter(SelectInterface $query) {
100 $entity_type_id = $query->getMetaData('entity_type');
101 if ($query->hasTag($entity_type_id . '_access')) {
102 $entity_type_manager = \Drupal::entityTypeManager();
103 $entity_type = $entity_type_manager->getDefinition($entity_type_id);
105 \Drupal::service('class_resolver')
106 ->getInstanceFromDefinition(EntityQueryAlter::class)
107 ->alter($query, $entity_type);
112 * Implements hook_views_query_alter().
114 function entity_views_query_alter(ViewExecutable $view, QueryPluginBase $query) {
115 if ($query instanceof Sql) {
116 \Drupal::service('class_resolver')
117 ->getInstanceFromDefinition(ViewsQueryAlter::class)
118 ->alter($query, $view);