This repository was archived by the owner on Nov 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathex_icons.module
More file actions
153 lines (129 loc) · 4.14 KB
/
ex_icons.module
File metadata and controls
153 lines (129 loc) · 4.14 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
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
<?php
/**
* @file
* External-use icons module.
*/
use Drupal\Core\Template\Attribute;
/**
* Implements hook_theme().
*/
function ex_icons_theme() {
return [
'ex_icon' => [
'variables' => [
'id' => '',
'attributes' => [],
],
],
];
}
/**
* Prepares variables for svg icon templates.
*
* Default template: ex-icon.html.twig.
*
* @param array $variables
* An associative array containing:
* - id: The plugin definition ID of the icon to use.
* - attributes: The HTML attributes to apply to the SVG element.
*/
function template_preprocess_ex_icon(array &$variables) {
/** @var \Drupal\ex_icons\ExIconsManager $icons_manager */
$icons_manager = \Drupal::service('ex_icons.manager');
/** @var \Drupal\ex_icons\ExIconInterface $icon */
$icon = $icons_manager->getInstance(['id' => $variables['id'] ?: '']);
// Default role to be img.
if (!isset($variables['attributes']['role'])) {
$variables['attributes']['role'] = 'img';
}
$dimension_keys = &drupal_static(__FUNCTION__, array_flip(['width', 'height']));
$dimension_attributes = array_intersect_key($variables['attributes'], $dimension_keys);
// Calculate other dimension if only one has been specified:
if (count($dimension_attributes) == 1) {
$existing_length = reset($dimension_attributes);
$missing_length_key = key(array_diff_key($dimension_keys, $dimension_attributes));
// Flip aspect ratio value for calculating height.
$aspect_ratio = $missing_length_key == 'height'
? 1 / $icon->getAspectRatio()
: $icon->getAspectRatio();
$variables['attributes'][$missing_length_key] = round($existing_length * $aspect_ratio, 2);
}
$variables['url'] = $icon->getUrl();
\Drupal::service('renderer')->addCacheableDependency($variables, $icons_manager);
}
/**
* Implements hook_preprocess_HOOK() for radios.html.twig.
*/
function ex_icons_preprocess_radios(&$variables) {
if ($variables['element']['#type'] == 'ex_icon_select') {
$variables['attributes']['class'][] = 'icon-selector';
$variables['#attached']['library'][] = 'ex_icons/icon-selector';
}
}
/**
* Implements hook_page_bottom().
*/
function ex_icons_page_bottom(array &$page_bottom) {
$icons_manager = \Drupal::service('ex_icons.manager');
$inline_defs = [];
if ($defs = implode('', $icons_manager->getInlineDefs())) {
$inline_defs = [
'#type' => 'inline_template',
'#template' => '<svg{{ attributes }}><defs>{{ defs|raw }}</defs></svg>',
'#context' => [
'attributes' => new Attribute(['class' => ['visually-hidden']]),
'defs' => $defs,
],
];
}
\Drupal::service('renderer')->addCacheableDependency($inline_defs, $icons_manager);
$page_bottom['ex_icons_inline_defs'] = $inline_defs;
}
/**
* Implements hook_page_attachments().
*/
function ex_icons_page_attachments(array &$attachments) {
$icons_manager = \Drupal::service('ex_icons.manager');
$paths = [];
foreach (array_keys($icons_manager->getDefinitions()) as $id) {
// Skip the fall-back null instance.
if ($id == 'ex_icon_null') {
continue;
}
$instance = $icons_manager->getInstance(['id' => $id]);
$provider = $instance->getProvider();
// Already got a URL for this provider.
if (isset($paths[$provider])) {
continue;
}
// Use first discovered icon's URL for each provider, removing it's hash
// portion to be more generic.
$paths[$provider] = strstr($instance->getUrl(), '#', TRUE);
}
$attachments['#attached']['drupalSettings']['exIcons']['paths'] = $paths;
}
/**
* Implements hook_ex_icons_alter().
*/
function ex_icons_ex_icons_alter(array &$definitions) {
$definitions['ex_icon_null'] = [
'id' => 'ex_icon_null',
'width' => 1,
'height' => 1,
'url' => '',
'provider' => 'ex_icons',
'class' => 'Drupal\\ex_icons\\ExIcon',
];
}
/**
* Implements hook_themes_installed().
*/
function ex_icons_themes_installed($theme_list) {
\Drupal::service('ex_icons.manager')->clearCachedDefinitions();
}
/**
* Implements hook_themes_uninstalled().
*/
function ex_icons_themes_uninstalled($theme_list) {
\Drupal::service('ex_icons.manager')->clearCachedDefinitions();
}