-
Notifications
You must be signed in to change notification settings - Fork 0
/
commerce_pickup.module
155 lines (143 loc) · 5.69 KB
/
commerce_pickup.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
<?php
/**
* @file
* Primary module hooks for Commerce Pickup module.
*/
use Drupal\Core\Form\FormStateInterface;
use Drupal\commerce_pickup\Plugin\Commerce\CheckoutPane\PickupShippingInformation;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Entity\EntityInterface;
use Drupal\user\Entity\User;
/**
* Implements hook_theme().
*/
function commerce_pickup_theme($existing, $type, $theme, $path) {
return [
'commerce_order__admin' => [
'base hook' => 'commerce_order',
'render element' => 'elements',
],
'commerce_order__user' => [
'base hook' => 'commerce_order',
'render element' => 'elements',
],
'commerce_order_receipt' => [
'variables' => [
'order_entity' => NULL,
'billing_information' => NULL,
'shipping_information' => NULL,
'payment_method' => NULL,
'totals' => NULL,
],
],
'commerce_order_receipt__entity_print' => [
'base hook' => 'commerce_order_receipt',
],
];
}
/**
* Implements hook_commerce_checkout_pane_info_alter().
*/
function commerce_pickup_commerce_checkout_pane_info_alter(array &$info) {
if (isset($info['shipping_information'])) {
$info['shipping_information']['class'] = PickupShippingInformation::class;
$info['shipping_information']['provider'] = 'commerce_pickup';
}
}
/**
* Implements hook_entity_presave().
*/
function commerce_pickup_entity_presave(EntityInterface $entity) {
$entity_type = $entity->getEntityTypeId();
if ($entity_type == 'profile' && $entity->bundle() == 'pickup' && $entity->isActive()) {
$manager = \Drupal::service('commerce_pickup.options_manager');
$manager->flushPickupAddresses([$entity]);
}
elseif ($entity_type == 'user' && $vendor_id = $entity->id()) {
$is_vendor = $entity->hasRole('pickup_vendor');
$was_vendor = $entity->original->hasRole('pickup_vendor');
$is_blocked = $entity->isBlocked();
$was_blocked = $entity->original->isBlocked();
if (($was_vendor && !$is_vendor)
|| ($is_vendor && $is_blocked)
|| (!$was_vendor && $is_vendor && !$is_blocked)
|| ($is_vendor && $was_blocked && !$is_blocked)) {
$profiles = \Drupal::entityTypeManager()->getStorage('profile')->loadByProperties([
'uid' => $vendor_id,
'type' => 'pickup',
'status' => 1,
]);
$manager = \Drupal::service('commerce_pickup.options_manager');
$manager->flushPickupAddresses($profiles);
}
}
}
/**
* Implements hook_entity_predelete().
*/
function commerce_pickup_entity_predelete(EntityInterface $entity) {
if ($entity->bundle() == 'pickup' && $entity->getEntityTypeId() == 'profile' && $entity->isActive()) {
$manager = \Drupal::service('commerce_pickup.options_manager');
$manager->flushPickupAddresses([$entity]);
}
}
/**
* Implements hook_entity_base_field_info().
*/
function commerce_pickup_form_alter(&$form, FormStateInterface $form_state, $form_id) {
if (preg_match('/^profile_pickup_(edit|add)_form$/', $form_id)) {
if (isset($form['pickup_address'])) {
$address = &NestedArray::getValue($form, ['pickup_address', 'widget', 0]);
$address['address']['#after_build'][] = 'commerce_pickup_address_customize';
if (empty($address['address']['#default_value']['organization'])) {
if ($profile = $form_state->getFormObject()->getEntity()) {
$address['address']['#default_value']['organization'] = $profile->getOwner()->getAccountName();
}
else {
$address['address']['#default_value']['organization'] = \Drupal::currentUser()->getAccountName();
}
}
if (empty($address['address']['#default_value']['family_name'])) {
if ($logo = User::load(\Drupal::currentUser()->id())->user_picture->entity) {
$address['address']['#default_value']['family_name'] = $logo->createFileUrl();
}
}
}
if (isset($form['pickup_timezone'])) {
$parents = ['pickup_timezone', 'widget', 0, 'value', '#default_value'];
if (!NestedArray::getValue($form, $parents)) {
NestedArray::setValue($form, $parents, \Drupal::currentUser()->getTimeZone());
}
$address['pickup_timezone'] = $form['pickup_timezone'];
$address['pickup_timezone']['#weight'] = -1;
unset($form['pickup_timezone']);
}
if (isset($form['pickup_stores'])) {
$stores = &$form['pickup_stores'];
$stores['#type'] = 'details';
$stores['#open'] = TRUE;
$stores['#title'] = $stores['widget']['#title'] ?? $stores['widget']['target_id']['#title'];
$stores['#description'] = t('Select stores which are allowed to use this pickup point.');
$stores['#title_display'] = 'invisible';
}
if (isset($form['pickup_hours'])) {
$form['pickup_hours']['widget']['#description'] = t("Define at least one working day or its comment.");
$form['pickup_hours']['#suffix'] = t('Later the %set_inactive VBO action can be used If you need to hide this pickup point on a vacation period.', [
'%set_inactive' => t('Set inactive'),
]);
}
}
}
/**
* Customize store pickup address field components.
*/
function commerce_pickup_address_customize($element, $form_state) {
$element['organization']['#weight'] = -10;
$element['given_name']['#size'] = $element['family_name']['#size'] = 60;
$element['given_name']['#title'] = t('Pickup point description');
$element['given_name']['#description'] = t('Required when exporting data to the Google Places.');
$element['family_name']['#title'] = t('Pickup point Logo');
$element['family_name']['#description'] = t('Relative or absolute url of an image (png, jpg, jpeg, svg).');
$element['given_name']['#placeholder'] = $element['family_name']['#placeholder'] = t('Optional');
return $element;
}