-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfontyourface.module
443 lines (410 loc) · 13.2 KB
/
fontyourface.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
<?php
/**
* @file
* Contains fontyourface.module..
*/
use Drupal\Core\Url;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Render\Element;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Component\Utility\Html;
use Drupal\taxonomy\Entity\Term;
use Drupal\fontyourface\FontInterface;
use Drupal\fontyourface\FontDisplayInterface;
use Drupal\fontyourface\Entity\Font;
use Drupal\fontyourface\Entity\FontDisplay;
use Drupal\Core\Link;
/**
* Implements hook_module_implements_alter().
*/
function fontyourface_module_implements_alter(&$implementations, $hook) {
if ($hook == 'page_attachments') {
// Move the fontyourface and submodules to be among the last to build on page.
$list = ['fontyourface'];
foreach (\Drupal::moduleHandler()->getImplementations('fontyourface_api') as $module_name) {
if (isset($implementations[$module_name])) {
$list[] = $module_name;
}
}
foreach ($list as $module_name) {
$group = $implementations[$module_name];
unset($implementations[$module_name]);
$implementations[$module_name] = $group;
}
}
}
/**
* Implements hook_help().
*/
function fontyourface_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
// Main module help for the fontyourface module.
case 'help.page.fontyourface':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('Web font management tools.') . '</p>';
return $output;
default:
}
}
/**
* Implements hook_library_info_build().
*/
function fontyourface_library_info_build() {
$libraries = [];
$drupal_themes = \Drupal::service('theme_handler')->listInfo();
foreach ($drupal_themes as $key => $theme) {
$styles = FontDisplay::loadByTheme($key);
if (!empty($styles)) {
foreach ($styles as $style) {
$file_url = file_build_uri('fontyourface/font_display/' . $style->id() . '.css');
$libraries['font_display_' . $key]['css']['base'][$file_url] = [
'basename' => $file_url,
];
}
}
}
return $libraries;
}
/**
* Implements hook_theme().
*/
function fontyourface_theme() {
return [
'font' => [
'render element' => 'elements',
],
'font__full' => [
'render element' => 'elements',
],
'font__teaser' => [
'render element' => 'elements',
],
];
}
/**
* Implements hook_page_attachments().
*/
function fontyourface_page_attachments(&$page) {
$config = \Drupal::config('fontyourface.settings');
$fonts = &drupal_static('fontyourface_fonts', []);
// Load all enabled fonts for theme if setting allows it.
if ($config->get('load_all_enabled_fonts')) {
if (empty($config->get('load_on_themes')) || in_array(\Drupal::theme()->getActiveTheme()->getName(), $config->get('load_on_themes'))) {
foreach (Font::loadActivatedFonts() as $font) {
$fonts[$font->url->value] = $font;
}
}
}
$font_preview = &drupal_static('fontyourface_font_preview');
// On a font preview page, ONLY show the preview font and other base theme font.
if (empty($font_preview)) {
// We are dealing with a regular page.
$theme = \Drupal::theme()->getActiveTheme()->getName();
$styles = FontDisplay::loadByTheme($theme);
if (!empty($styles)) {
$page['#attached']['library'][] = 'fontyourface/font_display_' . $theme;
foreach ($styles as $style) {
$font = $style->getFont();
if (!empty($font)) {
$fonts[$font->url->value] = $font;
}
}
}
}
}
/**
* Implements hook_preprocess_HOOK().
*
* Adds body classes if certain regions have content.
*/
function fontyourface_preprocess_html(&$variables) {
$variables['attributes']['class'][] = 'fontyourface';
}
/**
* Implements hook_theme_suggestions_HOOK().
*/
function fontyourface_theme_suggestions_font(array $variables) {
return ['font__' . $variables['elements']['#view_mode']];
}
/**
* Implements hook_views_pre_render().
*/
function fontyourface_views_pre_render($view) {
if ($view->id() == 'fontyourface_font_manager') {
$view->element['#attached']['library'][] = 'fontyourface/fontyourface.admin';
}
}
/**
* Prepares variables for Font templates.
*
* Default template: font.html.twig.
*
* @param array $variables
* An associative array containing:
* - elements: An associative array containing the user information and any
* - attributes: HTML attributes for the containing element.
*/
function fontyourface_preprocess_font(array &$variables) {
// Fetch Font Entity Object.
$font = $variables['elements']['#font'];
$fonts = &drupal_static('fontyourface_fonts');
$fonts[$font->url->value] = $font;
$font_preview = &drupal_static('fontyourface_font_preview');
$font_preview = TRUE;
$title = Html::escape($font->name->value);
$variables['font_style'] = fontyourface_font_css($font, NULL, "\n");
$variables['font_style_inline'] = fontyourface_font_css($font);
$variables['font_title'] = Link::fromTextAndUrl($title, $font->toUrl());
// Helpful $content variable for templates.
foreach (Element::children($variables['elements']) as $key) {
$variables['content'][$key] = $variables['elements'][$key];
}
$variables['attributes']['class'] = ['font'];
if ($font->isDeactivated()) {
$url = Url::fromRoute('entity.font.activate', ['js' => 'nojs', 'font' => $font->id()], ['query' => \Drupal::destination()->getAsArray()]);
$url->setOptions(
[
'attributes' => [
'id' => 'font-status-' . $font->id(),
'class' => [
'font-status',
'disabled',
'use-ajax',
],
],
]
);
$text = t('Enable');
$variables['attributes']['class'][] = 'disabled';
}
if ($font->isActivated()) {
$url = Url::fromRoute('entity.font.deactivate', ['js' => 'nojs', 'font' => $font->id()], ['query' => \Drupal::destination()->getAsArray()]);
$url->setOptions(
[
'attributes' => [
'id' => 'font-status-' . $font->id(),
'class' => [
'font-status',
'enabled',
'use-ajax',
],
],
]
);
$text = t('Disable');
$variables['attributes']['class'][] = 'enabled';
}
$url->setOption('destination', Drupal::destination());
$variables['operation_links'] = Link::fromTextAndUrl($text, $url);
}
/**
* Prepares variables for Font templates.
*
* Default template: font--full.html.twig.
*
* @param array $variables
* An associative array containing:
* - elements: An associative array containing the user information and any
* - attributes: HTML attributes for the containing element.
*/
function fontyourface_preprocess_font__full(array &$variables) {
fontyourface_preprocess_font($variables);
}
/**
* Prepares variables for font teaser preview.
*
* Default template: font--teaser.html.twig.
*
* @param array $variables
* An associative array containing:
* - elements: An associative array containing the user information and any
* - attributes: HTML attributes for the containing element.
*/
function fontyourface_preprocess_font__teaser(array &$variables) {
fontyourface_preprocess_font($variables);
}
/**
* Saves/updates font.
*
* @param object $font_data
* Simple stdclass rep of font for saving.
*
* @return Drupal\fontyourface\Entity\Font
* Newly saved/updated font.
*/
function fontyourface_save_font($font_data) {
$font = Font::loadByUrl($font_data->url);
if (empty($font)) {
$font = Font::create();
}
$font->pid = $font_data->provider;
$font->url = $font_data->url;
$font->name = $font_data->name;
$font->css_family = !empty($font_data->css_family) ? $font_data->css_family : '';
$font->css_style = !empty($font_data->css_style) ? $font_data->css_style : '';
$font->css_weight = !empty($font_data->css_weight) ? $font_data->css_weight : '';
$font->foundry = !empty($font_data->foundry) ? $font_data->foundry : '';
$font->foundry_url = !empty($font_data->foundry_url) ? $font_data->foundry_url : '';
$font->license = !empty($font_data->license) ? $font_data->license : '';
$font->license_url = !empty($font_data->license_url) ? $font_data->license_url : '';
$font->designer = !empty($font_data->designer) ? $font_data->designer : '';
$font->designer_url = !empty($font_data->designer_url) ? $font_data->designer_url : '';
if (!empty($font_data->classification)) {
$font->field_classification = [];
foreach ($font_data->classification as $classification) {
$font->field_classification[] = [
'target_id' => _fontyourface_get_vocabulary_term($classification, 'font_classification'),
];
}
}
if (!empty($font_data->language)) {
$font->field_supported_languages = [];
foreach ($font_data->language as $language) {
$font->field_supported_languages[] = [
'target_id' => _fontyourface_get_vocabulary_term($language, 'languages_supported'),
];
}
}
$font->tags = [];
if (!empty($font_data->foundry)) {
$font->field_tags[] = [
'target_id' => _fontyourface_get_vocabulary_term($font_data->foundry, 'font_foundry'),
];
}
if (!empty($font_data->designer)) {
$font->field_tags[] = [
'target_id' => _fontyourface_get_vocabulary_term($font_data->designer, 'font_designer'),
];
}
$font->setMetadata(!empty($font_data->metadata) ? $font_data->metadata : []);
$font->status = FALSE;
if ($font->isActivated()) {
$font->status = TRUE;
}
$font->save();
return $font;
}
/**
* Creates CSS with any properties set on font.
*
* @param Drupal\fontyourface\FontInterface $font
* The font object.
* @param Drupal\fontyourface\FontDisplayInterface $font_style
* The font display object.
* @param string $separator
* Approach to separating the resulting css.
*
* @return string
* The font-family css.
*/
function fontyourface_font_css(FontInterface $font, FontDisplayInterface $font_style = NULL, $separator = ' ') {
$css = \Drupal::moduleHandler()->invokeAll('fontyourface_font_css', [
$font,
$font_style,
$separator,
]);
if (!empty($css)) {
return implode("\n", $css);
}
$css = [];
// Enclose font family definition in single quotes if not already enclosed.
if ($font->css_family->value[0] === "'") {
$family_list = $font->css_family->value;
}
else {
$family_list = "'" . $font->css_family->value . "'";
}
if ($font_style !== NULL) {
if (!empty($font_style->getFallback())) {
$family_list .= ', ' . $font_style->getFallback();
}
}
$css[] = 'font-family: ' . $family_list . ';';
$css[] = 'font-style: ' . $font->css_style->value . ';';
$css[] = 'font-weight: ' . $font->css_weight->value . ';';
return implode($separator, $css);
}
/**
* Deletes fonts from a specific provider.
*
* @param string $provider
* The providing submodule.
*/
function fontyourface_delete($provider) {
// Delete fonts, 50 at a time.
@set_time_limit(3600);
while (TRUE) {
$storage_handler = \Drupal::entityTypeManager()->getStorage('font');
$fids = \Drupal::entityQuery('font')
->condition('pid', $provider)
->range(0, 50)
->execute();
if (!empty($fids)) {
$fonts = $storage_handler->loadMultiple(array_keys($fids));
$storage_handler->delete($fonts);
}
else {
break;
}
}
}
/**
* Logs to watchdog if logging is enabled.
*
* @param string $message
* Log message. This should be a literal string; see
* https://drupal.org/node/323101 for more details.
* @param array $arguments
* Arguments to replace placeholders, if there are any, in $message.
*/
function fontyourface_log($message, array $arguments) {
$config = \Drupal::config('fontyourface.settings');
if ($config->get('fontyourface_detailed_logging')) {
watchdog('@font-your-face', $message, $arguments, WATCHDOG_INFO);
}
}
/**
* Saves and generates font file based on font display config entity data.
*
* @param Drupal\fontyourface\FontDisplayInterface $style
* Custom config font display entity.
*
* @return bool
* TRUE if files save successfully. Throw any errors otherwise.
*/
function fontyourface_save_and_generate_font_display_css(FontDisplayInterface $style) {
/** @var \Drupal\Core\File\FileSystemInterface $file_system */
$file_system = \Drupal::service('file_system');
$directory = file_build_uri('fontyourface/font_display');
$file_system->prepareDirectory($directory, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS);
$font = $style->getFont();
$css_file = $directory . '/' . $style->id() . '.css';
$css_file_data = $style->getSelectors() . ' { ' . fontyourface_font_css($font, $style) . ' }';
$file_system->saveData($css_file_data, $css_file, FileSystemInterface::EXISTS_REPLACE);
return TRUE;
}
/**
* Retrieves a tid for use from taxonomy.
*
* @param string $term_value
* The taxonomy term string.
* @param string $vocabulary
* The machine name of the vocabulary the term belongs to.
*
* @return int
* The found/created taxonomy term id.
*/
function _fontyourface_get_vocabulary_term($term_value, $vocabulary) {
if ($terms = taxonomy_term_load_multiple_by_name($term_value, $vocabulary)) {
$term = reset($terms);
}
else {
$term = Term::create([
'name' => $term_value,
'vid' => $vocabulary,
]);
$term->save();
}
return $term->id();
}