forked from kalamuna/kalastatic_dot_module
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kalastatic.module
269 lines (242 loc) · 7.8 KB
/
kalastatic.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
<?php
/**
* @file
* Contains kalastatic.module.
*/
use Symfony\Component\Finder\Finder;
use Symfony\Component\Yaml\Parser;
use Symfony\Component\Filesystem\Filesystem;
use Drupal\Core\Url;
/**
* Implements hook_preprocess().
*/
function kalastatic_preprocess(&$vars) {
// Add a global flag to indicate it is rendering in a Drupal context. This is
// needed for our templates to be shared between Drupal and Kalastatic.
$vars['drupal'] = TRUE;
}
/**
* Implements hook_library_info_build().
*/
function kalastatic_library_info_build() {
global $base_path;
$settings = kalastatic_get_settings('yaml');
$config = kalastatic_get_settings('config');
$build_path = $base_path . $settings['destination'];
// Some library metadata.
$libraries = [];
$libraries['css-js'] = [
'dependencies' => [
'core/jquery',
],
'license' => [
'name' => 'MIT',
'url' => 'https://opensource.org/licenses/MIT',
'gpl-compatible' => TRUE,
],
];
// Build and array of css file paths.
$css = [];
foreach ($config['stylesheets'] as $css_path) {
$css[$build_path . '/' . $css_path] = [];
}
// Build and array of js file paths.
$js = [];
foreach ($config['scripts']['footer']['all'] as $js_path) {
$js[$build_path . '/' . $js_path] = [];
}
// Add the css and js to the library.
$libraries['css-js'] += [
'css' => [
'theme' => $css,
],
'js' => $js,
];
return $libraries;
}
/**
* Implements hook_page_attachments().
*/
function kalastatic_page_attachments(array &$attachments) {
// Get the module settings and the active theme.
$module_settings = \Drupal::config('kalastatic.settings');
$ks_themes = $module_settings->get('kalastatic_theme_list');
$active_theme = \Drupal::service('theme.manager')->getActiveTheme()->getName();
if ($ks_themes == NULL) {
// Set a message and bail out if no themes have been chosen.
$settings_url = Url::fromRoute('kalastatic.settings');
drupal_set_message(t('Kalastatic requires you to choose the theme(s) you would like it to work with. Go to the <a href="@link">settings page.</a>', array('@link' => $settings_url->toString())), 'error');
return;
}
// Check if the active theme has been chosen.
if (in_array($active_theme, $ks_themes) && $ks_themes[$active_theme]) {
// Attach library to this page.
$attachments['#attached']['library'][] = 'kalastatic/css-js';
}
}
/**
* Implements hook_rebuild().
*/
function kalastatic_rebuild() {
// Warm the settings cache.
kalastatic_get_settings();
}
/**
* Return the default path to Kalastatic.
*
* The assumption is that it is inside a folder called 'kalastatic' in the
* current default theme. This assumption is based on convention.
*/
function kalastatic_path_to_src_default() {
$default_theme = \Drupal::config('system.theme')->get('default');
return drupal_get_path('theme', $default_theme) . '/kalastatic';
}
/**
* Return the path to the default build folder.
*
* This assumption is based on what Kalastatic does if no build path is set in
* kalastatic.yaml file.
*/
function kalastatic_path_to_build_default() {
return kalastatic_path_to_src_default() . '/build';
}
/**
* Get Kalastatic settings from cache.
*
* Alternatively, if it doesn't exist then hit kalastatic.yaml file.
*
* @param string $type
* Which cache file to get.
*
* @return array
* An array of info from Kalastatic from the file requested.
*/
function kalastatic_get_settings(string $type = '') {
$cid = 'kalastatic_yaml';
$data = NULL;
if ($cache = \Drupal::cache('discovery')->get($cid)) {
// We have cache, so use it.
$data = $cache->data;
}
else {
// We have no cache so hit the file system.
$yaml = kalastatic_get_kalastatic_yaml();
$data['yaml'] = kalastatic_adjust_path_root($yaml);
$data['config'] = kalastatic_get_kalastatic_config($data['yaml']);
\Drupal::cache('discovery')->set($cid, $data);
}
return $type ? $data[$type] : $data;
}
/**
* Get Kalastatic settings from kalastatic.yaml file.
*/
function kalastatic_get_kalastatic_config($settings = []) {
// Find Kalastatic's config.metadata file.
$settings = empty($settings) ? kalastatic_get_settings() : $settings;
$source_dir = $settings['source'];
$fs = new Filesystem();
$config = [];
if ($fs->exists($settings['source'])) {
$finder = new Finder();
$iterator = $finder
->name('config.metadata')
->in($source_dir);
// This file is markdown with yaml front matter so we need to massage it a bit
// to parse the yaml.
foreach ($iterator as $file_path => $file) {
$yaml = new Parser();
$file_contents = file_get_contents($file_path);
$yaml_only = str_replace('---', '', $file_contents);
$config = $yaml->parse($yaml_only);
}
}
else {
// Log an error because the source directory doesn't exist.
\Drupal::logger('kalastatic')->error('Cannot find Kalastatic source directory. Looking in @dir', ['@dir' => $source_dir]);
}
return $config;
}
/**
* Get Kalastatic settings from kalastatic.yaml file.
*/
function kalastatic_get_kalastatic_yaml() {
// Find a kalastatic.yaml file. We start by looking 1 level higher than Drupal
// root to handle Composer builds.
$finder = new Finder();
$iterator = $finder
->name('kalastatic.yaml')
->in('..');
// Hopefully there's only one.
$settings = [];
foreach ($iterator as $file_path => $file) {
$yaml = new Parser();
$settings = $yaml->parse(file_get_contents($file_path));
}
// Set default paths if kalastatic.yaml doesn't have them set.
foreach ($settings as $setting => $path) {
if (!isset($settings['source'])) {
$settings[$setting] = kalastatic_path_to_src_default();
}
if (!isset($settings['destination'])) {
$settings[$setting] = kalastatic_path_to_build_default();
}
}
return $settings;
}
/**
* Mangle kalastatic.yaml paths for Drupal.
*
* If we are dealing with a composer project where Drupal is nested one
* folder deep in the project the paths defined in kalastatic.yaml will have 1
* more arg than Drupal knows about so we need to remove it.
*/
function kalastatic_adjust_path_root($settings) {
// Deal with the source and destination.
$settings['source'] = kalastatic_drupal_root_path($settings['source']);
$settings['destination'] = kalastatic_drupal_root_path($settings['destination']);
// All the Twig namespaces need their paths mangled too.
$namespaces = &$settings['pluginOpts']['metalsmith-jstransformer']['engineOptions']['twig']['namespaces'];
foreach ($namespaces as &$path) {
$path = kalastatic_drupal_root_path($path);
}
return $settings;
}
/**
* Mangle a path for Drupal.
*
* If we are dealing with a composer project where Drupal is nested one
* folder deep in the project the paths defined in kalastatic.yaml will have 1
* more arg than Drupal knows about so we need to remove it.
*/
function kalastatic_drupal_root_path($path) {
$root_args = explode('/', DRUPAL_ROOT);
$drupal_parent = end($root_args);
$path_args = explode('/', $path);
$prefix = reset($path_args);
$new_path = $path;
if ($prefix == $drupal_parent) {
// We add 1 to get rid of a preceding slash.
$length = strlen($prefix) + 1;
$new_path = substr($path, $length);
}
return $new_path;
}
/**
* Pull the Twig name spaces from kalastatic.yaml file.
*/
function kalastatic_get_namespaces() {
$settings = kalastatic_get_settings('yaml');
return $settings['pluginOpts']['metalsmith-jstransformer']['engineOptions']['twig']['namespaces'];
}
/**
* Implements hook_page_attachments_alter().
*/
function kalastatic_page_attachments_alter(array &$page) {
// Unset Drupals default favicon.
$links = $page['#attached']['html_head_link'];
foreach ($links as $key => $link) {
if ($link[0]['href'] == '/core/misc/favicon.ico') {
unset($page['#attached']['html_head_link'][$key]);
}
}
}