很多年前剛開始用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是四個子網站的内容類型,頁面例子分别如下:
自由标簽
评论