forked from apigee/apigee-m10n-drupal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapigee_m10n.module
113 lines (104 loc) · 3.4 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
<?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\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',
],
'api_product' => [
'variables' => ['api_product' => NULL],
],
'rate_plan_detail' => [
'variables' => [
'detail' => NULL,
'ratecard_rates' => NULL,
'revshare_rates' => NULL,
'free_quantity' => NULL,
'entity' => NULL,
],
'template' => 'rate-plan-detail',
],
'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_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));
}