Skip to content

Commit 7adec0a

Browse files
author
Callin Mullaney
committed
fix: harden favicon head link cleanup
1 parent 1e3271b commit 7adec0a

1 file changed

Lines changed: 37 additions & 8 deletions

File tree

src/Favicon/FaviconHeadBuilder.php

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,19 @@ public function apply(array &$attachments, array $settings): void {
8989
*/
9090
private function removeConflictingLinks(array &$attachments): void {
9191
if (!empty($attachments['#attached']['html_head_link'])) {
92-
$attachments['#attached']['html_head_link'] = array_values(array_filter(
93-
$attachments['#attached']['html_head_link'],
94-
static function (array $item): bool {
95-
$attributes = $item[0] ?? [];
96-
$rel = strtolower((string) ($attributes['rel'] ?? ''));
97-
return !in_array($rel, ['shortcut icon', 'icon', 'apple-touch-icon', 'manifest'], TRUE);
98-
},
99-
));
92+
$head_links = [];
93+
foreach ($attachments['#attached']['html_head_link'] as $item) {
94+
$normalized = $this->normalizeHeadLinkAttachment($item);
95+
if ($normalized === NULL) {
96+
continue;
97+
}
98+
99+
$rel = strtolower((string) $normalized[0]['rel']);
100+
if (!in_array($rel, ['shortcut icon', 'icon', 'apple-touch-icon', 'manifest'], TRUE)) {
101+
$head_links[] = $normalized;
102+
}
103+
}
104+
$attachments['#attached']['html_head_link'] = $head_links;
100105
}
101106

102107
if (!empty($attachments['#attached']['html_head'])) {
@@ -111,4 +116,28 @@ static function (array $item): bool {
111116
}
112117
}
113118

119+
/**
120+
* Normalizes one html_head_link attachment to Drupal core's expected shape.
121+
*
122+
* @return array{0: array<string, mixed>, 1: bool}|null
123+
* A normalized link attachment, or NULL when the item is unusable.
124+
*/
125+
private function normalizeHeadLinkAttachment(mixed $item): ?array {
126+
if (!is_array($item) || !isset($item[0]) || !is_array($item[0])) {
127+
return NULL;
128+
}
129+
130+
$attributes = $item[0];
131+
if (empty($attributes['rel']) || empty($attributes['href'])) {
132+
return NULL;
133+
}
134+
135+
$add_header = $item[1] ?? FALSE;
136+
137+
return [
138+
$attributes,
139+
is_bool($add_header) ? $add_header : FALSE,
140+
];
141+
}
142+
114143
}

0 commit comments

Comments
 (0)