-
Notifications
You must be signed in to change notification settings - Fork 1
/
burda_cmp.module
191 lines (167 loc) · 5.81 KB
/
burda_cmp.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
<?php
/**
* @file
* The consent management platform implementation via LiveRamp.
*/
use Drupal\Core\Cache\CacheableMetadata;
/**
* Implements hook_module_implements_alter().
*/
function burda_cmp_module_implements_alter(&$implementations, $hook) {
if ($hook == 'library_info_alter') {
// Move this hook implementation to the end of the list.
// \Drupal::moduleHandler()->getImplementations()
// iterates through $implementations with a foreach loop which PHP iterates
// in the order that the items were added, so to move an item to the end of
// the array, we remove it and then add it.
$group = $implementations['burda_cmp'];
unset($implementations['burda_cmp']);
$implementations['burda_cmp'] = $group;
}
}
/**
* Implements hook_library_info_alter().
*/
function burda_cmp_library_info_alter(&$libraries, $extension) {
/** @var \Drupal\burda_cmp\StaticConsentDataInterface $static_consent_data */
$static_consent_data = \Drupal::service('burda_cmp.static_consent_data');
// Process all registered JavaScript files that need to be only loaded when a
// consent is given. Therefore information about these files are added to
// 'burda_cmp' module's JavaScript settings and loaded dynamically based on
// the consent status.
foreach ($libraries as $name => &$library) {
if (!empty($library['js'])) {
foreach ($library['js'] as $js => &$definition) {
if (isset($definition['burda_cmp'])) {
$parsed = _burda_cmp_parse_js_definition($js, $extension, $definition);
// Ensure numeric vendor ID.
if (!empty($definition['burda_cmp']['vendor'])) {
if (!is_numeric($definition['burda_cmp']['vendor'])) {
$definition['burda_cmp']['vendor'] = $static_consent_data->getVendorId($definition['burda_cmp']['vendor']);
}
}
// Determine purposes by vendor (if empty).
if (!empty($definition['burda_cmp']['vendor'])) {
if (empty($definition['burda_cmp']['purposes'])) {
$definition['burda_cmp']['purposes'] = $static_consent_data->getPurposeIds($definition['burda_cmp']['vendor']);
}
}
$script = file_url_transform_relative(file_create_url($parsed['data']));
$library['drupalSettings']['burdaCmp'][$extension][$script] = $definition['burda_cmp'];
// Remove definition as it is loaded on demand now.
unset($library['js'][$js]);
}
}
}
}
}
/**
* Implements hook_page_attachments().
*/
function burda_cmp_page_attachments(array &$page) {
/** @var \Drupal\Core\Config\ImmutableConfig $config */
$config = \Drupal::config('burda_cmp.settings');
$liveramp_script_url = $config->get('liveramp_script_url');
CacheableMetadata::createFromRenderArray($page)
->addCacheableDependency($config)
->applyTo($page);
// LiveRamp init script.
if ($liveramp_script_url) {
$page['#attached']['html_head'][] = [
[
'#tag' => 'script',
'#attributes' => [
'type' => 'text/javascript',
'src' => $liveramp_script_url,
],
'#weight' => -100,
],
'burda_cmp_init',
];
}
}
/**
* Implements hook_theme().
*/
function burda_cmp_theme($existing, $type, $theme, $path) {
$items = [];
// Conditional content (only displayed when specific consent is given).
$items['burda_cmp_conditional_content'] = [
'variables' => [
'content' => NULL,
'message_preface' => NULL,
'message_postscript' => NULL,
'purposes' => [],
'toggle_label' => NULL,
'url_privacy_policy' => NULL,
'vendor' => NULL,
'vendor_label' => NULL,
],
'file' => 'burda_cmp.theme.inc',
];
return $items;
}
/**
* Parse a library's JavaScript definition.
*
* This is a verbatim copy of LibraryDiscoveryParser::buildByExtension() to have
* the same logic for when the $options['data'] array item is set.
*
* @param string $source
* The script file path/URI.
* @param string $extension
* The name of the extension that registered a library.
* @param array $options
* The definition options for the JavaScript file.
*
* @see \Drupal\Core\Asset\LibraryDiscoveryParser::buildByExtension()
*/
function _burda_cmp_parse_js_definition($source, $extension, array $options) {
if ($extension === 'core') {
$path = 'core';
}
else {
if (\Drupal::moduleHandler()->moduleExists($extension)) {
$extension_type = 'module';
}
else {
$extension_type = 'theme';
}
$path = drupal_get_path($extension_type, $extension);
}
if (!empty($options['type']) && $options['type'] === 'external') {
$options['data'] = $source;
}
// Determine the file asset URI.
else {
/** @var \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface $stream_wrapper_manager */
$stream_wrapper_manager = \Drupal::service('stream_wrapper_manager');
if ($source[0] === '/') {
// An absolute path maps to DRUPAL_ROOT / base_path().
if ($source[1] !== '/') {
$options['data'] = substr($source, 1);
}
// A protocol-free URI (e.g., //cdn.com/example.js) is external.
else {
$options['type'] = 'external';
$options['data'] = $source;
}
}
// A stream wrapper URI (e.g., public://generated_js/example.js).
elseif ($stream_wrapper_manager->isValidUri($source)) {
$options['data'] = $source;
}
// A regular URI (e.g., http://example.com/example.js) without
// 'external' explicitly specified, which may happen if, e.g.
// libraries-override is used.
elseif (count(explode('://', $source)) === 2) {
$options['type'] = 'external';
$options['data'] = $source;
}
// By default, file paths are relative to the registering extension.
else {
$options['data'] = $path . '/' . $source;
}
}
return $options;
}