-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomponent.module
81 lines (73 loc) · 2.18 KB
/
component.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
<?php
/**
* @file
* Component Module.
*/
/**
* Implements hook_theme() for Component templates.
*/
function component_theme() {
return [
'component_html' => [
'render element' => 'elements',
'variables' => [
'html_template' => NULL,
'content_attributes' => NULL,
],
],
];
}
/**
* Implements hook_library_info_build().
*
* Create a Drupal library entry for each component. This is what the
* component uses to load the css/js.
* Eg: 'component/example_tabs'
*/
function component_library_info_build() {
$libraries = [];
// Get a list of all components.
$discovery = \Drupal::service('component.discovery');
$component_list = $discovery->getComponents();
// Build the library registry for each component. We have to loop
// twice because they are keyed to the type of component (block, library)
// as well as the component machine/name.
foreach ($component_list as $components) {
foreach ($components as $name => $component) {
// Build the library array from the config.
if ($component['js']) {
$libraries[$name]['js'] = _component_build_library($component['js'], $component['subpath']);
}
if ($component['css']) {
$libraries[$name]['css']['component'] = _component_build_library($component['css'], $component['subpath']);
}
if ($component['dependencies']) {
$libraries[$name]['dependencies'] = $component['dependencies'];
}
}
}
return $libraries;
}
/**
* Helper function to process and build library assets.
*
* @param array $assets
* The asset array for CSS or JS.
* @param string $subpath
* The path to the component parent folder, relative to root. This is
* epected to have a forward slash at the beginning and end already.
*/
function _component_build_library(array $assets, $subpath) {
$processed = [];
foreach ($assets as $asset_file => $asset_data) {
// Allow external assets to use absolute path.
if (!empty($asset_data['type']) && $asset_data['type'] == 'external') {
$asset_path = $asset_file;
}
else {
$asset_path = $subpath . $asset_file;
}
$processed[$asset_path] = $asset_data;
}
return $processed;
}