theme.inc
2.49 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
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
<?php
/**
* @file
* Custom theme hooks.
*/
declare(strict_types = 1);
/**
* Implements hook_library_info_build().
*
* Automatically creates components libraries (Not visible in theme's
* libraries.yml file). You can directly use the component name as a library,
* e.g.: {{ attach_library('droopler_subtheme/block') }}.
*/
function droopler_subtheme_library_info_build() {
$theme_handler = \Drupal::service('theme_handler');
$theme_path = $theme_handler->getTheme('droopler_subtheme')->getPath();
$directory = $theme_path . '/build/components';
if (!is_dir($directory)) {
return [];
}
$extensions = ['css', 'js'];
$extensions = array_map('preg_quote', $extensions);
$extensions = implode('|', $extensions);
$file_scan = \Drupal::service('file_system')->scanDirectory($directory, "/{$extensions}$/", ['nomask' => '/extends/']);
$libraries = [];
foreach ($file_scan as $file) {
$parts = explode('.', $file->filename);
$extension = end($parts);
switch ($extension) {
case 'css':
$libraries[$file->name][$extension] = [
'component' => [
'/' . $file->uri => [],
],
];
break;
case 'js':
$libraries[$file->name][$extension] = [
'/' . $file->uri => [],
];
break;
}
}
return $libraries;
}
/**
* Implements hook_library_info_alter().
*
* Automatically extends existing components libraries if the file with the
* same name as the library exists in the build/components/extends directory.
*/
function droopler_subtheme_library_info_alter(&$libraries, $extension) {
$theme_handler = \Drupal::service('theme_handler');
$theme_path = $theme_handler->getTheme('droopler_subtheme')->getPath();
$directory = $theme_path . '/build/components/extends';
if (!is_dir($directory)) {
return [];
}
$extensions = ['css', 'js'];
$extensions = array_map('preg_quote', $extensions);
$extensions = implode('|', $extensions);
$file_scan = \Drupal::service('file_system')->scanDirectory($directory, "/{$extensions}$/");
foreach ($file_scan as $file) {
$file_name = $file->name;
$file_parts = explode('.', $file->filename);
$extension = end($file_parts);
if (isset($libraries[$file_name])) {
switch ($extension) {
case 'css':
$libraries[$file_name][$extension]['component']['/' . $file->uri] = [];
break;
case 'js':
$libraries[$file_name][$extension]['/' . $file->uri] = [];
break;
}
}
}
}