-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjs_component.module
More file actions
64 lines (55 loc) · 1.62 KB
/
js_component.module
File metadata and controls
64 lines (55 loc) · 1.62 KB
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
<?php
use Drupal\js_component\JSComponentManager;
use Drupal\js_component\Plugin\JSComponent;
/**
* Implements hook_library_info_build().
*/
function js_component_library_info_build() {
/** @var JSComponentManager $manager */
$manager = \Drupal::service('plugin.manager.js_component');
$libraries = [];
/** @var JSComponent $instance */
foreach ($manager->getDefinitionInstances() as $plugin_id => $instance) {
if (!$instance->hasLibraries()) {
continue;
}
$key = "{$instance->provider()}.{$plugin_id}";
$libraries[$key] = $instance->processLibraries();
}
return $libraries;
}
/**
* Implements hook_theme().
*/
function js_component_theme($existing, $type, $theme, $path) {
/** @var JSComponentManager $manager */
$manager = \Drupal::service('plugin.manager.js_component');
$theme_info = [];
/** @var JSComponent $instance */
foreach ($manager->getDefinitionInstances() as $plugin_id => $instance) {
if (!$instance->hasTemplate()) {
continue;
}
$theme_info[$instance->componentId()] = [
'render element' => 'element',
'path' => $instance->getTemplatePath(),
'template' => $instance->getTemplateName(),
'preprocess functions' => [
'template_preprocess_js_component'
]
];
}
return $theme_info;
}
/**
* Define template preprocess for JS component.
*
* @param $variables
* An array of theme variables.
*/
function template_preprocess_js_component(&$variables) {
$element = $variables['element'];
if (isset($element['#settings']) && !empty($element['#settings'])) {
$variables['settings'] = $element['#settings'];
}
}