Class 在drupal 8的标题区域中添加自定义类

Class 在drupal 8的标题区域中添加自定义类,class,drupal,hook,region,Class,Drupal,Hook,Region,我想在我的drupal模板的头部区域添加自定义类,但它不起作用 输出中已经有了所需的所有内容 许多路通向罗马 如果themes/marineat是基本主题,只需将themes/marinenat/templates/layout/region--header.html.twig复制到子主题的themes/MYSUBTHEME/templates目录中并编辑此文件。刷新缓存。完成了 如果themes/marinenat是自定义子主题,只需编辑建议的模板文件/themes/marinenat/te

我想在我的drupal模板的头部区域添加自定义类,但它不起作用


输出中已经有了所需的所有内容

许多路通向罗马 如果
themes/marineat
是基本主题,只需将
themes/marinenat/templates/layout/region--header.html.twig
复制到子主题的
themes/MYSUBTHEME/templates
目录中并编辑此文件。刷新缓存。完成了

如果
themes/marinenat
是自定义子主题,只需编辑建议的模板文件
/themes/marinenat/templates/layout/region--header.html.twig/
,然后将类添加到其中。刷新缓存。完成了

最后但并非最不重要的一点是,您还可以从
MYSUBTHEME.theme
文件或任何
MYMODULE.module
文件的预处理函数向区域添加类

/**
*实现模板预处理区域()。
*/
函数MYTHEME/MYMODULE_预处理_区域(&$variables){
if(isset($variables['region'])&&&$variables['region']=='header'){
$variables['attributes']['class'][]='MYCLASS';
}
}

如何将字符串从PHP传递到Twig
/**
*实现模板预处理区域()。
*/
函数MYTHEME/MYMODULE_预处理_区域(&$variables){
$variables['foo']=FALSE;
if(isset($variables['region'])&&&$variables['region']=='header'){
//获取当前节点。
$node=\Drupal::routeMatch()->getParameter('node');
if($node instanceof\Drupal\node\NodeInterface){
//获取节点类型。
$node_type=$node->bundle();
//执行动态检索类所需的其他操作。
$variables['foo']=$node\u type;
}
}
}
然后在
区域--header.html.twig
twig文件中:

{% if foo %}
  <div class="nav {{ foo }}">
    {{ content }}
  </div>
{% endif %}
{%if foo%}
{{content}}
{%endif%}

Hello,我添加了marinenat.theme预处理区域函数和刷新缓存,但没有添加类appear@antoine-你到底补充了什么?您是否用
marineat\u preprocess\u region
替换了
myteme/MYMODULE\u preprocess\u region
?如果它仍然不工作,编辑模板
/themes/marinenat/templates/layout/region--header.html.twig/
。我的模板/themes/marinenat/templates/layout/region--header.html.twig是:{%If content%}{{{content}{endif%}{code>猜一下当你把它改成
{%If content%}{content}{endif%}{content}{code>时会发生什么,没错,但我想动态添加类