【问题标题】:Drupal escaping url in dynamically created meta tagsDrupal 在动态创建的元标记中转义 url
【发布时间】:2020-06-02 10:00:01
【问题描述】:

我正在尝试使用 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&param2=2&param3=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&amp;param2=2&amp;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&amp;size=400" />

</head>
<body>
</body>
</html>

所有&amp;amp; 字符都已编码并转换为&amp;amp;。如何防止 Drupal 对字符进行编码?

【问题讨论】:

    标签: php drupal character-encoding facebook-opengraph drupal-8


    【解决方案1】:

    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&param2=2&param3=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"];
      }
    }
    

    【讨论】:

      【解决方案2】:

      在 drupal 的网站上有一张状态为 needs work 的问题的未决票。

      可以在这里找到问题:

      www.drupal.org/project/drupal/issues/2968558

      解决该问题的方法是截取页面并在模块文件中更改其模板,如下面的示例所示:

      文件名:example.module

      /**
       * Drupal bug caused the application to escape links. Therefore the og:image and og:url
       * were not working. Drupal kept converting `&` to `&amp;`
       * 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没有解决问题,因为这是一个错误。

      【讨论】:

        猜你喜欢
        • 2015-12-29
        • 1970-01-01
        • 2017-07-30
        • 2022-01-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多