81. 响应附属处理attachments_processor

龵龱龮龭龩龨龦龣龢龞龜龙龱龮龭龗龔龐龍龉龆龅龄龍龉龁龄龍龉齿齻齺齶龩龨

齲齱齭 attachments 齚齗龍龉龆龅龐 #attached 齐齌龣齈齄齃鼿鼻齲齱齭鼹鼸鼶鼵鼱鼻齲鼮鼹龣鼭鼪鼧鼥鼤龄鼢鼮齶鼞鼝鼚鼘鼗龣齲齱齭鼕鼑鼎 8 鼈鼅鼁鼀鼭龵 #attached 黽龐黼黹龔黸龣鼑黷黳黰黯黬黩黼黹黨黥黣黡黠黝黜

library 黍黊黉龣黇黄 css js 龄黉 js 麿麼齶龣麸麵龭龗龔黍黊黉麸麴

drupalSettings 龵龍龉龆龅黽麿麼龐 JavaScript 麿麼龆麡

html_head HTML <head> 齐齌龐麓麏

html_head_link HTML <head> 齐齌龐 <link> 麓麏龣齗 html_head 龐麊麇麆麅

feed RSS 麁龱龣齗 html_head_link 龐麊麇麆麅

http_header HTTP 鹾鹻鹸鼧鹴鹳鹱齶

placeholders 齿齻鹫鹨龣鹧鹤鹢鹞齈鼶鹝齲齱齭龣鹛齗龗鹗黨黬鼧齲齱齭龵鹔鹒鼈鹐鹌黽鹉鹅

html_response_attachment_placeholders 黇黄鹄鹃鼅鼗齲齱齭龐齿齻齺龣麵鸿

龵龗鹗黽鸻齲齱齭龐鹉鹅齗龵鸺鸷 kernel.response 鸲鼮鸰鸬黜

麁龱龁鹐鹌黜 html_response.subscriber

鼅黜 \Drupal\Core\EventSubscriber\HtmlResponseSubscriber



鸞鸜鸙鸘黜onRespond

鸗龙鸖黜 0-0

鸒麁龱龁鹛鸎鸻 html 鹾鹻鸍鸜鹉鹅黜

\Drupal\Core\Render\HtmlResponse

鹛齗黣鸋鸇鸆龐麁龱鸲鼮鸃鷿龣鷾齐齲齱齭鹉鹅鷼鸃龵齲齱齭鹉鹅龁黽鸍鸜龣麵鼎鷺

齲齱齭鹉鹅龁黜

鷶鷵龵鷱鷰麁龱龁黽鸻 HTML 鹾鹻鸍鸜齲齱齭齿齻齺鷬鷨鹉鹅

鹐鹌黹黜 html_response.attachments_processor

鼅黜 Drupal\Core\Render\HtmlResponseAttachmentsProcessor

鷢鷠鸙鸘黜 \Drupal::service('html_response.attachments_processor');

鸒鹉鹅龁鹛鸎鸻 \Drupal\Core\Render\HtmlResponse 鹾鹻龣齈鷔齗龗鹗 HTML 鷐鷌龐龩龜鹾鹻龣鼎鼢鸎鸻鷊鼅齲齱齭鸆鷉鷅麴鷺

鹫鹨齿齻齺 placeholders

placeholders 鹧鹤鹢鹞鼶齱鷁齲齱齭龣鼸齗鷐鷌鹫鹨龣鶿鶿齗鼶鶾鶽鶼黄龐龞龜鶸鹳鶷鶳龐鷐鷌鶰齐鹫鹨龣龞龜鹫鹨齿齻龐鶭鶩鼀鶭鶩鶦龍龉龆龅黝黼黹鹛鶢鶡鶟黜 '#lazy_builder', '#cache', '#create_placeholder','#weight', '#printed' 龣黬黽 '#lazy_builder' 鶔鶑黯鹒鼈鶏鶋鷿鷁鶷鶳鶉鶅鹫鹨龐龍龉龆龅龣龵 html 鶁齺鵽黽齈鵺龐鹫鹨龙鼭齿齻齺齿齻龣齿齻齺鵹龍龉齿齻齺鶷鶳龁鼀鹐鹌 id render_placeholder_generator 黝鶷鶳龣鹤鵵鼑鼎黜

