forked from drupalcommerce/commerce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
commerce.module
247 lines (225 loc) · 7.98 KB
/
commerce.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
<?php
/**
* @file
* Defines common functionality for all Commerce modules.
*/
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;
/**
* Implements hook_toolbar_alter().
*/
function commerce_toolbar_alter(&$items) {
$items['administration']['#attached']['library'][] = 'commerce/toolbar';
}
/**
* Implements hook_field_widget_info_alter().
*
* Exposes the commerce_plugin_item widgets for each of the field type's
* derivatives, since core does not do it automatically.
*/
function commerce_field_widget_info_alter(array &$info) {
foreach (['commerce_plugin_select', 'commerce_plugin_radios'] as $widget) {
if (isset($info[$widget])) {
$field_type_manager = \Drupal::service('plugin.manager.field.field_type');
foreach ($field_type_manager->getDefinitions() as $key => $definition) {
if ($definition['id'] == 'commerce_plugin_item') {
$info[$widget]['field_types'][] = $key;
}
}
}
}
}
/**
* Implements hook_field_widget_form_alter().
*
* Base fields have a description that's used for two very different purposes:
* - To describe the field in the Views UI and other parts of the system.
* - As user-facing help text shown on field widgets.
* The text is rarely suitable for both, and in most cases feels redundant
* as user-facing help text. Hence we remove it from that context, but only if
* the definition didn't specify otherwise via our display_description setting.
*/
function commerce_field_widget_form_alter(&$element, FormStateInterface $form_state, $context) {
$field_definition = $context['items']->getFieldDefinition();
if (!($field_definition instanceof BaseFieldDefinition)) {
// Not a base field.
return;
}
if (strpos($field_definition->getTargetEntityTypeId(), 'commerce_') !== 0) {
// Not a Commerce entity type.
return;
}
if ($field_definition->getSetting('display_description')) {
// The definition requested that the description stays untouched.
return;
}
$element['#description'] = '';
// Many widgets are nested one level deeper.
$children = Element::getVisibleChildren($element);
if (count($children) == 1) {
$child = reset($children);
$element[$child]['#description'] = '';
}
}
/**
* Gets the entity display for the given entity type and bundle.
*
* The entity display will be created if missing.
*
* @param string $entity_type
* The entity type.
* @param string $bundle
* The bundle.
* @param string $display_context
* The display context ('view' or 'form').
*
* @throws \InvalidArgumentException
* Thrown when an invalid display context is provided.
*
* @return \Drupal\Core\Entity\Display\EntityDisplayInterface
* The entity display.
*/
function commerce_get_entity_display($entity_type, $bundle, $display_context) {
if (!in_array($display_context, ['view', 'form'])) {
throw new \InvalidArgumentException(sprintf('Invalid display_context %s passed to _commerce_product_get_display().', $display_context));
}
$storage = \Drupal::entityTypeManager()->getStorage('entity_' . $display_context . '_display');
$display = $storage->load($entity_type . '.' . $bundle . '.default');
if (!$display) {
$display = $storage->create([
'targetEntityType' => $entity_type,
'bundle' => $bundle,
'mode' => 'default',
'status' => TRUE,
]);
}
return $display;
}
/**
* Helper for providing entity theme suggestions.
*
* @param string $entity_type_id
* The entity type ID.
* @param array $variables
* An array of variables passed to the theme hook.
*
* @return array
* An array of theme suggestions.
*/
function _commerce_entity_theme_suggestions($entity_type_id, array $variables) {
$original = $variables['theme_hook_original'];
$entity = $variables['elements']['#' . $entity_type_id];
$sanitized_view_mode = strtr($variables['elements']['#view_mode'], '.', '_');
$suggestions = [];
$suggestions[] = $original;
$suggestions[] = $original . '__' . $sanitized_view_mode;
$suggestions[] = $original . '__' . $entity->bundle();
$suggestions[] = $original . '__' . $entity->bundle() . '__' . $sanitized_view_mode;
$suggestions[] = $original . '__' . $entity->id();
$suggestions[] = $original . '__' . $entity->id() . '__' . $sanitized_view_mode;
return $suggestions;
}
/**
* Gets the entity types which use bundle plugins.
*
* @return \Drupal\Core\Entity\EntityTypeInterface[]
* The entity types.
*/
function commerce_get_bundle_plugin_entity_types() {
$entity_types = \Drupal::entityTypeManager()->getDefinitions();
$entity_types = array_filter($entity_types, function (EntityTypeInterface $entity_type) {
return $entity_type->hasHandlerClass('bundle_plugin');
});
return $entity_types;
}
/**
* Implements hook_entity_type_build().
*/
function commerce_entity_type_build(array &$entity_types) {
foreach ($entity_types as $entity_type) {
if ($entity_type->get('bundle_plugin_type')) {
$entity_type->setHandlerClass('bundle_plugin', 'Drupal\commerce\BundlePluginHandler');
}
}
}
/**
* Implements hook_entity_bundle_info().
*/
function commerce_entity_bundle_info() {
$bundles = [];
foreach (commerce_get_bundle_plugin_entity_types() as $entity_type) {
/** @var \Drupal\commerce\BundlePluginHandler $bundle_handler */
$bundle_handler = \Drupal::entityTypeManager()->getHandler($entity_type->id(), 'bundle_plugin');
$bundles[$entity_type->id()] = $bundle_handler->getBundleInfo();
}
return $bundles;
}
/**
* Implements hook_entity_field_storage_info().
*/
function commerce_entity_field_storage_info(EntityTypeInterface $entity_type) {
if ($entity_type->hasHandlerClass('bundle_plugin')) {
/** @var \Drupal\commerce\BundlePluginHandler $bundle_handler */
$bundle_handler = \Drupal::entityTypeManager()->getHandler($entity_type->id(), 'bundle_plugin');
return $bundle_handler->getFieldStorageDefinitions();
}
}
/**
* Implements hook_entity_bundle_field_info().
*/
function commerce_entity_bundle_field_info(EntityTypeInterface $entity_type, $bundle) {
if ($entity_type->hasHandlerClass('bundle_plugin')) {
/** @var \Drupal\commerce\BundlePluginHandler $bundle_handler */
$bundle_handler = \Drupal::entityTypeManager()->getHandler($entity_type->id(), 'bundle_plugin');
return $bundle_handler->getFieldDefinitions($bundle);
}
}
/**
* Implements hook_modules_installed().
*/
function commerce_modules_installed($modules) {
foreach (commerce_get_bundle_plugin_entity_types() as $entity_type) {
\Drupal::service('commerce.bundle_plugin_installer')->installBundles($entity_type, $modules);
}
}
/**
* Implements hook_module_preuninstall().
*/
function commerce_module_preuninstall($module) {
foreach (commerce_get_bundle_plugin_entity_types() as $entity_type) {
\Drupal::service('commerce.bundle_plugin_installer')->uninstallBundles($entity_type, [$module]);
}
}
/**
* Implements hook_action_info_alter().
*
* @todo: Core uses CategorizingPluginManagerTrait but does not implement.
*/
function commerce_action_info_alter(array &$definitions) {
foreach ($definitions as &$definition) {
// Ensure that every plugin has a category.
if (empty($definition['category'])) {
// Default to the human readable module name if the provider is a module;
// otherwise the provider machine name is used.
$definition['category'] = \Drupal::moduleHandler()->getName($definition['provider']);
}
}
}
/**
* Implements hook_condition_info_alter().
*
* @todo: Core uses CategorizingPluginManagerTrait but does not implement.
*/
function commerce_condition_info_alter(array &$definitions) {
foreach ($definitions as &$definition) {
// Ensure that every plugin has a category.
if (empty($definition['category'])) {
// Default to the human readable module name if the provider is a module;
// otherwise the provider machine name is used.
$definition['category'] = \Drupal::moduleHandler()
->getName($definition['provider']);
}
}
}