-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththeming.module
62 lines (58 loc) · 1.96 KB
/
theming.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
<?php
/**
* @file
* Hook implementations for the theming module.
*/
/**
* Implements hook_theme_registry_alter().
*/
function theming_theme_registry_alter(&$hooks) {
$path = drupal_get_path('module', 'theming');
$file = 'theming.theme.inc';
$override = [
'form_element',
'form_element_label',
];
foreach ($override as $name) {
$hook = &$hooks[$name];
// Only replace the theme function when no theme overrides it.
if ($hook['function'] == "theme_$name") {
$hook['theme path'] = $path;
$hook['file'] = $file;
$hook['function'] = "theming_$name";
}
$hook['includes'][] = "$path/$file";
// Depending on whether theming.theme.inc was included already the
// prepocess function might have been detected automatically.
// Make sure it’s added exactly once.
$hook['preprocess functions'][] = "theming_preprocess_$name";
$hook['preprocess functions'] = array_unique($hook['preprocess functions']);
}
}
/**
* Implements hook_element_info_alter().
*/
function theming_element_info_alter(&$info) {
// Replace the default #pre_render callback for scripts.
$info['scripts']['#pre_render'] = array_map(function ($hook) {
return $hook == 'drupal_pre_render_scripts' ? 'theming_pre_render_scripts' : $hook;
}, $info['scripts']['#pre_render']);
}
/**
* Wrap drupal_pre_render_scripts() to add custom attributes to script tags.
*/
function theming_pre_render_scripts(array $elements) {
$processed_elements = drupal_pre_render_scripts($elements);
foreach (element_children($processed_elements['scripts']) as $index) {
// Skip aggregated scripts.
if (is_string($processed_elements['scripts'][$index])) {
continue;
}
$attributes = &$processed_elements['scripts'][$index]['#attributes'];
if ($src = $attributes['src'] ?? NULL) {
$custom_attributes = $elements['#items'][$src]['attributes'] ?? [];
$attributes = $custom_attributes + $attributes;
}
}
return $processed_elements;
}