我有一段时间试图在D8中为我的身体元素上课。我正在使用自定义主题,到目前为止我的代码看起来像这样:
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是空的,读取,'page-node'我需要它读取'page-node-NID'。
答案 0 :(得分:0)
你必须用主题名mytheme_preprocess_html
替换HOOK,否则Drupal不会调用钩子。