<drupal-render-placeholder callback="' . Html::escape($callback) . '" arguments="' . Html::escape($arguments) . '" token="' . Html::escape($token) . '"></drupal-render-placeholder>

龍龉齿齻齺鶷鶳龁鹔鵧龵龍龉龆龅黽 $elements['#attached']['placeholders'] 黽黳黰鵣龣黬齗鹒鼈龆龅龣黼黹鼿齿齻齺龣黼鵣齗鹒鼈龆龅龣鸒龆龅鹛鼕鵢鼈黼黹黜 '#lazy_builder' 鷿鷁鶔鶑鵡鶏鵠龍龉龆龅龐鶏鶋龄 '#cache' 鷿鷁黇黄鶼黄黊龆麡

鸻鹫鹨齿齻齺龐鹉鹅齚齗黨鹾鹻鼀 html 鶁齺鵽黝黽龐鹫鹨齿齻齺鷬鷨鵜鶉鶅鹫鹨龣齈鹒鵙鵕龵龍龉龁黽鸞鸜龣鶉鶅鹫鹨鵹 $elements['#attached']['placeholders'] 黽齿齻齺黼黹黇黄龐鶏鶋鶷鶳鼀鹞鵓鷁龍龉龆龅龐 '#lazy_builder' 黝龣鶏鶋鵒鶷鶳鵠龐龍龉龆龅龣龍龉龁鸻黬鶾鵐鵏鶼黄鶭龆麡龣鵐龍龉龣鷿龍龉鵎黷鵍鷬鷨齿齻齺鷺

鸒鵙鵕鶿鶿鵒鶷鶳鵠龐鶼黄鶭龆麡鼧齲齱齭龣鵌鵋龵齲齱齭鹉鹅龁黽鵊龞龜鵉龙鹉鹅龣鵉龙鶋鷿龍龉龁龐 $renderer->renderRoot($html) 鸙鸘鸍鸜龍龉龣龵鵡鶏鸿鵅鵠鹾鹻鸻鵁龐鶼黄鶭龆麡鼧齲齱齭龣齈鵺齚鴿鴼鴻黬黩龐齲齱齭鹉鹅黯鷺鼀龮鸋齈齄齗鴸鴴鴲鸻 drupal 龍龉鴮鴫鼕鴩鴨麵鴥龐鴴鴲鴢龣鷔鴠鴜鴙鴗鼵鴕龍龉黯齗鴓黝

齲齱齭齿齻齺 html_response_attachment_placeholders

黇黄 4 鼈鼅鼁鼀 styles scripts scripts_bottom head 黝齲齱齭龐齿齻齺鵣龣黬黽 html_head html_head_link feed 鴍齗 head 鼅鼁龣齲齱齭鷬鷨齚齗鷬鷨鵊黇黄龐齿齻齺鶁齺鵽龣鸒鹫鹨齗龵鼭鼎鴉鹉鹅鴇龆黽黳黰龐黜

template_preprocess_html(&$variables) 鼀齻鷁 core\includes\theme.inc

鼀鴉鹉鹅鴇龆鼕鶢鴂黳黰鶼黄鶭龆麡鼧齲齱齭龣鴀鳾齻鷁龩龨鴮鹅龁黽龣麸麵龩龨鴮鹅龁鼧龩龨鳽鳺黽鴉鹉鹅鴇龆鹒鳷黝鷺

鵋鳳龵 \Drupal\Core\Render\HtmlResponse 黽鷔鼕鳰鳮鳪鳩麿麼龣鸒鳧鵣鼅鳤鼑鼎黜

[html_response_attachment_placeholders] => Array
(
[styles]=>
<css-placeholder token="adoAt0nf3KI5PsdTh274WS4ttC5No7Ko6rIYzlf4jt-dg5Qm9EfISwA93gWuIlIZpGGhBkYodQ">
[scripts]=>
<js-placeholder token="adoAt0nf3KI5PsdTh274WS4ttC5No7Ko6rIYzlf4jt-dg5Qm9EfISwA93gWuIlIZpGGhBkYodQ">
[scripts_bottom]=>
<js-bottom-placeholder token="adoAt0nf3KI5PsdTh274WS4ttC5No7Ko6rIYzlf4jt-dg5Qm9EfISwA93gWuIlIZpGGhBkYodQ">
[head]=>
<head-placeholder token="adoAt0nf3KI5PsdTh274WS4ttC5No7Ko6rIYzlf4jt-dg5Qm9EfISwA93gWuIlIZpGGhBkYodQ">
)

