-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelfi_proxy.module
139 lines (119 loc) · 3.85 KB
/
helfi_proxy.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
<?php
/**
* @file
* Contains helfi_proxy.
*/
use Drupal\Core\Asset\AttachedAssetsInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\helfi_proxy\ProxyManagerInterface;
/**
* Implements hook_module_implements_alter().
*/
function helfi_proxy_module_implements_alter(&$implementations, $hook) : void {
// Move helfi_proxy_file_url_alter() implementation to the top of the
// list, so this is always run first before any other alter hooks, more
// specifically before 'crop_file_url_alter()' which seems to convert certain
// responsive image styles to an external URL that will break our
// implementation. @see UHF-7946.
if ($hook === 'file_url_alter') {
$group = $implementations['helfi_proxy'];
$implementations = ['helfi_proxy' => $group] + $implementations;
}
}
/**
* Implements hook_file_url_alter().
*/
function helfi_proxy_file_url_alter(&$uri) : void {
/** @var \Drupal\helfi_proxy\ProxyManagerInterface $service */
$service = \Drupal::service('helfi_proxy.proxy_manager');
$uri = $service->processPath($uri);
}
/**
* Implements hook_js_settings_alter().
*/
function helfi_proxy_js_settings_alter(
array &$settings,
AttachedAssetsInterface $assets,
) {
if (isset($settings['radioactivity'])) {
/** @var \Drupal\helfi_proxy\ActiveSitePrefix $service */
$service = \Drupal::service('helfi_proxy.active_prefix');
if (!$prefix = $service->getPrefix()) {
// Fallback to /en if site has no prefixes configured, like Etusivu
// for example.
$prefix = '/en';
}
$settings['radioactivity']['endpoint'] = sprintf('%s/radioactivity/emit', $prefix);
}
}
/**
* Implements hook_page_attachments_alter().
*/
function helfi_proxy_page_attachments_alter(array &$attachments) {
// Set custom content type metatag on all content pages.
if (empty($attachments['#attached']['html_head'])) {
return;
}
$entities = array_filter(
\Drupal::routeMatch()->getParameters()->all(),
function ($param) {
return $param instanceof EntityInterface;
}
);
$entity = !empty($entities) ? reset($entities) : NULL;
if ($entity) {
$tags = [
'helfi_content_type' => $entity->bundle(),
'helfi_content_id' => $entity->id(),
];
foreach ($tags as $tag_name => $content) {
$tag = [
'#tag' => 'meta',
'#attributes' => [
'name' => $tag_name,
'content' => $content,
'class' => 'elastic',
],
];
$attachments['#attached']['html_head'][] = [$tag, $tag_name];
}
}
}
/**
* Implements hook_simple_sitemap_links_alter().
*/
function helfi_proxy_simple_sitemap_links_alter(array &$links, $sitemap_variant) {
/** @var \Drupal\Core\Config\ImmutableConfig $config */
$config = \Drupal::service('config.factory')->get('helfi_proxy.settings');
if (!$paths = implode("\n", $config->get(ProxyManagerInterface::ROBOTS_PATHS) ?? [])) {
return;
}
try {
/** @var \Drupal\helfi_api_base\Environment\Environment $environment */
$environment = \Drupal::service('helfi_api_base.environment_resolver')->getActiveEnvironment();
}
catch (\InvalidArgumentException) {
return;
}
/** @var \Drupal\Core\Path\PathMatcherInterface $pathMatcher */
$pathMatcher = \Drupal::service('path.matcher');
// helfi_proxy module sets "X-Robots-Tag: noindex" header for configured
// paths. These url should not be included in the sitemap.xml file.
foreach ($links as $key => $link) {
try {
$baseUrl = $environment->getUrl($link['langcode']);
}
catch (\InvalidArgumentException) {
// Base url not found for given langcode.
continue;
}
$url = $link['url'];
if (str_starts_with($url, $baseUrl)) {
$path = substr($url, strlen($baseUrl));
// Remove matched paths from sitemap.xml file.
if ($pathMatcher->matchPath($path, $paths)) {
unset($links[$key]);
}
}
}
}