drupal - 模块内 Drupal 7's "drupal_add_html_head"的 Drupal 8 等效项是什么?

标签 drupal drupal-8

我正在为 Drupal 8 创建一些自定义模块。它们包括一些 header 修改,以实现更好的 Facebook 集成。这是 Drupal 7 (SEVEN) 中的样子:

    $element1 = array
        (
        '#tag' => 'meta',
        '#attributes' => array
            ( 
            'property' => 'og:title',
            'content' => " Profile: " . $record->NameFirst . " " . $record->NameLast,
            ),
        );
    drupal_add_html_head($element1, 'og_title');

但是这个 drupal_add_html_head 函数在 Drupal 8 中早已消失了。而且我完全不知道从哪里开始攻击它。也许这就是“头包”?有一个 Headerbag::add。也许它在模块的返回变量中,可能在此处添加另一个元素:

return array(
  '#markup' => t($pageContent),
);

也许是 HtmlResponseAttachmentsProcessor::setHeaders?标题包::设置? session ::setRequestHeader? PoStreamWriter::setHeader? PoMetadataInterface::setHeader?

不幸的是,我几乎找不到如何使用它们的示例。我相信这里的每个人都熟悉这样的烦恼:在以前的版本中可以工作的代码在新代码中变成“没有解决方案就无法工作”。

最佳答案

您可以使用 your_module_page_attachments Hook 。例如,如果您想调整 og:image 标签,您可以执行以下操作:

function your_module_page_attachments(array &$page) {
    $ogImage = array(
        '#tag' => 'meta',
        '#attributes' => array(
            'property' => 'og:image',
            'content' => url to your image,
        ),
    );

    $page['#attached']['html_head'][] = [$ogImage, 'ogImage'];
}

Hook 信息:https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Render!theme.api.php/function/hook_page_attachments/8.2.x

关于drupal - 模块内 Drupal 7's "drupal_add_html_head"的 Drupal 8 等效项是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36905114/

相关文章:

drupal - Style Drupal 7登录页面

paypal - 使用 JavaScript 提交 PayPal 按钮时出现间歇性 500 错误

Drupal 7 到 Drupal 8 : migrate taxonomy with translation (i18n)?

Drupal 8 - entity_autocomplete 的自定义选择处理程序导致无限加载

php - 使用 php 和 html 打开 outlook 新邮件

php - 如何在 nginx 上安装 PHP 扩展?

datetime - Drupal 6 : Date module and displaying only DATE value, 没有时间

php - 在 Drupal 7 中禁用 Teaser

PostgreSQL:使用 ONLY_FULL_GROUP_BY 事件的 2 级连接的 COUNT() 行

php - 未找到 Drupal 类