很多年前刚开始用Drupal时就记录过《直接调用drupal_set_breadcrumb设置面包屑导航条》,在Drupal 6的时候可以直接在node-abc.tpl.php里面使用这种办法,但Drupal 7以后node--abc.tpl.php里面直接调用drupal_set_breadcrumb就无效了,我就刻意添加了一个block,在block里面使用php来调用drupal_set_breadcrumb,这个办法一直都在使用。
最近在为以前做的一把刀词典系列的网站添加Emoji的时候,也想着把几个站内页添加面包屑,在网上搜了一下,还有一个更合适的办法,就是在自定义模块中调用hook_node_view_alter,在自定义模块cn_18dao.module中的代码如下:
//修改breadcrumb function cn_18dao_node_view_alter(&$build) { $node = $build['#node']; $title = $node->title; $shouzi = mb_substr($title,0,1); $weizi = mb_substr($title,-1); $view_mode = $build['#view_mode']; if( $view_mode == "full" || $view_mode == "amp" || $view_mode == "mip") { switch ($node->type) { case "chengyucidian": $breadcrumb = array(); $breadcrumb[] = l(t('Home'), '<front>'); $breadcrumb[] = l("首字:$shouzi","shouzi/$shouzi"); $breadcrumb[] = l("接龙:$weizi","shouzi/$weizi"); drupal_set_breadcrumb($breadcrumb); break; case "xinhuazidian": if (isset($node->field_bushou['und'][0]['value'])) { $bushou = $node->field_bushou['und'][0]['value']; } if (isset($node->field_bihuashu2['und'][0]['value'])) { $bihuashu2 = $node->field_bihuashu2['und'][0]['value']; } $breadcrumb = array(); $breadcrumb[] = l(t('Home'), '<front>'); if (isset($bushou)) $breadcrumb[] = l("部首:$bushou","bushou/$bushou"); if (isset($bihuashu2)) $breadcrumb[] = l("部首后笔画:$bihuashu2","bushouhoubihua/$bushou/$bihuashu2"); drupal_set_breadcrumb($breadcrumb); break; case "hanyingcidian": if (isset($node->field_fayin['und'][0]['value'])) { $fayin = $node->field_fayin['und'][0]['value']; $shouzimu = substr($fayin,0,1); } $breadcrumb = array(); $breadcrumb[] = l(t('Home'), '<front>'); if (isset($shouzimu)) $breadcrumb[] = l("首字母:$shouzimu","shouzimu/$shouzimu"); drupal_set_breadcrumb($breadcrumb); break; case "yinghancidian": if (isset($node->field_yingwen['und'][0]['value'])) { $yingwen = $node->field_yingwen['und'][0]['value']; $shouzimu = substr($yingwen,0,1); } $breadcrumb = array(); $breadcrumb[] = l(t('Home'), '<front>'); if (isset($shouzimu)) $breadcrumb[] = l("首字母:$shouzimu","shouzimu/$shouzimu"); drupal_set_breadcrumb($breadcrumb); break; default: } } }
这其实与专门添加一个block也类似,只是不需要专门添加一个block,程序看上去也更清晰。
上面4个case是四个子网站的内容类型,页面例子分别如下:
自由标签
评论