Drupal 页面标题栏
Drupal Page Title Block
今天我尝试在 "Page Title" 块中添加 image_field。
不知道可不可以?
如果不可能,可以在 twig 模板中添加 field_image ?
谢谢。
以下可能对您有所帮助。首先,我为 page_title 添加了一个主题建议,这样我就可以控制我必须超越模板的地方。我正在添加 node_type - 作为模板的后缀。
然后我要添加预处理功能 - 确保将签名替换为您想要的类型 "news" 最后应替换为第一个函数中的内容类型。
在第二个函数中,我正在获取一个名为 subtitle 的字段,并将该值添加到变量中,以便它在模板中可用。
function mytheme_theme_suggestions_alter(array &$suggestions, array &$variables, $hook) {
if($hook === 'page_title') {
if($node = \Drupal::routeMatch()->getParameter('node')){
$node_type = $node->getType();
// suggestion must use _ and __ as per convention.
$suggestions[] = 'page_title__'.$node_type;
}
}
}
function mytheme_preprocess_page_title__news(&$variables) {
if ($node = \Drupal::routeMatch()->getParameter('node')) {
try {
if ($node->get('field_subtitle') && !$node->get('field_subtitle')->isEmpty()) {
$variables['sub_title'] =
$node->get('field_subtitle')
->get(0)
->get('value')
->getValue();
}
}catch(Exception $e){
\Drupal::logger('mytheme')->error($e);
}
}
}
现在在模板中,您将 sub_title 在模板文件中可用,在我的例子中它是 page-title--news.html.twig - 从 page-title.html.twig 复制并添加相应的变量。
今天我尝试在 "Page Title" 块中添加 image_field。
不知道可不可以?
如果不可能,可以在 twig 模板中添加 field_image ?
谢谢。
以下可能对您有所帮助。首先,我为 page_title 添加了一个主题建议,这样我就可以控制我必须超越模板的地方。我正在添加 node_type - 作为模板的后缀。
然后我要添加预处理功能 - 确保将签名替换为您想要的类型 "news" 最后应替换为第一个函数中的内容类型。
在第二个函数中,我正在获取一个名为 subtitle 的字段,并将该值添加到变量中,以便它在模板中可用。
function mytheme_theme_suggestions_alter(array &$suggestions, array &$variables, $hook) {
if($hook === 'page_title') {
if($node = \Drupal::routeMatch()->getParameter('node')){
$node_type = $node->getType();
// suggestion must use _ and __ as per convention.
$suggestions[] = 'page_title__'.$node_type;
}
}
}
function mytheme_preprocess_page_title__news(&$variables) {
if ($node = \Drupal::routeMatch()->getParameter('node')) {
try {
if ($node->get('field_subtitle') && !$node->get('field_subtitle')->isEmpty()) {
$variables['sub_title'] =
$node->get('field_subtitle')
->get(0)
->get('value')
->getValue();
}
}catch(Exception $e){
\Drupal::logger('mytheme')->error($e);
}
}
}
现在在模板中,您将 sub_title 在模板文件中可用,在我的例子中它是 page-title--news.html.twig - 从 page-title.html.twig 复制并添加相应的变量。