【发布时间】:2019-11-18 15:51:48
【问题描述】:
我想在树枝布局文件中为块添加自定义类型类:block.html.twig 但我不知道如何输入。 类只是样式需求所需要的,因为我有多种语言的许多块。我不想通过自定义 id 来设置元素的样式。
【问题讨论】:
我想在树枝布局文件中为块添加自定义类型类:block.html.twig 但我不知道如何输入。 类只是样式需求所需要的,因为我有多种语言的许多块。我不想通过自定义 id 来设置元素的样式。
【问题讨论】:
实际上我找到了一个可行的解决方案,其中一个使用主题挂钩:
/**
* Implements hook_preprocess_HOOK() for block.html.twig.
*/
function MYTHEME_preprocess_block(&$variables) {
// adding custom attribute class for block
if ($variables['elements']['#base_plugin_id'] == 'block_content') {
$blockType = strtr($variables['content']['#block_content']->bundle(), '_', '-');
$variables['attributes']['class'][] = 'block--type-' . $blockType;
}
}
这个只是为自定义块类型添加类。但可以满足我的需要。
【讨论】: