-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommerce_variation_bundle.module
111 lines (100 loc) · 3.25 KB
/
commerce_variation_bundle.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
<?php
/**
* @file
* Main module file.
*/
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;
use Drupal\commerce_product\Entity\ProductVariationType;
use Drupal\commerce_variation_bundle\Entity\BundleItemType;
use Drupal\commerce_variation_bundle\Entity\VariationBundle;
/**
* Implements hook_theme().
*/
function commerce_variation_bundle_theme() {
return [
'commerce_bundle_item' => [
'render element' => 'elements',
],
];
}
/**
* Prepares variables for bundle item templates.
*
* Default template: commerce-bundle-item.html.twig.
*
* @param array $variables
* An associative array containing:
* - elements: An associative array containing rendered fields.
* - attributes: HTML attributes for the containing element.
*/
function template_preprocess_commerce_bundle_item(array &$variables) {
/** @var \Drupal\commerce_variation_bundle\Entity\BundleItemInterface $bundle_item */
$bundle_item = $variables['elements']['#commerce_bundle_item'];
$variables['view_mode'] = $variables['elements']['#view_mode'];
$variables['bundle_item'] = $bundle_item;
// Helpful $content variable for templates.
$variables['content'] = [];
foreach (Element::children($variables['elements']) as $key) {
$variables['content'][$key] = $variables['elements'][$key];
}
}
/**
* Implements hook_entity_bundle_info_alter().
*/
function commerce_variation_bundle_entity_bundle_info_alter(array &$bundles): void {
$enabled_types = commerce_variation_bundle_enabled_types();
if (!empty($bundles['commerce_product_variation'])) {
foreach ($bundles['commerce_product_variation'] as $type => $bundle) {
if (in_array($type, $enabled_types)) {
$bundles['commerce_product_variation'][$type]['class'] = VariationBundle::class;
}
}
}
}
/**
* Get list of product variation types with bundle option enabled.
*
* @return array
* List of product variation types.
*/
function commerce_variation_bundle_enabled_types(): array {
$types = [];
$product_variation_types = ProductVariationType::loadMultiple();
foreach ($product_variation_types as $product_variation_type) {
if ($product_variation_type->hasTrait('purchasable_entity_variation_bundle')) {
$types[] = $product_variation_type->id();
}
}
return $types;
}
/**
* Implements hook_inline_entity_form_entity_form_alter().
*/
function commerce_variation_bundle_inline_entity_form_entity_form_alter(array &$entity_form, FormStateInterface &$form_state) {
if ($entity_form['#entity_type'] == 'commerce_bundle_item') {
$entity_bundle = $entity_form['#bundle'];
if ($bundle_item_type = BundleItemType::load($entity_bundle)) {
if ($bundle_item_type->shouldGenerateTitle()) {
$entity_form['title']['#access'] = FALSE;
}
}
}
}
/**
* Implements hook_inline_entity_form_table_fields_alter().
*/
function commerce_variation_bundle_inline_entity_form_table_fields_alter(array &$fields, array $context) {
if ($context['entity_type'] == 'commerce_bundle_item' && $context['field_name'] == 'bundle_items') {
$fields['quantity'] = [
'type' => 'field',
'label' => t('Quantity'),
'weight' => 10,
];
$fields['price'] = [
'type' => 'field',
'label' => t('Price'),
'weight' => 10,
];
}
}