-
Notifications
You must be signed in to change notification settings - Fork 1
/
real_favicon.module
81 lines (74 loc) · 2.34 KB
/
real_favicon.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
<?php
/**
* @file
* Contains real_favicon.module..
*/
use Drupal\Core\Routing\RouteMatchInterface;
/**
* Implements hook_help().
*/
function real_favicon_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
// Main module help for the real_favicon module.
case 'help.page.real_favicon':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('Add responsive favicons to your site based on the code from http://realfavicongenerator.neet') . '</p>';
return $output;
default:
}
}
/**
* Implements hook_page_attachments_alter().
*/
function real_favicon_page_attachments_alter(array &$attachments) {
$theme = \Drupal::theme()->getActiveTheme()->getName();
$realFaviconManager = \Drupal::service('real_favicon.manager');
if ($tags = $realFaviconManager->getTags($theme)) {
// Remove default favicon from html_head_link.
if (!empty($attachments['#attached']['html_head_link'])) {
foreach ($attachments['#attached']['html_head_link'] as $i => $item) {
if (!empty($item) && is_array($item)) {
foreach ($item as $ii => $iitem) {
if (isset($iitem['rel']) && in_array($iitem['rel'], ['shortcut icon', 'icon'])) {
unset($attachments['#attached']['html_head_link'][$i][$ii]);
}
}
if (empty($attachments['#attached']['html_head_link'][$i])) {
unset($attachments['#attached']['html_head_link'][$i]);
}
}
}
if (empty($attachments['#attached']['html_head_link'])) {
unset($attachments['#attached']['html_head_link']);
}
}
// Attach favicon tags.
$attachments['#attached']['html_head'][] = [
[
'#type' => 'markup',
'#markup' => $tags,
'#allowed_tags' => ['link', 'meta'],
'#cache' => [
'tags' => $realFaviconManager->getCacheTags(),
],
],
'real_favicon',
];
}
}
/**
* Load favicon by theme.
*
* @param string $theme_id
* The theme id.
*
* @return \Drupal\real_favicon\Entity\RealFavicon|null
* The real favicon entity.
*/
function real_favicon_load_by_theme($theme_id = NULL) {
if (empty($active_theme)) {
$active_theme = \Drupal::theme()->getActiveTheme()->getName();
}
return \Drupal::service('real_favicon.manager')->loadFavicon($active_theme);
}