-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommerce_recurring.module
126 lines (113 loc) · 4.47 KB
/
commerce_recurring.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
<?php
/**
* @file
* Provides recurring billing for Drupal Commerce.
*/
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\commerce\PurchasableEntityInterface;
/**
* Implements hook_commerce_entity_trait_info_alter().
*/
function commerce_recurring_commerce_entity_trait_info_alter(array &$definitions) {
// Expose the purchasable entity trait for every purchasable entity type.
$entity_types = \Drupal::entityTypeManager()->getDefinitions();
$entity_types = array_filter($entity_types, function (EntityTypeInterface $entity_type) {
return $entity_type->entityClassImplements(PurchasableEntityInterface::class);
});
$entity_type_ids = array_keys($entity_types);
$definitions['purchasable_entity_subscription']['entity_types'] = $entity_type_ids;
}
/**
* Implements hook_cron().
*/
function commerce_recurring_cron() {
\Drupal::service('commerce_recurring.cron')->run();
}
/**
* Implements hook_field_widget_form_alter().
*/
function commerce_recurring_field_widget_form_alter(&$element, FormStateInterface $form_state, $context) {
$field_definition = $context['items']->getFieldDefinition();
$field_name = $field_definition->getName();
$widget_name = $context['widget']->getPluginId();
if ($field_name == 'subscription_type' && $widget_name == 'commerce_plugin_select') {
// The standalone subscription type is used for subscriptions not backed
// by a purchasable entity, so it doesn't make sense to offer it on a
// purchasable entity form (such as the product variation one).
unset($element['target_plugin_id']['#options']['standalone']);
}
}
/**
* Implements hook_form_FORM_ID_alter() for 'commerce_order_type_form'.
*/
function commerce_recurring_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();
if ($order_type->id() != 'recurring') {
// The recurring workflow is only meant for the recurring order type.
unset($form['workflow']['#options']['Order']['order_recurring']);
}
}
/**
* Implements hook_form_FORM_ID_alter() for 'commerce_order_item_type_form'.
*/
function commerce_recurring_form_commerce_order_item_type_form_alter(array &$form, FormStateInterface $form_state) {
/** @var \Drupal\commerce_order\Entity\OrderTypeInterface $order_item_type */
$order_item_type = $form_state->getFormObject()->getEntity();
if (!in_array($order_item_type->id(), commerce_recurring_order_item_types())) {
// Hide the recurring order type from the dropdown, to avoid confusing
// merchants who are configuring non-recurring order item types.
unset($form['orderType']['#options']['recurring']);
}
}
/**
* Implements hook_form_FORM_ID_alter() for 'commerce_product_variation_type_form'.
*/
function commerce_recurring_form_commerce_product_variation_type_form_alter(array &$form, FormStateInterface $form_state) {
if (!isset($form['orderItemType'])) {
return;
}
// Recurring order item types are used by the module, they should never
// be selected by the merchant for a product variation type.
foreach ($form['orderItemType']['#options'] as $id => $label) {
if (in_array($id, commerce_recurring_order_item_types())) {
unset($form['orderItemType']['#options'][$id]);
}
}
}
/**
* Implements hook_form_FORM_ID_alter() for 'commerce_order_add_form'.
*/
function commerce_recurring_form_commerce_order_add_form_alter(array &$form, FormStateInterface $form_state) {
$form['type']['#after_build'] = ['commerce_recurring_order_type_after_build'];
}
/**
* After build callback for the order type radio buttons in the order add form.
*/
function commerce_recurring_order_type_after_build(array $element, FormStateInterface $form_state) {
// Hide the recurring order type from the radio buttons, it should never
// be selected by the merchant when creating an order from the admin.
unset($element['value']['#options']['recurring']);
$element['value']['recurring']['#access'] = FALSE;
return $element;
}
/**
* Gets the recurring order item types.
*
* @return string[]
* The recurring order item type IDs.
*/
function commerce_recurring_order_item_types() {
return ['recurring_standalone', 'recurring_product_variation'];
}
/**
* Implements hook_theme().
*/
function commerce_recurring_theme($existing, $type, $theme, $path) {
return [
'commerce_subscription_form' => [
'render element' => 'form',
],
];
}