2 回答
TA贡献1829条经验 获得超9个赞
DrupalCoreRenderMarkup应该可以解决问题:
<?php
use Drupal\Core\Render\Markup;
function module_page_attachments(array &$page)
{
$tags = [
["name" => "twitter:card", "content" => "summary"],
["name" => "og:url", "content" => Markup::create("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" => Markup::create("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"];
}
}
TA贡献2019条经验 获得超9个赞
needs work在 drupal 的网站上有一个关于状态问题的公开票。
解决此问题的一种方法是拦截页面并在模块文件中更改其模板,如下例所示:
文件名:example.module
/**
* Drupal bug caused the application to escape links. Therefore the og:image and og:url
* were not working. Drupal kept converting `&` to `&`
* The solution here below converts the tags into inline templates
*/
function spa_seo_page_attachments_alter(array &$attachments)
{
if (isset($attachments['#attached']['html_head'])) {
foreach ($attachments['#attached']['html_head'] as $key => $item) {
$property = !empty($item[0]['#attributes']['property']) ? $item[0]['#attributes']['property'] : '';
if ($property == "og:url" || $property == "og:image") {
$content = $item[0]['#attributes']['content'];
$property = $item[0]['#attributes']['property'];
$attachments['#attached']['html_head'][$key][0] = [
'#type' => 'inline_template',
'#template' => "{{ meta|raw }}",
'#context' => [
'meta' => '<meta property="' . $property . '" content="' . $content . '" />',
]
];
}
}
}
}
笔记
请注意,这DrupalCoreRenderMarkup并不能解决问题,因为这是一个错误。
- 2 回答
- 0 关注
- 149 浏览
添加回答
举报