首页
学习
活动
专区
圈层
工具
发布

D8体类
EN

Stack Overflow用户
提问于 2016-09-07 21:04:09
回答 1查看 415关注 0票数 1

我正尝试在D8中获取body元素的类。我使用的是一个自定义主题,到目前为止我的代码如下所示:

themename.theme

代码语言:javascript
复制
use Drupal\Component\Utility\Html;

/**
 * Preprocess variables for html templates.
 */
function HOOK_preprocess_html(&$variables) {
  $path_class = !$variables['root_path'] ? 'path-frontpage' : 'path-' . Html::getClass($variables['root_path']);
  if (isset($path_class)) {
    $variables['attributes']['class'][] = $path_class;
  }
}

html.html.twig

代码语言:javascript
复制
    {%
        set body_classes = [
          not root_path ? 'path-frontpage' : 'path-' ~ root_path|clean_class,
        ]
      %}
<body{{ attributes.addClass(body_classes) }}>

我在body元素上得到类,但是NID是空的,读取的是“页面-节点”(我需要它读取的地方),“页面-节点-NID”。

EN

回答 1

Stack Overflow用户

发布于 2016-09-09 07:43:40

您必须用主题mytheme_preprocess_html的名称替换钩子,否则Drupal将不会调用钩子。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39378981

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档