Drupal 8 - 根据分类术语或其他添加主体类别

标签 drupal themes preprocessor drupal-8

使用优雅的子主题在 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/

相关文章:

android - 如何在样式中引用颜色属性

Android 在 Spinner 中更改文本颜色

c - 如何在 C include 语句中使用#define?

javascript - Gulp CJSX 渲染编译错误

c - 为什么鼓励使用 "declared data types"而不鼓励使用 "processor-dependent data types"?

mysql - 如何在drupal 7 db_select查询中编写自定义orderby?

drupal - hook_user op 加载不触发?

css - 是否可以在 Drupal 的内容节点上方有一个大图像?

php - 如何以编程方式获取 drupal webform 模块提交的值?

wpf - 如何找到正确的颜色以使用Visual Studio 2010主题编辑器进行编辑?