forked from creativepropulsionlabs/commerce_rma
-
Notifications
You must be signed in to change notification settings - Fork 0
/
commerce_rma.module
652 lines (596 loc) · 23.1 KB
/
commerce_rma.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
<?php
/**
* @file
* Contains commerce_rma.module.
*/
use Drupal\commerce_order\Entity\OrderInterface;
use Drupal\commerce_order\Entity\OrderTypeInterface;
use Drupal\commerce_rma\Entity\CommerceReturn;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url;
use Drupal\entity\BundleFieldDefinition;
use Drupal\commerce_rma\Entity\CommerceReturnTypeInterface;
use Drupal\workflows\WorkflowInterface;
/**
* Implements hook_help().
*/
function commerce_rma_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
// Main module help for the commerce_rma module.
case 'help.page.commerce_rma':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('Commerce return') . '</p>';
return $output;
default:
}
}
/**
* Implements hook_theme().
*/
function commerce_rma_theme() {
$theme = [];
$themes['commerce_return'] = [
'render element' => 'children',
];
$theme['commerce_return'] = [
'render element' => 'elements',
];
$theme['commerce_return_content_add_list'] = [
'render element' => 'content',
'variables' => ['content' => NULL],
];
$theme['commerce_return_item'] = [
'render element' => 'elements',
];
$theme['commerce_return_item_content_add_list'] = [
'render element' => 'content',
'variables' => ['content' => NULL],
];
return $theme;
}
/**
* Implements hook_theme_suggestions_HOOK().
*/
function commerce_rma_theme_suggestions_commerce_return_item(array $variables) {
$suggestions = [];
$entity = $variables['elements']['#commerce_return_item'];
$sanitized_view_mode = strtr($variables['elements']['#view_mode'], '.', '_');
$suggestions[] = 'commerce_return_item__' . $sanitized_view_mode;
$suggestions[] = 'commerce_return_item__' . $entity->bundle();
$suggestions[] = 'commerce_return_item__' . $entity->bundle() . '__' . $sanitized_view_mode;
$suggestions[] = 'commerce_return_item__' . $entity->id();
$suggestions[] = 'commerce_return_item__' . $entity->id() . '__' . $sanitized_view_mode;
return $suggestions;
}
/**
* Implements hook_theme_suggestions_HOOK().
*/
function commerce_rma_theme_suggestions_commerce_return(array $variables) {
$suggestions = [];
$entity = $variables['elements']['#commerce_return'];
$sanitized_view_mode = strtr($variables['elements']['#view_mode'], '.', '_');
$suggestions[] = 'commerce_return__' . $sanitized_view_mode;
$suggestions[] = 'commerce_return__' . $entity->bundle();
$suggestions[] = 'commerce_return__' . $entity->bundle() . '__' . $sanitized_view_mode;
$suggestions[] = 'commerce_return__' . $entity->id();
$suggestions[] = 'commerce_return__' . $entity->id() . '__' . $sanitized_view_mode;
return $suggestions;
}
/**
* Implements hook_entity_operation_alter().
*/
function commerce_rma_entity_operation_alter(array &$operations, EntityInterface $entity) {
if ($entity->getEntityTypeId() == 'commerce_return') {
// Place the edit operation before any others.
if (isset($operations['edit'])) {
$operations['edit']['weight'] = -100;
}
}
}
/**
* Implements hook_entity_operation().
*/
function commerce_rma_entity_operation(EntityInterface $entity) {
$operations = [];
// Only show the "Shipments" operation link for commerce_order entities.
if ($entity->getEntityTypeId() == 'commerce_order') {
if (!$entity->hasField('returns')) {
return;
}
$show_return_states = [
'completed',
'partial_returned',
'returned',
];
// Do not show for a not completed order.
if (!in_array($entity->getState()->value , $show_return_states)) {
return;
}
// Only show if the user has the "administer commerce_shipment" permission.
if (!\Drupal::currentUser()->hasPermission('administer commerce return')) {
return;
}
$operations['returns'] = [
'title' => t('Returns'),
'url' => Url::fromRoute('entity.commerce_return.collection', [
'commerce_order' => $entity->id(),
]),
'weight' => 60,
];
return $operations;
}
if ($entity->getEntityTypeId() == 'commerce_return') {
/** @var OrderTypeInterface $order_type */
$return_type = \Drupal::entityTypeManager()
->getStorage('commerce_return_type')
->load($entity->bundle());
$workflow_id = $return_type->getWorkflowId();
$workflow_manager = \Drupal::service('plugin.manager.workflow');
/** @var WorkflowInterface $workflow */
$workflow = $workflow_manager->createInstance($workflow_id);
if ($workflow) {
/** @var \Drupal\state_machine\Plugin\Workflow\WorkflowTransition[] $transitions */
$transitions = $workflow->getAllowedTransitions($entity->getState()->value, $entity);
$destination = \Drupal::destination()->get();
foreach ($transitions as $transition) {
if ($transition->getId() == 'cancel') {
$operations[$transition->getId()] = [
'title' => $transition->getLabel(),
'url' => Url::fromRoute('commerce_rma.confirm_transition', [
'commerce_return' => $entity->id(),
'workflow' => $workflow_id,
'workflow_transition' => $transition->getId()
], [
'query' => ['destination' => $destination],
]),
'weight' => -1,
];
continue;
}
$confirm_redirect_url = Url::fromRoute('commerce_rma.confirm_transition', [
'commerce_return' => $entity->id(),
'workflow' => $workflow_id,
'workflow_transition' => $transition->getId()
], [
'query' => ['destination' => $destination],
]);
$operations[$transition->getId()] = [
'title' => $transition->getLabel(),
'url' => Url::fromRoute('entity.commerce_return.edit_form', [
'commerce_return' => $entity->id(),
'commerce_order' => $entity->getOrderId(),
], [
'query' => [
'destination' => $confirm_redirect_url->toString(),
],
]),
'weight' => -1,
];
}
}
return $operations;
}
return $operations;
}
/**
* Implements hook_views_data_alter().
*/
function commerce_rma_views_data_alter(array &$data) {
$data['commerce_order_item']['edit_rma_quantity']['field'] = [
'title' => t('Quantity'),
'help' => t('Adds a text field for editing the RMA quantity.'),
'id' => 'commerce_rma_order_item_edit_quantity',
];
$data['commerce_order_item']['edit_rma_reason']['field'] = [
'title' => t('Reason'),
'help' => t('Adds a dropdown field for editing the RMA Reason.'),
'id' => 'commerce_rma_order_item_edit_reason',
];
$data['commerce_order_item']['edit_rma_expected_resolution']['field'] = [
'title' => t('Expected resolution'),
'help' => t('Adds a dropdown field for editing the RMA Expected resolution.'),
'id' => 'commerce_rma_order_item_edit_expected_resolution',
];
$data['commerce_order_item']['edit_rma_note']['field'] = [
'title' => t('Note'),
'help' => t('Adds a text field for editing the RMA note.'),
'id' => 'commerce_rma_order_item_edit_note',
];
$data['commerce_order_item']['edit_rma_files']['field'] = [
'title' => t('Attachments'),
'help' => t('Adds a file field for editing the RMA attachments.'),
'id' => 'commerce_rma_order_item_edit_files',
];
$data['commerce_order']['rma_allow_return_status']['field'] = [
'title' => t('Return Allow Status'),
'help' => t('Is return eligible or not.'),
'id' => 'commerce_rma_order_item_allow_return_status',
];
}
/**
* Adds the default order_item field to an order type.
*
* Order item can't be a base field because the Views integration is broken.
* Instead, it is created as a configurable field for each order type.
*
* @param \Drupal\commerce_rma\Entity\CommerceReturnTypeInterface $order_type
* The order type.
*/
function commerce_order_add_return_items_field(CommerceReturnTypeInterface $return_type) {
$field_definition = BundleFieldDefinition::create('entity_reference')
->setTargetEntityTypeId('commerce_return')
->setTargetBundle($return_type->id())
->setName('return_items')
->setLabel('Return items')
->setCardinality(BundleFieldDefinition::CARDINALITY_UNLIMITED)
->setRequired(TRUE)
->setSetting('target_type', 'commerce_return_item')
->setSetting('handler', 'default')
->setDisplayOptions('form', [
'type' => 'inline_entity_form_complex',
'weight' => 0,
'settings' => [
'override_labels' => TRUE,
'label_singular' => 'return item',
'label_plural' => 'return items',
],
])
->setDisplayOptions('view', [
'type' => 'commerce_order_item_table',
'weight' => 0,
]);
$configurable_field_manager = \Drupal::service('commerce.configurable_field_manager');
$configurable_field_manager->createField($field_definition);
}
/**
* Prepares variables for RMA templates.
*
* Default template: commerce-return.html.twig.
*
* @param array $variables
* An associative array containing:
* - elements: An associative array containing the user information and any
* - attributes: HTML attributes for the containing element.
*/
function template_preprocess_commerce_return(array &$variables) {
$commerce_return = $variables['elements']['#commerce_return'];
// Helpful $content variable for templates.
foreach (Element::children($variables['elements']) as $key) {
$variables['content'][$key] = $variables['elements'][$key];
}
}
/**
* Prepares variables for RMA item templates.
*
* Default template: commerce-return-item.html.twig.
*
* @param array $variables
* An associative array containing:
* - elements: An associative array containing the user information and any
* - attributes: HTML attributes for the containing element.
*/
function template_preprocess_commerce_return_item(array &$variables) {
// Fetch RMAItem Entity Object.
$commerce_return_item = $variables['elements']['#commerce_return_item'];
// Helpful $content variable for templates.
foreach (Element::children($variables['elements']) as $key) {
$variables['content'][$key] = $variables['elements'][$key];
}
}
/**
* Implements hook_form_FORM_ID_alter() for 'commerce_order_type_form'.
*/
function commerce_rma_form_commerce_order_type_form_alter(array &$form, FormStateInterface $form_state) {
/** @var \Drupal\commerce_order\Entity\OrderTypeInterface $order_type */
$order_type = $form_state->getFormObject()->getEntity();
$return_type_id = $order_type->getThirdPartySetting('commerce_rma', 'return_type');
$return_order_workflow_id = $order_type->getThirdPartySetting('commerce_rma', 'return_workflow');
$return_type_storage = \Drupal::entityTypeManager()->getStorage('commerce_return_type');
$return_types = $return_type_storage->loadMultiple();
$return_types = array_map(function ($return_type) {
return $return_type->label();
}, $return_types);
$return_type_ids = array_keys($return_types);
/** @var \Drupal\state_machine\WorkflowManagerInterface $workflow_manager */
$workflow_manager = \Drupal::service('plugin.manager.workflow');
$return_order_workflows = $workflow_manager->getDefinitions();
// $return_order_workflows = $workflow_manager->getGroupedLabels('commerce_order');
$return_order_workflows = array_map(function ($return_order_workflow) {
if ($return_order_workflow["group"] == 'commerce_order_return') {
return $return_order_workflow['label'];
}
}, $return_order_workflows);
$return_order_workflows = array_filter($return_order_workflows);
$form['commerce_rma'] = [
'#type' => 'container',
'#weight' => 4,
'#element_validate' => ['commerce_rma_order_type_form_validate'],
];
$form['commerce_rma']['enable_return'] = [
'#type' => 'checkbox',
'#title' => t('Enable Returns for this order type'),
'#default_value' => !empty($return_type_id),
];
$form['commerce_rma']['return_type'] = [
'#type' => 'select',
'#title' => t('Return type'),
'#options' => $return_types,
'#default_value' => $return_type_id ?: reset($return_type_ids),
'#required' => TRUE,
'#states' => [
'visible' => [
':input[name="commerce_rma[enable_return]"]' => ['checked' => TRUE],
],
],
];
$form['commerce_rma']['return_workflow'] = [
'#type' => 'select',
'#title' => t('Return Workflow applied to Order'),
'#options' => $return_order_workflows,
'#default_value' => $return_order_workflow_id ?: reset($return_order_workflows),
'#required' => TRUE,
'#states' => [
'visible' => [
':input[name="commerce_rma[enable_return]"]' => ['checked' => TRUE],
],
],
];
$max_order_age = $order_type->getThirdPartySetting('commerce_rma', 'return_max_order_age', 15);
$form['commerce_rma']['return_max_order_age'] = [
'#type' => 'number',
'#min' => 0,
'#title' => t('Max order age'),
'#description' => t('Max order age to allow request return for user(days). Use 0 for no limit.'),
'#default_value' => $max_order_age,
'#required' => TRUE,
'#states' => [
'visible' => [
':input[name="commerce_rma[enable_return]"]' => ['checked' => TRUE],
],
],
];
$form['actions']['submit']['#submit'][] = 'commerce_rma_order_type_form_submit';
}
/**
* Validation handler for commerce_rma_form_commerce_order_type_form_alter().
*/
function commerce_rma_order_type_form_validate(array $element, FormStateInterface $form_state) {
/** @var \Drupal\commerce_order\Entity\OrderTypeInterface $order_type */
$order_type = $form_state->getFormObject()->getEntity();
$previous_value = $order_type->getThirdPartySetting('commerce_rma', 'return_type');
$settings = $form_state->getValue(['commerce_rma']);
/** @var \Drupal\commerce\ConfigurableFieldManagerInterface $configurable_field_manager */
$configurable_field_manager = \Drupal::service('commerce.configurable_field_manager');
// Don't allow return to be disabled if there's data in the field.
if ($previous_value && !$settings['enable_return']) {
$field_definition = commerce_rma_build_return_field_definition($order_type->id());
if ($configurable_field_manager->hasData($field_definition)) {
$form_state->setError($element['enable_return'], t('Return cannot be disabled until all orders with return data are deleted.'));
}
}
}
/**
* Submission handler for commerce_rma_form_commerce_order_type_form_alter().
*/
function commerce_rma_order_type_form_submit(array $form, FormStateInterface $form_state) {
/** @var \Drupal\commerce_order\Entity\OrderTypeInterface $order_type */
$order_type = $form_state->getFormObject()->getEntity();
$previous_value = $order_type->getThirdPartySetting('commerce_rma', 'return_type');
$settings = $form_state->getValue(['commerce_rma']);
/** @var \Drupal\commerce\ConfigurableFieldManagerInterface $configurable_field_manager */
$configurable_field_manager = \Drupal::service('commerce.configurable_field_manager');
$field_definition = commerce_rma_build_return_field_definition($order_type->id());
if (!$previous_value && $settings['enable_return']) {
$all_bundle_fields = \Drupal::service('entity_field.manager')->getFieldDefinitions('commerce_order', $order_type->id());
if (!isset($all_bundle_fields['returns'])) {
$configurable_field_manager->createField($field_definition);
}
}
elseif ($previous_value && !$settings['enable_return']) {
$configurable_field_manager->deleteField($field_definition);
}
$state_field_definition = commerce_rma_build_order_state_field_definition($order_type->id());
if (!$previous_value && $settings['enable_return']) {
$all_bundle_fields = \Drupal::service('entity_field.manager')->getFieldDefinitions('commerce_order', $order_type->id());
if (!isset($all_bundle_fields['return_state'])) {
$configurable_field_manager->createField($state_field_definition);
}
}
elseif ($previous_value && !$settings['enable_return']) {
$configurable_field_manager->deleteField($state_field_definition);
}
$return_type_id = $settings['enable_return'] ? $settings['return_type'] : '';
$return_workflow_id = $settings['return_workflow'] ? $settings['return_workflow'] : '';
$return_max_order_age = is_numeric($settings['return_max_order_age']) ? $settings['return_max_order_age'] : 15;
$order_type->setThirdPartySetting('commerce_rma', 'return_type', $return_type_id);
$order_type->setThirdPartySetting('commerce_rma', 'return_workflow', $return_workflow_id);
$order_type->setThirdPartySetting('commerce_rma', 'return_max_order_age', $return_max_order_age);
$order_type->save();
}
/**
* Builds the $order->returns field definition.
*
* @param string $order_type_id
* The order type ID.
*
* @return \Drupal\entity\BundleFieldDefinition
* The field definition.
*/
function commerce_rma_build_return_field_definition($order_type_id) {
$field_definition = BundleFieldDefinition::create('entity_reference')
->setTargetEntityTypeId('commerce_order')
->setTargetBundle($order_type_id)
->setName('returns')
->setLabel('Returns')
->setProvider('commerce_rma')
->setCardinality(BundleFieldDefinition::CARDINALITY_UNLIMITED)
->setSetting('target_type', 'commerce_return')
->setSetting('handler', 'default');
return $field_definition;
}
function commerce_rma_build_order_state_field_definition($order_type_id){
$field_definition = BundleFieldDefinition::create('state')
->setLabel(t('Return State'))
->setName('return_state')
->setDescription(t('The order return state.'))
->setRequired(FALSE)
->setTargetBundle($order_type_id)
->setSetting('max_length', 255)
->setTargetEntityTypeId('commerce_order')
->setDisplayOptions('view', [
'label' => 'hidden',
'type' => 'list_default',
'weight' => 10,
])
->setProvider('commerce_rma')
->setDisplayConfigurable('form', TRUE)
->setDisplayConfigurable('view', TRUE)
->setSetting('workflow_callback', 'commerce_rma_order_workflow_id');
return $field_definition;
}
function commerce_rma_order_workflow_id(OrderInterface $entity) {
/** @var \Drupal\commerce_order\Entity\OrderTypeInterface $order_type */
$order_type = \Drupal::entityTypeManager()->getStorage('commerce_order_type')->load($entity->bundle());
$workflow_id = $order_type->getThirdPartySetting('commerce_rma', 'return_workflow');
return $workflow_id;
}
/**
* Implements hook_inline_entity_form_table_fields_alter().
*/
function commerce_rma_inline_entity_form_table_fields_alter(&$fields, $context) {
if ($context['entity_type'] == 'commerce_return_item') {
$fields['unit_price'] = [
'type' => 'field',
'label' => t('Unit Price'),
'weight' => 101,
];
$fields['quantity'] = [
'type' => 'field',
'label' => t('Quantity'),
'weight' => 101,
];
$fields['reason'] = [
'type' => 'field',
'label' => t('Reason'),
'weight' => 101,
'display_options' => [
'type' => 'entity_reference_label',
'settings' => ['link' => FALSE],
],
];
$fields['expected_resolution'] = [
'type' => 'field',
'label' => t('Expected resolution'),
'weight' => 101,
'display_options' => [
'type' => 'entity_reference_label',
'settings' => ['link' => FALSE],
],
];
$fields['note'] = [
'type' => 'field',
'label' => t("Client's Note"),
'weight' => 101,
];
$fields['manager_note'] = [
'type' => 'field',
'label' => t("Manager's Note"),
'weight' => 101,
];
$fields['confirmed_price'] = [
'type' => 'field',
'label' => t('Confirmed Price'),
'weight' => 101,
];
$fields['confirmed_quantity'] = [
'type' => 'field',
'label' => t('Confirmed Quantity'),
'weight' => 101,
];
}
}
function commerce_rma_field_widget_form_alter(&$element, FormStateInterface $form_state, $context) {
$field_definition = $context['items']->getFieldDefinition();
$entity = $context['items']->getParent()->getEntity();
$entity_type_id = $entity->getEntityTypeId();
if ($entity_type_id == 'commerce_return_item' && $field_definition->isReadOnly()) {
if ($context["form"]["#op"] == 'edit') {
$element['#disabled'] = TRUE;
}
}
if ($entity_type_id == 'commerce_return_item' && $field_definition->getName() == 'confirmed_quantity') {
if ($context["form"]["#op"] == 'edit') {
/** @var \Drupal\commerce_rma\Entity\CommerceReturnItemInterface $return_item */
$return_item = $context["form"]["#default_value"];
$element["value"]["#min"] = 0;
$element["value"]["#max"] = $return_item->getQuantity();
$return = $form_state->getFormObject()->getEntity();
$confirmed_states = ['received', 'rejected', 'completed', 'canceled'];
if (in_array($return->get('state')->value, $confirmed_states)) {
$element["value"]["#max"] = $return_item->getConfirmedQuantity();
}
}
}
if ($entity_type_id == 'commerce_return' && $field_definition->isReadOnly()) {
$element['#disabled'] = TRUE;
//@todo BUG State chenged on shipping edit.
if ($field_definition->getName() == 'billing_profile') {
unset($element['#disabled']);
}
}
if ($entity_type_id == 'commerce_return' && $field_definition->getName() == 'shipping_profile') {
$disabled_states = ['received', 'rejected', 'completed', 'canceled'];
if (in_array($entity->get('state')->value, $disabled_states)) {
$element['#disabled'] = TRUE;
}
}
if ($entity_type_id == 'commerce_return_item' && $field_definition->getName() == 'field_attachments') {
if ($context["form"]["#op"] == 'edit') {
$element['#disabled'] = TRUE;
}
}
}
function commerce_rma_inline_entity_form_entity_form_alter(&$entity_form, &$form_state) {
if ($entity_form['#entity_type'] == 'commerce_return_item') {
if ($entity_form["#op"] == 'edit') {
/** @var \Drupal\commerce_order\Entity\OrderInterface $order */
$order = $form_state->getFormObject()->getEntity()->getOrder();
/** @var \Drupal\commerce_rma\Entity\CommerceReturnItemInterface[] $items */
$items = $form_state->getFormObject()->getEntity()->getItems();
$ids = [];
foreach ($items as $item) {
$ids[]= $item->getOrderItem()->id();
}
$entity_form['order_item']['#access'] = FALSE;
$entity_form['order_item_view'] = [
'#type' => 'view',
// @todo Allow the view to be configurable.
'#name' => 'commerce_rma_order_item_table',
'#arguments' => [$order->id(), implode($ids, ',')],
'#embed' => TRUE,
'#weight' => -100,
];
}
}
}
/**
* Implements hook_preprocess_commerce_return().
*/
function commerce_rma_preprocess_commerce_return(&$variables) {
/** @var \Drupal\commerce_rma\Entity\CommerceReturnInterface $commerce_return */
$commerce_return = $variables['elements']['#commerce_return'];
$variables['activity'] = [
'#type' => 'view',
'#name' => 'return_activity',
'#display_id' => 'default',
'#arguments' => [$commerce_return->id(), 'commerce_return'],
'#embed' => TRUE,
'#title' => t('Return activity'),
];
}