forked from apigee/apigee-m10n-drupal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
apigee_m10n.module
executable file
·174 lines (162 loc) · 5.25 KB
/
apigee_m10n.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
<?php
/**
* @file
* Copyright 2018 Google Inc.
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License version 2 as published by the
* Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
* License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc., 51
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Extension\InfoParser;
use Drupal\Core\Extension\InfoParserInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\apigee_m10n\Entity\RatePlanInterface;
/**
* Implements hook_ENTITY_TYPE_access().
*/
function apigee_m10n_api_product_access(EntityInterface $entity, $operation, AccountInterface $account) {
// For the assignment operation a product must either be free or purchased.
if ($operation == 'assign') {
return \Drupal::service('apigee_m10n.monetization')
->apiProductAssignmentAccess($entity, $account);
}
// No opinion on other operations.
return AccessResult::neutral();
}
/**
* Implements hook_theme().
*/
function apigee_m10n_theme($existing, $type, $theme, $path) {
return [
'apigee_entity__rate_plan' => [
'base hook' => 'apigee_entity',
],
'apigee_entity__product_bundle' => [
'base hook' => 'apigee_entity',
],
'apigee_entity__xrate_plan' => [
'base hook' => 'apigee_entity',
],
'apigee_entity__xproduct' => [
'base hook' => 'apigee_entity',
],
'api_product' => [
'variables' => ['api_product' => NULL],
],
'rate_plan_detail' => [
'variables' => [
'detail' => NULL,
'ratecard_rates' => NULL,
'revshare_rates' => NULL,
'free_quantity' => NULL,
'entity' => NULL,
'apiproduct' => NULL,
],
'template' => 'rate-plan-detail',
],
'rate_plan_xfee' => [
'variables' => [
'detail' => NULL,
'entity' => NULL,
],
'template' => 'rate-plan-xfee',
],
'rate_plan_fixed_recurringfee' => [
'variables' => [
'detail' => NULL,
'entity' => NULL,
],
'template' => 'rate-plan-fixed-recurringfee',
],
'rate_plan_consumption_rates' => [
'variables' => [
'detail' => NULL,
'fee_currency_code' => NULL,
'fee_units' => NULL,
'fee_nanos' => NULL,
'multipleConsumptionText' => NULL,
'singleConsumptionText' => NULL,
'entity' => NULL,
],
'template' => 'rate-plan-consumption-rates',
],
'rate_plan_revenue_rates' => [
'variables' => [
'detail' => NULL,
'revenueSharePercentage' => NULL,
'singleShareText' => NULL,
'multipleShareText' => NULL,
'entity' => NULL,
],
'template' => 'rate-plan-revenue-rates',
],
'ratecard_rates' => [
'variables' => [
'rates' => NULL,
],
],
'revshare_rates' => [
'variables' => [
'rates' => NULL,
],
],
'conflicting_products' => [
'variables' => ['items' => NULL],
'template' => 'conflicting-products',
],
];
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function apigee_m10n_form_user_admin_permissions_alter(&$form, FormStateInterface $form_state, $form_id) {
\Drupal::service('apigee_m10n.monetization')->formUserAdminPermissionsAlter($form, $form_state, $form_id);
}
/**
* Implements hook_apigee_edge_user_agent_string_alter().
*/
function apigee_m10n_apigee_edge_user_agent_string_alter(&$user_agent_parts) {
$app_root = \Drupal::hasService('kernel') ? \Drupal::root() : DRUPAL_ROOT;
$infoParser = new InfoParser($app_root);
$m10_module_info = $infoParser->parse(\Drupal::service('module_handler')->getModule('apigee_m10n')->getPathname());
if (!isset($m10_module_info['version'])) {
$m10_module_info['version'] = '2.x-dev';
}
// m10 module info at beginning of the array.
array_unshift($user_agent_parts , $m10_module_info['name'] . '/' . $m10_module_info['version']);
}
/**
* Implements hook_ENTITY_TYPE_presave().
*/
function apigee_m10n_user_role_presave(EntityInterface $entity) {
\Drupal::service('apigee_m10n.monetization')->userRolePresave($entity);
}
/**
* Implements hook_preprocess_field().
*/
function apigee_m10n_preprocess_field(&$variables) {
// Add a library to the rate plan `futurePlanLinks` field.
if ($variables['field_name'] === 'futurePlanLinks') {
$variables['#attached']['library'][] = 'apigee_m10n/rate_plan.future_links_field';
}
}
/**
* Implements hook_ENTITY_TYPE_create_access().
*/
function apigee_m10n_purchased_plan_create_access(AccountInterface $account, array $context, $entity_bundle) {
// Make sure rate plan context exists.
$access = AccessResult::allowedIf(isset($context['rate_plan']) && $context['rate_plan'] instanceof RatePlanInterface);
return $access->andIf($context['rate_plan']->access('purchase', $account, TRUE));
}