-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsapi.module
184 lines (152 loc) · 4.83 KB
/
sapi.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
175
176
177
178
179
180
181
182
183
184
<?php
/**
* @file
* Hooks and functions for the Statistics API module.
*/
/**
* Implements hook_menu().
*/
function sapi_menu() {
// Statistics settings list page.
$items['admin/config/statistics'] = array(
'title' => 'Statistics',
'description' => 'General statistics related configuration.',
'position' => 'left',
'page callback' => 'system_admin_menu_block_page',
'access arguments' => array('access administration pages'),
'file' => 'system.admin.inc',
'file path' => drupal_get_path('module', 'system'),
);
// Listing of all declared Stat methods.
$items['admin/config/statistics/methods'] = array(
'title' => 'Statistics methods',
'description' => 'Manage defined statistics methods.',
'route_name' => 'sapi_stat_method_overview',
);
// Edit form for an individual Stat method.
$items['admin/config/statistics/methods/%stat_method'] = array(
'title' => 'Edit statistics methods',
'title callback' => 'entity_page_label',
'title arguments' => array(4),
'route_name' => 'sapi_stat_method_edit',
);
// Local task for the edit form.
$items['admin/config/statistics/methods/%stat_method/edit'] = array(
'title' => 'Edit',
'type' => MENU_DEFAULT_LOCAL_TASK,
);
// Statistics method enable callback.
$items['admin/config/statistics/methods/%stat_method/enable'] = array(
'title' => 'Delete',
'route_name' => 'sapi_stat_method_enable',
);
// Statistics method disable callback.
$items['admin/config/statistics/methods/%stat_method/disable'] = array(
'title' => 'Disable',
'route_name' => 'sapi_stat_method_disable',
);
return $items;
}
/**
* Implements hook_permission().
*/
function sapi_permission() {
$permissions = array();
// Expose a master adminster stat methods permission.
$permissions['administer statistics methods'] = array(
'title' => t('Administer statistics methods'),
'description' => t('Configure data collection methods.'),
'restrict access' => TRUE,
);
// Expose one permission per stat method.
foreach (sapi_get_method_plugins() as $id => $definition) {
$t_args = array('%method' => $definition['label']);
$permissions['configure ' . $id . ' method'] = array(
'title' => t('Configure the %method method', $t_args),
'description' => t('Make configuration changes to the %method method.', $t_args),
);
}
return $permissions;
}
/**
* Implements hook_entity_bundle_info().
*/
function sapi_entity_bundle_info() {
$bundles = array();
// For each Statistics method plugin defined, note a Stat entity bundle.
foreach (sapi_get_method_plugins() as $id => $definition) {
$bundles['stat'][$id]['label'] = $definition['label'];
}
return $bundles;
}
/**
* Implements hook_schema_alter().
*/
function sapi_schema_alter(&$schema) {
// Retrieve and indicate all active plugins
$plugins = sapi_get_data_plugins();
foreach ($plugins as $id => $definition) {
$schema['stat']['fields'][$id] = $definition['schema'];
}
}
/**
* Implements hook_cron().
*
* @todo Needs tests.
*/
function sapi_cron() {
// Loop through defined stat methods and call manageData().
$manager = Drupal::service('plugin.manager.sapi.method');
foreach (sapi_get_method_plugins() as $id => $definition) {
$method = $manager->createInstance($id);
$method->manageData();
}
}
/**
* Implements hook_modules_uninstalled().
*
* @todo Needs tests.
*/
function sapi_modules_uninstalled($modules) {
$plugins = Drupal::state()->get('sapi_data_plugin_purgatory') ?: array();
$modules = array_flip($modules);
// Loop through the data plugins.
foreach (sapi_get_data_plugins() as $plugin => $definition) {
// If an uninstalled module matches, remove its property from storage.
if (isset($modules[$definition['provider']])) {
\Drupal::entityManager()->getStorageController('stat')->ensureNoProperty($plugin);
}
}
}
/**
* Helper function to get a list of statistics method plugin definitions.
*
* @param boolean $clear_cache
* Optional: Whether or not the plugin definition cache should be flushed.
*
* @return array
* An array of Statistics method plugins, keyed by plugin ID.
*/
function sapi_get_method_plugins($clear_cache = FALSE) {
$plugin = Drupal::service('plugin.manager.sapi.method');
if ($clear_cache) {
$plugin->clearCachedDefinitions();
}
return $plugin->getDefinitions();
}
/**
* Helper function to get a list of statistics data plugin definitions.
*
* @param boolean $clear_cache
* Optional: Whether or not the plugin definition cache should be flushed.
*
* @return array
* An array of Statistics data plugins, keyed by plugin ID.
*/
function sapi_get_data_plugins($clear_cache = FALSE) {
$plugin = Drupal::service('plugin.manager.sapi.data');
if ($clear_cache) {
$plugin->clearCachedDefinitions();
}
return $plugin->getDefinitions();
}