1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @file |
4
|
|
|
* Code for the DF Admin feature. |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
include_once 'df_admin.features.inc'; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Implements hook_migrate_api(). |
11
|
|
|
*/ |
12
|
|
|
function df_admin_migrate_api() { |
13
|
|
|
$api = array( |
14
|
|
|
'api' => 2, |
15
|
|
|
'groups' => array( |
16
|
|
|
'df' => array( |
17
|
|
|
'title' => t('Demo Framework'), |
18
|
|
|
), |
19
|
|
|
), |
20
|
|
|
); |
21
|
|
|
return $api; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Implements hook_menu(). |
26
|
|
|
*/ |
27
|
|
|
function df_admin_menu() { |
28
|
|
|
$items['admin/df'] = array( |
|
|
|
|
29
|
|
|
'title' => 'Demo Framework', |
30
|
|
|
'description' => 'Manage Demo Framework Scenarios and configurations.', |
31
|
|
|
'page callback' => 'df_admin_page', |
32
|
|
|
'page arguments' => array(), |
33
|
|
|
'access arguments' => array('administer demo framework'), |
34
|
|
|
'type' => MENU_NORMAL_ITEM, |
35
|
|
|
'weight' => 50, |
36
|
|
|
'file' => 'df_admin.page.inc', |
37
|
|
|
); |
38
|
|
|
$items['admin/df/enable/%'] = array( |
39
|
|
|
'page callback' => 'df_admin_page_enable', |
40
|
|
|
'page arguments' => array(3), |
41
|
|
|
'access arguments' => array('administer demo framework'), |
42
|
|
|
'type' => MENU_CALLBACK, |
43
|
|
|
'file' => 'df_admin.enable.inc', |
44
|
|
|
); |
45
|
|
|
$items['admin/df/reset/%'] = array( |
46
|
|
|
'page callback' => 'df_admin_page_reset', |
47
|
|
|
'page arguments' => array(3), |
48
|
|
|
'access arguments' => array('administer demo framework'), |
49
|
|
|
'type' => MENU_CALLBACK, |
50
|
|
|
'file' => 'df_admin.reset.inc', |
51
|
|
|
); |
52
|
|
|
$items['admin/df/uninstall'] = array( |
53
|
|
|
'page callback' => 'df_admin_page_uninstall', |
54
|
|
|
'page arguments' => array(), |
55
|
|
|
'access arguments' => array('administer demo framework'), |
56
|
|
|
'type' => MENU_CALLBACK, |
57
|
|
|
'file' => 'df_admin.uninstall.inc', |
58
|
|
|
); |
59
|
|
|
|
60
|
|
|
return $items; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Implements hook_theme(). |
65
|
|
|
*/ |
66
|
|
|
function df_admin_theme(){ |
67
|
|
|
return array( |
68
|
|
|
'df_admin_page_template' => array( |
69
|
|
|
'template' => 'df-admin-page', |
70
|
|
|
'variables' => array('scenarios' => NULL), |
71
|
|
|
), |
72
|
|
|
); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Alter navbar menu to clean things up a bit. |
77
|
|
|
* |
78
|
|
|
* Implements hook_navbar_alter(). |
79
|
|
|
*/ |
80
|
|
|
function df_admin_navbar_alter(&$items) { |
81
|
|
|
foreach ($items['administration']['tray']['navbar_administration']['administration_menu'] as $mid => $item) { |
82
|
|
|
// Change "My Workbench" to "Workbench" in the Navbar. |
83
|
|
View Code Duplication |
if (isset($item['#href']) && $item['#href'] == 'admin/workbench') { |
|
|
|
|
84
|
|
|
$items['administration']['tray']['navbar_administration']['administration_menu'][$mid]['#title'] = 'Workbench'; |
85
|
|
|
} |
86
|
|
|
// Change "Modules" to "Extend" to reflect D8 Toolbar. |
87
|
|
View Code Duplication |
if (isset($item['#href']) && $item['#href'] == 'admin/modules') { |
|
|
|
|
88
|
|
|
$items['administration']['tray']['navbar_administration']['administration_menu'][$mid]['#title'] = 'Extend'; |
89
|
|
|
} |
90
|
|
|
// Remove Help and Advanced Help from the Navbar. |
91
|
|
|
$remove = array('admin/help', 'admin/advanced_help'); |
92
|
|
|
if (isset($item['#href']) && in_array($item['#href'], $remove)) { |
93
|
|
|
unset($items['administration']['tray']['navbar_administration']['administration_menu'][$mid]); |
94
|
|
|
} |
95
|
|
|
// Shorten "Demo Framework" to "Demo" on the Navbar. |
96
|
|
View Code Duplication |
if(isset($item['#href']) && $item['#href'] == 'admin/df') { |
|
|
|
|
97
|
|
|
$items['administration']['tray']['navbar_administration']['administration_menu'][$mid]['#title'] = 'Demo'; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Alter advanced_help_topic_info. |
104
|
|
|
* |
105
|
|
|
* Advanced Help only displays topics for enabled modules. Add topics for any |
106
|
|
|
* disabled Scenarios. |
107
|
|
|
*/ |
108
|
|
|
function df_admin_advanced_help_topic_info_alter(&$ini) { |
109
|
|
|
global $language; |
110
|
|
|
|
111
|
|
|
$modules = system_rebuild_module_data(); |
112
|
|
|
foreach ($modules as $index => $module) { |
113
|
|
|
// Process only modules that are Scenarios and are not enabled. |
114
|
|
|
if ($module->info['package'] != 'Demo Framework Scenarios') continue; |
115
|
|
|
if ($module->status) continue; |
116
|
|
|
|
117
|
|
|
// Functionality taken from _advanced_help_parse_ini(). |
118
|
|
|
$module_path = drupal_get_path('module', $module->name); |
119
|
|
|
$info = array(); |
|
|
|
|
120
|
|
|
if (file_exists("$module_path/help/$module->name.help.ini")) { |
121
|
|
|
$info = parse_ini_file("./$module_path/help/$module->name.help.ini", TRUE); |
122
|
|
|
|
123
|
|
|
if (!empty($info)) { |
124
|
|
|
// Get translated titles: |
125
|
|
|
$translation = array(); |
126
|
|
|
if (file_exists("$module_path/translations/help/$language->language/$module->name.help.ini")) { |
127
|
|
|
$translation = parse_ini_file("$module_path/translations/help/$language->language/$module->name.help.ini", TRUE); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
$ini['settings'][$module->name] = array(); |
131
|
|
|
if (!empty($info['advanced help settings'])) { |
132
|
|
|
$ini['settings'][$module->name] = $info['advanced help settings']; |
133
|
|
|
unset($info['advanced help settings']); |
134
|
|
|
|
135
|
|
|
// Check translated strings for translatable global settings. |
136
|
|
View Code Duplication |
if (isset($translation['advanced help settings']['name'])) { |
|
|
|
|
137
|
|
|
$ini['settings']['name'] = $translation['advanced help settings']['name']; |
138
|
|
|
} |
139
|
|
View Code Duplication |
if (isset($translation['advanced help settings']['index name'])) { |
|
|
|
|
140
|
|
|
$ini['settings']['index name'] = $translation['advanced help settings']['index name']; |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
foreach ($info as $name => $topic) { |
145
|
|
|
// Each topic should have a name, a title, a file and path. |
146
|
|
|
$file = !empty($topic['file']) ? $topic['file'] : $name; |
147
|
|
|
$ini['topics'][$module->name][$name] = array( |
148
|
|
|
'name' => $name, |
149
|
|
|
'module' => $module->name, |
150
|
|
|
'ini' => $topic, |
151
|
|
|
'title' => !empty($translation[$name]['title']) ? $translation[$name]['title'] : $topic['title'], |
152
|
|
|
'weight' => isset($topic['weight']) ? $topic['weight'] : 0, |
153
|
|
|
'parent' => isset($topic['parent']) ? $topic['parent'] : 0, |
154
|
|
|
'popup width' => isset($topic['popup width']) ? $topic['popup width'] : 500, |
155
|
|
|
'popup height' => isset($topic['popup height']) ? $topic['popup height'] : 500, |
156
|
|
|
'file' => $file . '.html', |
157
|
|
|
// Not in .ini file. |
158
|
|
|
'path' => "$module_path/help", |
159
|
|
|
'line break' => isset($topic['line break']) ? $topic['line break'] : (isset($ini['settings'][$module->name]['line break']) ? $ini['settings'][$module->name]['line break'] : FALSE), |
160
|
|
|
'navigation' => isset($topic['navigation']) ? $topic['navigation'] : (isset($ini['settings'][$module->name]['navigation']) ? $ini['settings'][$module->name]['navigation'] : TRUE), |
161
|
|
|
'css' => isset($topic['css']) ? $topic['css'] : (isset($ini['settings'][$module->name]['css']) ? $ini['settings'][$module->name]['css'] : NULL), |
162
|
|
|
'readme file' => FALSE, |
163
|
|
|
); |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Helper function to reset and revert all Features. |
172
|
|
|
*/ |
173
|
|
|
function df_admin_revert_all() { |
174
|
|
|
// Clear & rebuild Features key caches. |
175
|
|
|
features_get_info(NULL, NULL, TRUE); |
176
|
|
|
features_rebuild(); |
177
|
|
|
|
178
|
|
|
// Revert all Features (cleanup everything). |
179
|
|
|
features_revert(); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Helper function to return all DFS modules. |
184
|
|
|
*/ |
185
|
|
|
function df_get_scenario_modules() { |
186
|
|
|
$scenarios = array(); |
187
|
|
|
$modules = system_rebuild_module_data(); |
188
|
|
|
foreach ($modules as $name => $module) { |
189
|
|
|
if ($module->info['package'] == 'Demo Framework Scenarios') { |
190
|
|
|
$scenarios[$name] = $module; |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
return $scenarios; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Defines a library for the jquery-migrate plugin |
198
|
|
|
*/ |
199
|
|
|
function df_admin_library() { |
200
|
|
|
$libraries['jquery-migrate'] = array( |
|
|
|
|
201
|
|
|
'title' => 'jQuery Migrate Plugin', |
202
|
|
|
'website' => 'https://github.com/jquery/jquery-migrate', |
203
|
|
|
'version' => '1.2.1', |
204
|
|
|
'js' => array( |
205
|
|
|
drupal_get_path('module', 'df_admin') . '/js/jquery-migrate-1.2.1.min.js' => array(), |
206
|
|
|
), |
207
|
|
|
); |
208
|
|
|
return $libraries; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Adds jQuery 1.9+ compatibility by adding $.browser and other components back |
213
|
|
|
*/ |
214
|
|
|
function df_admin_page_alter(&$page) { |
|
|
|
|
215
|
|
|
drupal_add_library('df_admin', 'jquery-migrate', TRUE); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* Batch API finished callback - report results |
220
|
|
|
* |
221
|
|
|
* @param $success |
222
|
|
|
* Ignored |
223
|
|
|
* @param $results |
224
|
|
|
* List of results from batch processing |
225
|
|
|
* @param $operations |
226
|
|
|
* Ignored |
227
|
|
|
*/ |
228
|
|
|
function df_admin_batch_finish($success, $results, $operations) { |
|
|
|
|
229
|
|
|
unset($results['stopped']); |
230
|
|
|
if (user_access(MIGRATE_ACCESS_ADVANCED)) { |
231
|
|
|
foreach ($results as $result) { |
232
|
|
|
drupal_set_message($result['message']); |
233
|
|
|
} |
234
|
|
|
} |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* BatchAPI callback. |
239
|
|
|
* |
240
|
|
|
* Logs a message with the watchdog once features have been reverted after |
241
|
|
|
* enabling a scenario. |
242
|
|
|
* |
243
|
|
|
* @see df_admin_reset_scenario() |
244
|
|
|
*/ |
245
|
|
|
function _df_admin_watchdog_revert_features($scenario, &$context) { |
|
|
|
|
246
|
|
|
watchdog('df_admin', 'Features reverted after enabling the %scenario scenario.', array('%scenario' => $scenario), WATCHDOG_INFO); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* BatchAPI callback. |
251
|
|
|
* |
252
|
|
|
* Logs a message with the watchdog once defaultconfig has been rebuilt after |
253
|
|
|
* enabling a scenario. |
254
|
|
|
* |
255
|
|
|
* @see df_admin_reset_scenario() |
256
|
|
|
*/ |
257
|
|
|
function _df_admin_watchdog_defaultconfig_rebuild($scenario, &$context) { |
|
|
|
|
258
|
|
|
watchdog('df_admin', 'Rebuilt defaultconfig after enabling the %scenario scenario.', array('%scenario' => $scenario), WATCHDOG_INFO); |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* BatchAPI callback. |
263
|
|
|
* |
264
|
|
|
* Logs a message with the watchdog once caches have been cleared after enabling |
265
|
|
|
* a scenario. |
266
|
|
|
* |
267
|
|
|
* @see df_admin_reset_scenario() |
268
|
|
|
*/ |
269
|
|
|
function _df_admin_watchdog_cache_clear($scenario, &$context) { |
|
|
|
|
270
|
|
|
watchdog('df_admin', 'Cleared caches after enabling the %scenario scenario.', array('%scenario' => $scenario), WATCHDOG_INFO); |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* Implements hook_entity_insert(). |
276
|
|
|
* |
277
|
|
|
* Replaces instances of "sites/default/files" in fields with the current file |
278
|
|
|
* path. |
279
|
|
|
*/ |
280
|
|
|
function df_admin_entity_insert($entity, $type, $ignore_migrate = FALSE) { |
281
|
|
|
if (isset($entity->migrate) || $ignore_migrate) { |
282
|
|
|
$bundle = NULL; |
283
|
|
|
if (isset($entity->type)) { |
284
|
|
|
$bundle = $entity->type; |
285
|
|
|
} |
286
|
|
|
else if (isset($entity->bundle)) { |
287
|
|
|
$bundle = $entity->bundle; |
288
|
|
|
} |
289
|
|
|
$fields = array_keys(field_info_instances($type, $bundle)); |
290
|
|
|
foreach ($fields as $field) { |
291
|
|
|
if (isset($entity->{$field}) && isset($entity->{$field}[LANGUAGE_NONE][0]['value'])) { |
292
|
|
|
$value = $entity->{$field}[LANGUAGE_NONE][0]['value']; |
293
|
|
|
if (strpos($value, 'sites/default/files') !== FALSE) { |
294
|
|
|
$file_path = variable_get('file_public_path', 'sites/default/files'); |
295
|
|
|
$entity->{$field}[LANGUAGE_NONE][0]['value'] = str_replace('sites/default/files', $file_path, $value); |
296
|
|
|
entity_save($type, $entity); |
297
|
|
|
} |
298
|
|
|
} |
299
|
|
|
} |
300
|
|
|
} |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
/** |
304
|
|
|
* Replaces instances of "sites/default/files" in fields with the current file |
305
|
|
|
* path, for all entities we care about. |
306
|
|
|
*/ |
307
|
|
|
function _df_admin_reset_file_paths() { |
308
|
|
|
$entity_types = array('user', 'fieldable_panels_pane', 'node'); |
309
|
|
|
foreach ($entity_types as $type) { |
310
|
|
|
foreach (entity_load($type) as $entity) { |
311
|
|
|
df_admin_entity_insert($entity, $type, TRUE); |
312
|
|
|
} |
313
|
|
|
} |
314
|
|
|
} |
315
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.