亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

動態創建的元標記中的 Drupal 轉義 url

動態創建的元標記中的 Drupal 轉義 url

PHP
牧羊人nacy 2023-04-28 14:02:43
我正在嘗試使用 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;. 如何防止 Drupal 對字符進行編碼?
查看完整描述

2 回答

?
PIPIONE

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&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"];

? }

}


查看完整回答
反對 回復 2023-04-28
?
慕少森

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 `&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并不能解決問題,因為這是一個錯誤。


查看完整回答
反對 回復 2023-04-28
  • 2 回答
  • 0 關注
  • 177 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號