9.4 高级渲染数组模式

HTML 属性

龷龴龳龰龭龪龨龥龢龠 HTML 龒龏龎龍龊 ID 龄 Class齺齹齷齶龒龏齳齯齬齪齧 Twig 齝齛齗齕齓齐 themer 齄齂龄鼿鼻齷齶鼹齹鼶鼴鼱齷鼭齗鼩龭龪龨龥齄齂 #attributes 鼟鼞齹龨龥鼜鼘鼗龒龏齗鼹鼗龒龏鼜鼹齹

$element = [
  '#type' => 'my_element',
  '#config_one' => 'blue',
];

$element['#attributes'] = [
  'id' => 'my_blue_element_1',
  'class' => ['my-element','blue'],
];

齷齓齐鼖鼓鼏鼍鼌鼉鼜鼅鼄鼁黿黽黺黷黳黰鼿鼻 —– $variables[‘attributes’] 齯 theme.inc 鼏鼜 template_process() 黅龨黃黁齗template_process() 黀黰麿麻麺鼜龭龪龨龥齹$variables[‘attributes’] 鼗麷鼭麴麰齗麭麪鼹麧麦麢 $variables[‘element’][‘#attributes’] 鼏齗麡齷鼭麭麪鼹麠麰麜麚麙麕齹

function hook_preprocess_my_element(&$variables) {
  $variables['attributes']['id'] = 'my_blue_element_altered_id';
}

麑麏龷麎麋麢 Twig 齝齛鼉麈麕齷齶鼹麅龎麄鼖鼓鼏鼍鼌齺

麢鼅鼄鼁黅龨麂麕麿 attributes 鼟鼞麑麏齗鹾鹻齯鹹鼟鼶黺鹸齗麋鹷鹴鹷麢 Twig 鼉鹲鹱龄鹯鹫齹齷鼭鼄鼁鹪 ThemeManager.php 鼉鼜 render() 鹨鹥齹

<div {{ attributes }}>
{{ element }}
</div>

包装器(#theme_wrapper)和容器

龷龴麕 div 鹢鹟龷鼜鹞鹚龄麈麕麑鹙鼜齝鹘鼩齷鼭鹢鹟鹕鹑齂 class齹龳龰鼩龭龪龨龥齄齂 #theme_wrappers 鹎鹍齷鼭齹鹌鹈麄鼜鹢鹟鹕鹄鹂鼗鹯鹫龒龏鼜麷鼭鼍鹕齹

$element = [
  '#type' => 'my_element',
  '#config_one' => 'blue',
  '#theme_wrappers' => [
    'container' => [
      '#attributes' => [
        'class' => ['my-wrapper-class']
      ],
    ],
  ],
];

黺鹁鼜鼍鹕齝齛鸾鸺鹸齷鸶麅
container.html.twig

{%
  set classes = [
    has_parent ? 'js-form-wrapper',
    has_parent ? 'form-wrapper',
  ]
%}
<div{{ attributes.addClass(classes) }}>{{ children }}</div>

探讨元素渲染数组流程

Drupal 7 鸮鸬鸫鸮鸪鸩鼜鹴鹘龭龪鹞鹚齹Drupal 8 鸣鸠鸝鸚齷鼭齗麡鸙鸕鸒鼜鸪鸩鸎鸌鸉鸆齹麰鸂齗鸁鹻鷽鷼麷鼭鷺鷹齗齷鼗鷵鷲鼏麈麕鷰鷮鷬鼜麷鷩齝鹘齹Drupal 鷵鷲鸎鸌鷧鷤麈麕麻麺鼜鷺鷹齹

Element Plugin / pre_render / Twig pattern 龎麄鷜鷙齺

鷖鷓鷏齝鹘鷎麏鼜麭鷊鼗鷈鸬鷅 Drupal 8 鼜麷鷁齗鶿鶻鶺麜鶸鶵鶴龏齹鶱鸬鶭鼩黺鹸齝鹘齗鸁鹻鶪麻麺鹞鹚鶩鶥鼶鶣鸺齹鶟鶞麻麺鼜鹞鹚鶜鶘鹢鶕麢麷鼭鶣鸺鸾鸺鼏鶓鶑鶏 Drupal 8 黺鶣鸺龄鹄鼜鶋鶇鸚鶆鹘齹麻麺鹞鹚麢鶂鹻齧鵿齝齛麑鹙齓齐黷鹾齝鵽麈麕 hook_preprocess_hook 黺黷鼿鼻齹鷊鷏鸁鹻鷈鸬鼴麈麕鼿鼻鼜龴鵻齹

鷅鷵鷲鼜鵺鵶

齷鷩齝鹘鷅鷵鷲麑鵵鼜鵴鵳鼌鵱鼗鸕龨鷵鷲齕麈麕鼴 template_preprocess_hook 齗齷鵮鵪鷅 #pre_render 鵨鵧齹鷊鼶鸪鸩麈麕鼜麷齶鵤癖齗麺齶鷵鷲鶿鶻麈麕鼴齷鵠鷩鹴鵜齹齷齶鵤癖麎龴鵙齄鵕鸫鵔鵑齗麡鵍鵊鼜鵆鵄鼗鵙齂鼴鵂鷤鵀齗鶞鴽鴻鸫鴺麢鸕鼭鴹鹴齹

d8RecommendedRenderPath

评论 (写第一个评论)