使用优雅的子主题在 Drupal 8 中构建站点。遇到一个令人费解的主题问题 - 根据该节点上的分类术语将主体类添加到 html.html.twig。
主题使用它来自定义页面显示,在我的例子中,使用它来定义我网站的几个部分,以便我可以更改颜色和格式。
我尝试了一些在谷歌上看到的预处理函数,但没有结果。
还有其他人遇到并解决了这个问题吗?
最佳答案
使用它来获取节点的所有字段并检查您需要的内容:
\Drupal::service('current_route_match')->getParameter('node')->toArray();
在您的 .theme 文件中,您可以使用 html 预处理 Hook :
function your_theme_preprocess_html(&$variables) {
$body_classes = [];
$nodeFields = \Drupal::service('current_route_match')->getParameter('node')->toArray();
// if something, then set $body_classes to something.
$variables['attributes']['class'] += $body_classes;
}
然后在 html twig 模板中将属性添加到 body 元素:
<body{{ attributes }}>
希望这有帮助。
关于Drupal 8 - 根据分类术语或其他添加主体类别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37661815/