-
Notifications
You must be signed in to change notification settings - Fork 129
/
weeshop.install
117 lines (104 loc) · 3.76 KB
/
weeshop.install
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
<?php
/**
* @file
* Install, update and uninstall functions for the installation profile.
*/
use Drupal\commerce_shipping\Entity\ShippingMethod;
use Drupal\Core\Entity\EntityStorageException;
use Drupal\Core\File\FileSystemInterface;
use Drupal\user\RoleInterface;
/**
* Implements hook_install().
*
* Perform actions to set up the site for this profile.
*
* @see system_install()
*/
function weeshop_install(): void {
// First, do everything that is done in the standard profile.
include_once DRUPAL_ROOT . '/core/profiles/standard/standard.install';
standard_install();
// Enable default permissions for system roles.
user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, ['access comments']);
user_role_grant_permissions(RoleInterface::AUTHENTICATED_ID, [
'access comments',
'post comments',
'skip comment approval',
]);
user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, ['access site-wide contact form']);
user_role_grant_permissions(RoleInterface::AUTHENTICATED_ID, ['access site-wide contact form']);
// Allow all users to use search.
user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, ['search content']);
user_role_grant_permissions(RoleInterface::AUTHENTICATED_ID, ['search content']);
// Set Default user picture.
_weeshop_set_default_user_picture();
$permissions = [
'use search_api_autocomplete for product_search',
];
user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, $permissions);
_weeshop_set_shipping();
}
/**
* Set default picture to user.user_picture field.
*/
function _weeshop_set_default_user_picture(): void {
$avatar_path = Drupal::config('system.file')->get('default_scheme') . '://default_user_picture.png';
/** @var \Drupal\file\FileRepositoryInterface $fileRepository */
$fileRepository = Drupal::service('file.repository');
try {
$file = $fileRepository->writeData(
file_get_contents(__DIR__ . '/assets/image/default_user_picture.png'),
$avatar_path,
FileSystemInterface::EXISTS_REPLACE
);
Drupal::configFactory()->getEditable('field.field.user.user.user_picture')
->set('settings.default_image.uuid', $file->uuid())
->set('alt', 'Default user picture')
->set('title', 'Default user picture')
->save(TRUE);
}
catch (Exception $e) {
Drupal::messenger()->addError(t('default_user_picture.png count not be saved to :path, :e', [
':path' => $avatar_path,
':e' => $e->getMessage(),
]));
}
}
/**
* Set shipping for default order type.
*/
function _weeshop_set_shipping(): void {
Drupal::configFactory()->getEditable('commerce_order.commerce_order_type.default')
->set('workflow', 'order_fulfillment')
->set('third_party_settings.commerce_checkout.checkout_flow', 'shipping')
->set('third_party_settings.commerce_shipping.shipment_type', 'default')
->save(TRUE);
// Create the order field.
$field_definition = commerce_shipping_build_shipment_field_definition('default');
Drupal::service('commerce.configurable_field_manager')->createField($field_definition);
try {
ShippingMethod::create([
'stores' => [1, 2, 3],
'plugin' => [
'target_plugin_id' => 'flat_rate',
'target_plugin_configuration' => [
'rate_label' => 'Fixed rate',
'rate_description' => '',
'rate_amount' => [
'number' => '10',
'currency_code' => 'CNY',
],
'services' => ['default'],
'default_package_type' => 'custom_box',
'workflow' => 'shipment_default',
],
],
'name' => 'Standard express',
])->save();
}
catch (EntityStorageException $e) {
Drupal::messenger()->addError(t('Shipping method for default order type could not be saved., :e', [
':e' => $e->getMessage(),
]));
}
}