Added Entity and Entity Reference Revisions which got dropped somewhere along the...
[yaffs-website] / web / modules / contrib / entity / entity.module
1 <?php
2
3 /**
4  * @file
5  * Provides expanded entity APIs.
6  */
7
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;
16
17 /**
18  * Gets the entity types which use bundle plugins.
19  *
20  * @return \Drupal\Core\Entity\EntityTypeInterface[]
21  *   The entity types.
22  */
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');
27   });
28
29   return $entity_types;
30 }
31
32 /**
33  * Implements hook_entity_type_build().
34  */
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);
39     }
40   }
41 }
42
43 /**
44  * Implements hook_entity_bundle_info().
45  */
46 function entity_entity_bundle_info() {
47   $bundles = [];
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();
52   }
53   return $bundles;
54 }
55
56 /**
57  * Implements hook_entity_field_storage_info().
58  */
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();
64   }
65 }
66
67 /**
68  * Implements hook_entity_bundle_field_info().
69  */
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);
75   }
76 }
77
78 /**
79  * Implements hook_modules_installed().
80  */
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);
84   }
85 }
86
87 /**
88  * Implements hook_module_preuninstall().
89  */
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]);
93   }
94 }
95
96 /**
97  * Implements hook_query_TAG_alter().
98  */
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);
104
105     \Drupal::service('class_resolver')
106       ->getInstanceFromDefinition(EntityQueryAlter::class)
107       ->alter($query, $entity_type);
108   }
109 }
110
111 /**
112  * Implements hook_views_query_alter().
113  */
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);
119   }
120 }