我正尝试在D8中获取body元素的类。我使用的是一个自定义主题,到目前为止我的代码如下所示:
themename.theme
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
{%
set body_classes = [
not root_path ? 'path-frontpage' : 'path-' ~ root_path|clean_class,
]
%}
<body{{ attributes.addClass(body_classes) }}>我在body元素上得到类,但是NID是空的,读取的是“页面-节点”(我需要它读取的地方),“页面-节点-NID”。
发布于 2016-09-09 07:43:40
您必须用主题mytheme_preprocess_html的名称替换钩子,否则Drupal将不会调用钩子。
https://stackoverflow.com/questions/39378981
复制相似问题