This repository was archived by the owner on Apr 1, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcommerce_recurring.module
More file actions
211 lines (193 loc) · 6.46 KB
/
Copy pathcommerce_recurring.module
File metadata and controls
211 lines (193 loc) · 6.46 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
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
<?php
/**
* @file
* Defines the recurring entity and associated features.
*/
use Drupal\commerce_recurring\Entity\RecurringTypeInterface;
use Drupal\commerce_recurring\Event\RecurringCronEvent;
use Drupal\Core\Render\Element;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
/**
* Implements hook_cron().
*/
function commerce_recurring_cron() {
$event = new RecurringCronEvent();
$event_dispatcher = \Drupal::service('event_dispatcher');
$event_dispatcher->dispatch(RecurringCronEvent::EVENT_NAME, $event);
}
/**
* Implements hook_theme().
*/
function commerce_recurring_theme($existing, $type, $theme, $path) {
return [
'commerce_recurring' => [
'render element' => 'elements',
],
'commerce_recurring__user' => [
'base hook' => 'commerce_recurring',
'render element' => 'elements',
],
'commerce_recurring_edit_form' => [
'render element' => 'form',
],
];
}
/**
* Prepares variables for recurring templates.
*
* Default template: commerce-recurring.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_recurring(&$variables) {
/** @var Drupal\commerce_recurring\Entity\RecurringInterface $recurring */
$recurring = $variables['elements']['#commerce_recurring'];
$variables['recurring_entity'] = $recurring;
$variables['recurring'] = [];
foreach (Element::children($variables['elements']) as $key) {
$variables['recurring'][$key] = $variables['elements'][$key];
}
}
/**
* Implements hook_theme_suggestions_commerce_recurring().
*/
function commerce_recurring_theme_suggestions_commerce_recurring(array $variables) {
return _commerce_entity_theme_suggestions('commerce_recurring', $variables);
}
/**
* Adds the default order_items field to a recurring type.
*
* order items can't be a base field because the Views integration is broken.
* Instead, it is created as a configurable field for each recurring type.
*
* @param \Drupal\commerce_recurring\Entity\RecurringTypeInterface $recurring_type
* The recurring type.
*/
function commerce_recurring_add_order_items_field(RecurringTypeInterface $recurring_type) {
$field_storage = FieldStorageConfig::loadByName('commerce_recurring', 'order_items');
$field = FieldConfig::loadByName('commerce_recurring', $recurring_type->id(), 'order_items');
if (empty($field_storage)) {
$field_storage = FieldStorageConfig::create([
'field_name' => 'order_items',
'entity_type' => 'commerce_recurring',
'type' => 'entity_reference',
'cardinality' => FieldStorageConfig::CARDINALITY_UNLIMITED,
'settings' => [
'target_type' => 'commerce_order_item',
],
'locked' => TRUE,
'translatable' => FALSE,
]);
$field_storage->save();
}
if (empty($field)) {
$field = FieldConfig::create([
'field_storage' => $field_storage,
'bundle' => $recurring_type->id(),
'label' => 'order items',
'required' => TRUE,
'settings' => [
'handler' => 'default',
'handler_settings' => [],
],
'translatable' => FALSE,
]);
$field->save();
$form_display = commerce_get_entity_display('commerce_recurring', $recurring_type->id(), 'form');
$form_display->setComponent('order_items', [
'type' => 'inline_entity_form_complex',
'weight' => 5,
'settings' => [
'override_labels' => TRUE,
'label_singular' => 'order item',
'label_plural' => 'order items',
'allow_new' => TRUE,
'match_operator' => 'CONTAINS',
'allow_existing' => FALSE,
'form_mode' => 'default',
],
]);
$form_display->save();
$view_display = commerce_get_entity_display('commerce_recurring', $recurring_type->id(), 'view');
$view_display->setComponent('order_items', [
'type' => 'commerce_recurring_order_item_table',
'weight' => 9,
'label' => 'above',
]);
$view_display->save();
}
}
/**
* Adds the default recurring_orders field to a recurring type.
*
* Recurrings can't be a base field because the Views integration is
* broken. Instead, it is created as a configurable field for each recurring
* type.
*
* @param \Drupal\commerce_recurring\Entity\RecurringTypeInterface $recurring_type
* The recurring type.
*/
function commerce_recurring_add_recurring_orders_field(RecurringTypeInterface $recurring_type) {
$field_storage = FieldStorageConfig::loadByName('commerce_recurring', 'recurring_orders');
$field = FieldConfig::loadByName('commerce_recurring', $recurring_type->id(), 'recurring_orders');
if (empty($field_storage)) {
$field_storage = FieldStorageConfig::create([
'field_name' => 'recurring_orders',
'entity_type' => 'commerce_recurring',
'type' => 'entity_reference',
'cardinality' => FieldStorageConfig::CARDINALITY_UNLIMITED,
'settings' => [
'target_type' => 'commerce_order',
],
'locked' => TRUE,
'translatable' => FALSE,
]);
$field_storage->save();
}
if (empty($field)) {
$field = FieldConfig::create([
'field_storage' => $field_storage,
'bundle' => $recurring_type->id(),
'label' => 'Recurrings',
'required' => FALSE,
'settings' => [
'handler' => 'default',
'handler_settings' => [],
],
'translatable' => FALSE,
]);
$field->save();
$form_display = commerce_get_entity_display('commerce_recurring', $recurring_type->id(), 'form');
$form_display->setComponent('recurring_orders', [
'type' => 'inline_entity_form_complex',
'weight' => 6,
'settings' => [
'override_labels' => TRUE,
'label_singular' => 'recurring',
'label_plural' => 'recurrings',
'allow_new' => FALSE,
'match_operator' => 'CONTAINS',
'allow_existing' => TRUE,
'form_mode' => 'default',
],
]);
$form_display->save();
$view_display = commerce_get_entity_display('commerce_recurring', $recurring_type->id(), 'view');
$view_display->setComponent('recurring_orders', [
'type' => 'commerce_recurring_order_table',
'weight' => 10,
'label' => 'above',
]);
$view_display->save();
}
}
/**
* Implements hook_views_data_alter().
*/
function commerce_recurring_views_data_alter(array &$data) {
$data['commerce_recurring']['store_id']['field']['id'] = 'commerce_store';
}