鹸齐鲺鷿麓麏 html_head

html 鹸齐麓麏鼀齻鷁 head 鼘鲹龐麓麏黝龣鹞鵓龍龉龆龅 $renderArray['#attached']['html_head'] 龣齗鵹 $head 鶭鶩鲸鵜龐鹒鼈龆龅龣 $head 龐鹤鵵鼑鼎黜

$head = [$element , $name];

$element 齗鸆鼈鹸齐麓麏龐龍龉龆龅龣鴿鼭鲵鲲 $element ['#type'] = 'html_tag'; 龣龗鹗鵒鵓鶸鳰鷾

$name 鷿鷁鲯鲮龍龉鹸齐麓麏鵧龵 $element 龅鵜龐鵅鹃龐龍龉龆龅黽鳮鲪龭鶭鶩龐鲧鶭鶩黹龣鴼鸷鲤鲡鲟鶔鶑

鲜鲛鴿鼭龵龍龉龆龅黽鲺鵙鵊麿麼鲙鲟鹸齐麓麏龣鲗鲖鼑鼎黜

$renderArray['#attached']['html_head'][] = [
    [
        '#type'       => 'html_tag', //如上所述,可省略该行,系统会自动补充
        '#tag'        => 'meta', //头部标签名
        '#attributes' => [
            'charset' => 'utf-8',
        ],
        '#weight'     => -1000,
    ],
    'system_meta_content_type'
];
$renderArray['#attached']['html_head'][] = [
    [
        '#tag'        => 'meta',
        '#attributes' => [
            'name'    => 'viewport',
            'content' => 'width=device-width, initial-scale=1.0',
        ],
    ],
    'viewport'
];

齈黨鶷鶳鼭鼎麓麏黜

<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />

鹸齐 link 麓麏 html_head_link

html 鹸齐鼀 head 鼘鲹黝龐 link 麓麏龣鹞鵓龍龉龆龅 $renderArray['#attached']['html_head_link'] 龣齗鵹 $link 鲸鵜龐龆龅龣 $link 鹤鵵鼑鼎黜

$link = [$attributes, $should_add_header];

$attributes 齗鹒鼈齱鲍鲉龆龅龣鲸鵜 link 麓麏龐齱鲍龣黼黹鼿齱鲍黹龣黼鵣鼿齱鲍鵣

$should_add_header 齗鹒鼈鲇鲆鵣龣鶔鲗鸒 link 齗鴸鲂龞龜黳黰鸋 http 鹾鹻鹸黽龣鳪鳩鵣鼿 false

鲜鲛鴿鼭龵龍龉龆龅黽鲺鵙鵊麿麼鲙鲟鹸齐 link 麓麏龣鲗鲖鼑鼎黜

$renderArray['#attached']['html_head_link'][] = [
    [
        'rel'  => 'shortcut icon',
        'href' => '/core/misc/favicon.ico',
        'type' => 'image/vnd.microsoft.icon',
    ],
    false
];

黨鶳鵜鼑鼎鹒鼈 link 麓麏黜

<link rel="shortcut icon" href="/core/misc/favicon.ico" type="image/vnd.microsoft.icon" />

鼑黷鷱鷌龐 $should_add_header 鶔鶑鼿 true 鵧鲂鵒黳黰鼑鼎鹒鱾 http 鹾鹻鹸黜

Link: < /core/misc/favicon.ico>; rel="shortcut icon"; type="image/vnd.microsoft.icon"

html_head_link html_head 龐麊麇麆麅

龵龗鹗鱻鸿 $renderArray['#attached']['html_head_link'] 黨鱹鱶鱳鼿 $renderArray['#attached']['html_head'] 鱯鹉鹅

鱮鶾黊 link 麓麏 feed

鱮鶾黊鼀鼑黷龢鸻 rss feed 鲂鼶鱪鱩龣鱦龙鱣鱢黝龣鹞鵓龍龉龆龅 $renderArray['#attached']['feed'] 龣齗鵹 $feed 鲸鵜龐龆龅龣 $feed 鹤鵵鼑鼎黜

$feed= [$href , $title];

$href feed 龐鱠鱝

$title 齗麓龨龣鴿鱜龣鳪鳩鼿鱙鶁齺鵽龣鸒鵣鹻鸒齗鱘鱗鸿龐鵣

