-
Notifications
You must be signed in to change notification settings - Fork 252
Expand file tree
/
Copy pathProductFieldEquals.php
More file actions
186 lines (161 loc) · 6.06 KB
/
ProductFieldEquals.php
File metadata and controls
186 lines (161 loc) · 6.06 KB
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
<?php
namespace Drupal\commerce_product\Plugin\Commerce\PromotionCondition;
use Drupal\commerce_order\Entity\OrderItemInterface;
use Drupal\commerce_promotion\Plugin\Commerce\PromotionCondition\PromotionConditionBase;
use Drupal\Component\Utility\Html;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Form\FormStateInterface;
use Drupal\taxonomy\TermInterface;
use Drupal\taxonomy\TermStorage;
/**
* Provides an 'Order: Total amount comparison' condition.
*
* @CommercePromotionCondition(
* id = "commerce_promotion_product_field_equals",
* label = @Translation("Product field equals"),
* target_entity_type = "commerce_order_item",
* )
*/
class ProductFieldEquals extends PromotionConditionBase {
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return [
'bundle' => NULL,
'field' => NULL,
] + parent::defaultConfiguration();
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form += parent::buildConfigurationForm($form, $form_state);
$ajax_wrapper_id = Html::getUniqueId('ajax-wrapper');
// Prefix and suffix used for Ajax replacement.
$form['#prefix'] = '<div id="' . $ajax_wrapper_id . '">';
$form['#suffix'] = '</div>';
$selected_bundle = isset($this->configuration['bundle']) ? $this->configuration['bundle'] : NULL;
$bundles = \Drupal::service("entity_type.bundle.info")->getBundleInfo('commerce_product');
$bundle_options = [];
foreach ($bundles as $bundle => $label) {
$bundle_options[$bundle] = $label['label'];
}
$form['bundle'] = [
'#type' => 'select',
'#options' => $bundle_options,
'#title' => t('Product bundle'),
'#default_value' => $selected_bundle,
'#required' => TRUE,
'#ajax' => [
'callback' => [$this, 'bundleAjaxCallback'],
'wrapper' => $ajax_wrapper_id,
],
];
if (!$selected_bundle) {
return $form;
}
$fields = \Drupal::service("entity_field.manager")->getFieldDefinitions('commerce_product', $selected_bundle);
$selected_field = isset($this->configuration['field']) ? $this->configuration['field'] : NULL;
$filed_options = [];
foreach ($fields as $field_id => $field_definition) {
$filed_options[$field_id] = $field_definition->getLabel();
}
$form['field'] = [
'#type' => 'select',
'#title' => t('Field'),
'#options' => $filed_options,
'#default_value' => $selected_field,
'#required' => TRUE,
'#ajax' => [
'callback' => [$this, 'bundleAjaxCallback'],
'wrapper' => $ajax_wrapper_id,
],
];
if (!$selected_field) {
return $form;
}
//Create an empty representative entity
$commerce_product = \Drupal::service('entity_type.manager')->getStorage('commerce_product')->create(array(
'type' => $selected_bundle,
$selected_field => $this->configuration[$selected_field],
)
);
//Get the EntityFormDisplay (i.e. the default Form Display) of this content type
$entity_form_display = \Drupal::service('entity_type.manager')->getStorage('entity_form_display')
->load('commerce_product.' . $selected_bundle . '.default');
//Get the body field widget and add it to the form
if ($widget = $entity_form_display->getRenderer($selected_field)) { //Returns the widget class
$items = $commerce_product->get($selected_field); //Returns the FieldItemsList interface
$items->filterEmptyItems();
$form[$selected_field] = $widget->form($items, $form, $form_state); //Builds the widget form and attach it to your form
$form[$selected_field]['widget']['#required'] = TRUE;
}
return $form;
}
public function bundleAjaxCallback(array $form, FormStateInterface $form_state) {
$triggering_element = $form_state->getTriggeringElement();
$parents = array_slice($triggering_element['#array_parents'], 0, -1);
$form_element = NestedArray::getValue($form, $parents);
return $form_element;
}
/**
* {@inheritdoc}
*/
public function evaluate() {
$bundle_id = $this->configuration['bundle'];
if (empty($bundle_id)) {
return FALSE;
}
$field_id = $this->configuration['field'];
if (empty($field_id)) {
return FALSE;
}
/** @var OrderItemInterface $order_item */
$order_item = $this->getContextValue('commerce_order_item');
/** @var \Drupal\commerce_product\Entity\ProductInterface $current_product */
$current_product = $order_item->getPurchasedEntity()->getProduct();
if ($current_product->bundle() != $bundle_id) {
return FALSE;
}
if (!$current_product->hasField($field_id)) {
return FALSE;
}
$field_type = $current_product->get($field_id)->getFieldDefinition()->getType();
$target_type = NULL;
if ($field_type == 'entity_reference') {
$target_type = $current_product->get($field_id)->getFieldDefinition()->getFieldStorageDefinition()->getSetting('target_type');
}
if ($target_type == 'taxonomy_term') {
if ($current_product->get($field_id)->getValue() == $this->configuration[$field_id]) {
return TRUE;
}
else {
/** @var TermInterface $term */
$term = \Drupal::service('entity_type.manager')
->getStorage("taxonomy_term")->load($this->configuration[$field_id][0]['target_id']);
$tree = \Drupal::service('entity_type.manager')
->getStorage("taxonomy_term")
->loadTree($term->getVocabularyId(), $term->id());
$found = FALSE;
foreach ($tree as $item) {
if ($item->tid == $current_product->get($field_id)->getValue()[0]['target_id']) {
$found = TRUE;
break;
}
}
return $found;
}
}
elseif ($current_product->get($field_id)->getValue() != $this->configuration[$field_id]) {
return FALSE;
}
return TRUE;
}
/**
* {@inheritdoc}
*/
public function summary() {
return $this->t('Compares the product entity.');
}
}