所以我要做的是让页脚区域中的所有块使用相同的模板,比如block--footer-block.html.twig
。
我尝试的是使用hook_theme_suggestions_HOOK_alter()
来检查块的位置并将区域名称添加到模板建议中。
/**
* Implements hook_theme_suggestions_HOOK_alter().
* */
function mytheme_theme_suggestions_block_alter(array &$suggestions, array
$variables {
$block = Block::load($variables['elements']);
$region = $block->getRegion();
$suggestions[] = 'block . '__' . $region . '__block';
}
目前$block->getRegion();
破坏了我的网站 - 不知道为什么。它甚至没有给出任何错误,该网站只是空白的。
这甚至是一个好方法吗?
这是在这里回答的: https://drupal.stackexchange.com/questions/192616/how-to-make-a-theme-hook-suggestion-for-blocks-according-to-region
function mytheme_theme_suggestions_block_alter(array &$suggestions, array $variables) {
$block = Block::load($variables['elements']['#id']);
$region = $block->getRegion();
$suggestions[] = 'block__'.$region.'__block';
}
您也可以在hook_theme_suggestions_HOOK_alter
中使用此代码段:
if (isset($variables['elements']['#configuration']['region'])) {
$region = $variables['elements']['#configuration']['region'];
$suggestions[] = 'block__' . $region;
if (isset($variables['elements']['#configuration']['provider'])) {
$provider = $variables['elements']['#configuration']['provider'];
$suggestions[] = 'block__' . $region . '__' . $provider;
}
}
参考