鲗鲖鼑鼎黜

$renderArray['#attached']['feed'][] = [
    'http://www.yunke.com/rss.xml',
    'yunke'
];

html 鹸齐鼘鲹黨鶷鶳鼑鼎鹒鼈 link 麓麏黜

<link rel="alternate" type="application/rss+xml" title="yunke" href=" http://www.yunke.com/rss.xml " />

feed html_head_link 龐鹒鱒麊麇麆麅

龵龗鹗鱻鸿 $renderArray['#attached']['feed'] 黨鱹鱶鱳鼿 $renderArray['#attached']['html_head_link'] 鱯鹉鹅龣 feed 鼶鵒鶷鶳 http 鹾鹻鹸

鹾鹻鹸 http_header

鹞鵓龍龉龆龅 $renderArray['#attached']['http_header'] 龣齗鵹 $ header 鲸鵜龐龆龅龣 $ header 鹤鵵鼑鼎黜

$ header = [$name , $value , $replace];

$name 齗鹾鹻鹸龐黹鶁

$value 齗鹾鹻鹸龐鵣

$replace 齗鹒鼈鲇鲆鵣龣 true 鱏鲗鷬鷨鲪龦鱍麿麼龐鵣龣 false 鱏鲗黨鵣鱉黰鸋鲪龦龐鵣黽龣鼶麿麼鳪鳩鼿 false

鲗鲖鼑鼎黜

    $renderArray ['#attached']['http_header'][] = ['X-Test-Teapot-Replace', 'This value gets replaced'];
    $renderArray ['#attached']['http_header'][] = ['X-Test-Teapot-Replace', 'Teapot replaced', TRUE];
    $renderArray ['#attached']['http_header'][] = ['X-Test-Teapot-No-Replace', 'This value is not replaced'];
    $renderArray ['#attached']['http_header'][] = ['X-Test-Teapot-No-Replace', 'This one is added', FALSE];
    $renderArray ['#attached']['http_header'][] = [' status', 404];

鸒鱜鳧鷔鴿鼭鷿鷁麿麼鹴鹳鹱鱁 cookie 龣鵹鷁鸞鸜鵧鱀龵龗鹗鸺鸷鹾鹻鸲鼮鵧龣鵌鵋鸗龙鸖鰽鰻龣鼑黷龢鰸龜鸍鸜鰷鰳龣鱦龵鰱鰯龁黽鸞鸜鼭鼎鰬鹱黜

        $renderArray=['#markup' => "this is http_header test"];
        $renderArray['#attached']['http_header'][] = ['X-Yunke', 'This is http_header value', FALSE];
        return $renderArray;

鴿龵鰨鰦鴼鸷鲤鷼鰥黽鰤鰣鹾鹻鹸龣鼑鼎黜

X-Yunke: This is http_header value

黍黊麴鰠龁黜

鸒鰜鹫鹨鰽鰙龣鷿鷉鰖龩龨鰒鷰龣鱦麵龭龗龔鰎黍黊麴鰠龁 AssetResolver

黍黊鲉 css js 龍龉龁

黍黊麴鰠龁鵡鶏龐黍黊齗鼭鱜鳧龆龅鱏鲗龐龣麸麵龭龗龔黍黊麴鰠龁龩龨龣黍黊鲉龍龉龁龩龜鷼鸃齗黨鸒鱜鳧龆龅鱶鷨鼿龍龉龆龅龣鵐鶋鰄鰁鯿齶

css 黍黊鲉龍龉龁黜

鹐鹌黹黜 asset.css.collection_renderer

鼅黜 Drupal\Core\Asset\CssCollectionRenderer

js 黍黊鲉龍龉龁黜

鹐鹌黹黜 asset.js.collection_renderer

鼅黜 Drupal\Core\Asset\JsCollectionRenderer

鳰鳮鹢鴠黜

鼑黷鷐鷌齗 ajax 黰鯾龣鯻鴗鼭鼎齻麼黨黇黄鱍黰鯾龆麡黜

$ajax_page_state = \Drupal::requestStack()->getCurrentRequest()->get('ajax_page_state');

鯶鼑 $ajax_page_state['libraries'] 黨黇黄鱍鯵黰鯾龐黍黊黉

本书共88小节:

评论 (写第一个评论)