我正在尝试使用 page_attachments 挂钩将开放图元标记动态添加到 drupal 8。
元标记生成正确,但图像和网站 url 正在由 drupal 编码,结果是断开的链接。
function module_page_attachments(array &$page)
{
$tags = [
["name" => "twitter:card", "content" => "summary"],
["name" => "og:url", "content" => "https://example.net/index.php?param1=1¶m2=2¶m3=3"],
["name" => "og:title", "content" => "My title"],
["name" => "og:description", "content" => "My description"],
["name" => "og:image", "content" => "https://example.net/images?id=1&size=400"],
];
foreach ($tags as $tag) {
$headerTag = array(
'#tag' => 'meta',
'#attributes' => array(
'property' => $tag['name'],
'content' => $tag['content'],
),
);
$page['#attached']['html_head'][] = [$headerTag, $tag['name'] . "Id"];
}
}
结果如下
<html>
<head>
<meta charset="utf-8" />
<meta property="twitter:card" content="summary" />
<meta property="og:url"
content="https://example.com/index.php?param1=1&param2=2&param3=3" />
<meta property="og:title" content="My title" />
<meta property="og:description"
content="My description" />
<meta property="og:image"
content="https://example.net/images?id=1&size=400" />
</head>
<body>
</body>
</html>
所有的&字符都被编码并转化为&. 如何防止 Drupal 对字符进行编码?
PIPIONE
慕少森