You are here

Drupal8 下面为body增加指定的class

g089h515r806 的头像
Submitted by g089h515r806 on 星期二, 2019-07-30 02:58

我们在开发Drupal8中,我们将一个Drupal网站分成多个频道,每个频道包含多个栏目和子栏目。

为了控制每个频道的样式,需要为html的body指定自己的class,经过研究可以使用hook_preprocess_html。


示例代码:

function mytheme_preprocess_html(&$variables) {
  // Add node id to the body class.
  $pindao_id = lanmu_get_current_pindao();
  $pindao_mappings =[
    1=> 'about', 
	2 => 'science-research',  
	3 => 'technology', 
	4 => 'services', 
	5 => 'cooperation',  
	6 => 'human-resources',  
	7 => 'dangjian',  
	8 => 'resource',  
	9 => 'contactus',  
	10 => 'news',  
  ];
  if(!empty($pindao_mappings[$pindao_id])) {
	$variables['attributes']['class'][] = 'pindao';
    $variables['attributes']['class'][] = 'pindao-' . $pindao_mappings[$pindao_id];
  }
}

这样就可以按照频道,给网站添加特定频道的class,对于每个频道特定的样式,我们可以单独指定。

论坛:

Drupal版本: