作者: 老葛 亚艾元软件
1, menu breadcrumb
2, easy breadcrumb
目前模块只有这两个可用,但是功能,常常满足不了需求。在其它模块没有升级上去的情况下,我建议采用在主题层覆写的方式解决这一问题,只需要懂得一些代码即可。以huiling主题为例,huiling.theme文件里面,添加以下代码:
function huiling_preprocess_breadcrumb(&$variables) {
//$breadcrumb = $variables['breadcrumb'];
$breadcrumb = array();
//drupal_set_message(var_export($term_id,true));
$node = \Drupal::routeMatch()->getParameter('node');
if ($node) {
$breadcrumb[] = array(
'text' => '首页',
'url' => base_path() . 'frontpage',
);
$type = $node->getType();
if($type == 'news'){
$breadcrumb[] = array(
'text' => '新闻',
'url' => base_path() . 'company-news'
);
}
if($type == 'product'){
$breadcrumb[] = array(
'text' => '产品与服务',
'url' => base_path() . 'product-list'
);
}
if($type == 'lesson'){
$breadcrumb[] = array(
'text' => '教育与培训',
'url' => base_path() . 'education-training'
);
}
if($type == 'example'){
$breadcrumb[] = array(
'text' => '解决方案',
'url' => base_path() . 'solutions'
);
}
if($type == 'team_member'){
$breadcrumb[] = array(
'text' => '关于我们',
'url' => base_path() . 'aboutus'
);
$breadcrumb[] = array(
'text' => '管理层介绍',
'url' => base_path() . 'team-member-info'
);
}
$breadcrumb[] = array(
'text' => $node->getTitle(),
);
}
if(!empty($breadcrumb)){
$variables['breadcrumb'] = $breadcrumb;
}
}
另外,天津网站里面的代码:
function tianjin_preprocess_breadcrumb(&$variables) {
//$breadcrumb = $variables['breadcrumb'];
$breadcrumb = array();
$breadcrumb[] = array(
'text' => '首页',
'url' => base_path() . 'frontpage',
);
/*
drupal_set_message(var_export($breadcrumb,true));
if(!empty($breadcrumb) && is_array($breadcrumb)){
$front = array_shift($breadcrumb);
$breadcrumb = array($front);
}
drupal_set_message(var_export($breadcrumb,true));
*/
//$current_path = current_path();
$current_path = \Drupal::service('path.current')->getPath();
if(substr($current_path, 0, 12) == '/articlelist'){
$path_args = explode('/', $current_path);
$term_id = isset($path_args[2]) ? $path_args[2] : null;
}
if(substr($current_path, 0, 11) == '/tongxunlu/'){
$path_args1 = explode('/', $current_path);
$term_id1 = isset($path_args1[2]) ? $path_args1[2] : null;
$term1 = \Drupal::entityManager()->getStorage('taxonomy_term')->load($term_id1);
$breadcrumb[] = array(
'text' => '通讯录',
'url' => base_path() . 'tongxunlu',
);
$breadcrumb[] = array(
'text' => $term1->getName(),
);
}
//drupal_set_message(var_export($current_path,true));
// \Drupal::routeMatch()
$route_name = \Drupal::routeMatch()->getRouteName();
//drupal_set_message(var_export($term_id,true));
$node = \Drupal::routeMatch()->getParameter('node');
if ($node) {
if(isset($node->field_lanmu->target_id)){
$term = $node->field_lanmu->entity;
$tid =null;
$tid = $term->id();
//$term1 = $node->field_lanmu;
//$parent_term = $term->parents[0];
// $parent_term = \TermStorage::loadParents($term->id());
$parent_terms = \Drupal::entityManager()->getStorage('taxonomy_term')->loadParents($term->id());
$ptid =null;
if(!empty($parent_terms)){
$parent_term = array_pop($parent_terms);
if(isset($parent_term)){
$ptid = $parent_term->id();
}
}
//drupal_set_message(var_export($parent_term,true));
//$ptid = $parent_term->id();
// drupal_set_message($ptid);
//党政工团 tid=5
if($ptid == 5){
$breadcrumb[] = array(
'text' => '党政工团',
'url' => base_path() . 'dzgt',
);
$breadcrumb[] = array(
'text' => $term->getName(),
'url' => base_path() . 'articlelist/' . $term->id()
);
}
//行政事务 tid=11
elseif($ptid == 11){
$breadcrumb[] = array(
'text' => '行政事务',
'url' => base_path() . 'xingzheng'
);
$breadcrumb[] = array(
'text' => $term->getName(),
'url' => base_path() . 'articlelist/' . $term->id()
);
}
//业务管理 tid=18
elseif($ptid == 18){
$breadcrumb[] = array(
'text' => '业务管理',
'url' => base_path() . 'yewuguanli'
);
$breadcrumb[] = array(
'text' => $term->getName(),
'url' => base_path() . 'articlelist/' . $term->id()
);
}
//人事培训 tid=23
elseif($ptid == 23){
$breadcrumb[] = array(
'text' => '人事培训',
'url' => base_path() . 'renshipeixun'
);
$breadcrumb[] = array(
'text' => $term->getName(),
'url' => base_path() . 'articlelist/' . $term->id()
);
}
//中心概况 tid=4
elseif($tid == 4){
$breadcrumb[] = array(
'text' => '中心概况',
'url' => base_path() . 'articlelist/4'
);
}
//部门建设 tid=9
elseif($tid == 9){
$breadcrumb[] = array(
'text' => '部门建设',
'url' => base_path() . 'articlelist/9'
);
}
//文化建设 tid=28
elseif($tid == 28){
$breadcrumb[] = array(
'text' => '文化建设',
'url' => base_path() . 'articlelist/28'
);
}
//互动平台 tid=29
elseif($tid == 29){
$breadcrumb[] = array(
'text' => '互动平台',
'url' => base_path() . 'articlelist/29'
);
}
/*
drupal_set_message(var_export($term,true));
$tid = $node->field_lanmu->target_id;
if($tid == 6){
$breadcrumb[] = array(
'text' => '党政工团',
'url' => 'dzgt'
);
$breadcrumb[] = array(
'text' => '党委',
'url' => 'articlelist/6'
);
}
*/
}
/*
$breadcrumb[] = array(
'text' => $node->getTitle(),
);
*/
}
elseif(!empty($term_id)){
//array_pop($breadcrumb);
//drupal_set_message('term_id:' .$term_id);
$parent_terms = \Drupal::entityManager()->getStorage('taxonomy_term')->loadParents($term_id);
$term = \Drupal::entityManager()->getStorage('taxonomy_term')->load($term_id);
$ptid =null;
if(!empty($parent_terms)){
$parent_term = array_pop($parent_terms);
if(isset($parent_term)){
$ptid = $parent_term->id();
}
}
//drupal_set_message(var_export($parent_term,true));
//$ptid = $parent_term->id();
//drupal_set_message('ptid:' .$ptid);
//党政工团 tid=5
if($ptid == 5){
$breadcrumb[] = array(
'text' => '党政工团',
'url' => base_path() . 'dzgt',
);
$breadcrumb[] = array(
'text' => $term->getName(),
//'url' => base_path() . 'articlelist/' . $term->id()
);
}
//行政事务 tid=11
elseif($ptid == 11){
$breadcrumb[] = array(
'text' => '行政事务',
'url' => base_path() . 'xingzheng'
);
$breadcrumb[] = array(
'text' => $term->getName(),
//'url' => base_path() . 'articlelist/' . $term->id()
);
}
//业务管理 tid=18
elseif($ptid == 18){
$breadcrumb[] = array(
'text' => '业务管理',
'url' => base_path() . 'yewuguanli'
);
$breadcrumb[] = array(
'text' => $term->getName(),
//'url' => base_path() . 'articlelist/' . $term->id()
);
}
//人事培训 tid=23
elseif($ptid == 23){
$breadcrumb[] = array(
'text' => '人事培训',
'url' => base_path() . 'renshipeixun'
);
$breadcrumb[] = array(
'text' => $term->getName(),
//'url' => base_path() . 'articlelist/' . $term->id()
);
}
if(empty($ptid)){
$breadcrumb[] = array(
'text' => $term->getName(),
//'url' => base_path() . 'articlelist/' . $term->id()
);
}
}
$variables['breadcrumb'] = $breadcrumb;
//$nid = arg(1);
//drupal_set_message(var_export($nid,true));
//drupal_set_message(var_export($current_path,true));
}
面包屑,就是拼凑一组链接。根据当前路径。
重点代码:
$node = \Drupal::routeMatch()->getParameter('node');
if ($node) {
这段代码,是用来获取当前页面的节点对象的。如果有的话,比如node/8这个页面,就可以获取到。
如果没有的话,返回空。
$breadcrumb[] = array(
'text' => '首页',
'url' => base_path() . 'frontpage',
);
$type = $node->getType();
if($type == 'news'){
$breadcrumb[] = array(
'text' => '新闻',
'url' => base_path() . 'company-news'
);
}
面包屑链接,是一个数组,不断向其追加链接,链接包含text,url两部分。
$node->getType() 获取节点类型属性,根据节点类型的不同进行分别判断。
对于非节点页面,天津里面给出了例子:
$current_path = \Drupal::service('path.current')->getPath();
if(substr($current_path, 0, 12) == '/articlelist'){
$path_args = explode('/', $current_path);
$term_id = isset($path_args[2]) ? $path_args[2] : null;
}
上面的代码,用来获取当前路径。Drupal8里面,没有了arg(1),arg(2)函数。
写法使用:
$path_args = explode('/', $current_path);
$term_id = isset($path_args[2]) ? $path_args[2] : null;
根据当前路径的不同,可以分别设置面包屑。
对于分类页面,或者
节点页面,如果还需要根据分类进行进一步的判断,比如
内容类型:新闻
分类:公司新闻(tid:2),业内新闻(tid:3), 分类字段:field_news_category.
if($type == 'news'){
$breadcrumb[] = array(
'text' => '新闻',
'url' => base_path() . 'company-news'
);
$term = $node->field_lanmu->entity;
$tid = $term->id();
if($tid == 2){
$breadcrumb[] = array(
'text' => '公司新闻',
'url' => base_path() . 'company-news/2'
);
}
elseif($tid == 3){
$breadcrumb[] = array(
'text' => '业内新闻',
'url' => base_path() . 'company-news/3'
);
}
}
根据一个分类tid,获取他的父terms,代码如下:
$parent_terms = \Drupal::entityManager()->getStorage('taxonomy_term')->loadParents($term_id);
$term = \Drupal::entityManager()->getStorage('taxonomy_term')->load($term_id);
$ptid =null;
if(!empty($parent_terms)){
$parent_term = array_pop($parent_terms);
if(isset($parent_term)){
$ptid = $parent_term->id();
}
}
如果分类层级比较多的话,甚至可以循环处理,为$breadcrumb赋值,可能数组需要反转一下。
把这些代码,掌握了以后,Drupal8下面的面包屑,就完全可以解决了。