forked from yoomoney/cms-drupal8
-
Notifications
You must be signed in to change notification settings - Fork 1
/
yandex_checkout.install
53 lines (47 loc) · 1.62 KB
/
yandex_checkout.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
<?php
/**
* @file
* Provides module installation functions.
* @todo: ignore this phpcs rule.
*/
/**
* Implements hook_requirements().
*/
function yandex_checkout_requirements($phase) {
$requirements = [];
if ($phase === 'install') {
if (!class_exists('\YandexCheckout\Client')) {
$requirements['yandex_checkout_sdk_library'] = [
'description' => t('Yandex Checkout requires the yandex-money/yandex-checkout-sdk-php library.'),
'severity' => REQUIREMENT_ERROR,
];
}
}
return $requirements;
}
/**
* Updates placeholders in payments descriptions.
*/
function yandex_checkout_update_8001() {
$gateways = \Drupal::entityTypeManager()->getStorage('commerce_payment_gateway')
->loadByProperties(['plugin' => ['yandex_checkout', 'yandex_checkout_billing']]);
/** @var \Drupal\commerce_payment\Entity\PaymentGatewayInterface $gateway */
foreach ($gateways as $gateway) {
$plugin_id = $gateway->getPluginId();
$field_to_update = $plugin_id === 'yandex_checkout' ? 'description_template' : 'narrative';
$configuration = $gateway->getPluginConfiguration();
$string_to_replace = $configuration[$field_to_update] ?? NULL;
if (empty($string_to_replace)) {
continue;
}
$matches = [];
preg_match_all('/%(.*?)%/', $string_to_replace, $matches, PREG_SET_ORDER);
foreach ($matches as $match) {
$replace = "[commerce_order:$match[1]]";
$string_to_replace = str_replace($match[0], $replace, $string_to_replace);
}
$configuration[$field_to_update] = $string_to_replace;
$gateway->setPluginConfiguration($configuration);
$gateway->save();
}
}