Drupal文档教程翻译 - 在节点中显示完整的词条层级路径

logo-drupal-documentation本站原创编译,转载请给面子,尊重劳动果实,欢迎交流指正。

早上好,仍是炎热的一个早上,昨天的大雨简直就是走走形式,和哀悼日还有地铁里的安检差不多的形式。走你妹啊。继续Drupal文档中文翻译,前面一篇中,我们了解了怎样为节点添加“最后更新时间”,今天的小话题是,怎样在节点中显示当前内容所属的词汇表的完整词条路径。很喜欢代码片段这个大类啊,文字篇幅不长,每一篇都比较有实用价值,拿去就能用的那种,而且针对的需求都很普遍。话说这些很普遍的需求蛮好整合到原生系统中去的。

大家都知道Drupal会默认在节点页面的主要内容下面输出当前内容所包含的词条,也就是我们定义好的分类和tag一类;输出格式比较二,就是一个个的罗列出来,没有区别和层次。

现在呢,我们的需求是将每一个词条都以类似面包屑的形式输出其完整的词汇表路径这样更利于相关内容之间的导航,分类层级关系也更明确。

节点中的词条是由node.tpl.php模板文件中的$terms变量输出的,我们需要在当前主题的template.php文件中添加如下的相应代码,来篡改这个$terms变量。对应三个细化的需求,我们来看下代码。

节点内容属于一个词汇表

<?php
$termtoexclude=arg(2); //figure out what term page you are looking at
$printvocab=FALSE;
$vocabs = array();
foreach( (array)$node->taxonomy as $term ) {
$vocab = taxonomy_vocabulary_load($term->vid); // Changed for Druapl 6
$parents = taxonomy_get_parents_all($term->tid); //get the parents of the current term in the list of terms
if ( !isset($vocabs[$vocab->name]) ) {
$vocabs[$vocab->name] = array();
}
if ($term->tid != $termtoexclude) //make sure the current page term isn't in the list of terms
{
$printvocab=TRUE;
if (count($parents) > 0) //make sure there is at least one level of parents. If not just use the current term
{
$vocabstemp = array(); //need this because taxonomy_get_parents_all starts with the current term and works backwards, so you have to reverse the order.
$vocabsfinal='';
//build your temporary list of parents
foreach ($parents as $parent) {
$vocabstemp[] .= l($parent->name, "taxonomy/term/$parent->tid");
}
$vocabstemp = array_reverse($vocabstemp); //reverse the list of parents so that it makes sense to the human eye. The next "foreach" build the final list of parents, making sure to take out the separator on the last term
foreach ($vocabstemp as $vocabstemp2key => $vocabstemp2) {

if ($vocabstemp2key < count($vocabstemp)-1)
{
$vocabsfinal .= $vocabstemp2.">";
}
else
{
$vocabsfinal .= $vocabstemp2;
}
}
$vocabs[$vocab->name][] = $vocabsfinal;
}
else
{
$vocabs[$vocab->name][] = l($term->name, "taxonomy/term/$term->tid");
}
}
}
//print the term list only if it isn't the current page term.
if ($printvocab) {
foreach ( $vocabs as $name => $termlinks ) {
print '<div class="taxonomy"> See Also: ';
print implode(', ', $termlinks);
print '</div>';
}
}
?>

节点内容属于多个词汇表

<?php
$termtoexclude=arg(2);
//We don't want to repeat the current page's term or vocabulary. The next 3 lines are the setup for this.
$printvocab=FALSE;
$vocabtermtoskip = taxonomy_get_term($termtoexclude);
$vocabtoskip = taxonomy_get_vocabulary($vocabtermtoskip->vid);
$vocabs = array(); //The $vocabs array has changed to accommodate the multiple vocabularies. $vocabs[$vocab->name]['vocab'] contains the name / links to the vocabularies and $vocabs[$vocab->name]['links'][] contains the names / links to the terms of the vocabulary instance. (Yes, you can have more than one instance of the same vocabulary.)
foreach($node->taxonomy as $term )
{
$vocab = taxonomy_vocabulary_load($term->vid); // Changed for Druapl 6
$parents = taxonomy_get_parents_all($term->tid);
if ($vocab->name != $vocabtoskip->name)//If we aren't skipping the current page's vocabulary, we are putting the vocabulary in front of the term path.
{
$vocabs[$vocab->name]['vocab']=l($vocab->name, "taxonomy/vocabulary/$term->vid").">";
}
else
{
$vocabs[$vocab->name]['vocab']="";
}
if ($term->tid != $termtoexclude)
{
$printvocab=TRUE;
if (count($parents) > 0)
{
$vocabstemp = array();
$vocabsfinal='';
foreach ($parents as $parent)
{
$vocabstemp[] .= l($parent->name, "taxonomy/term/$parent->tid");
}
$vocabstemp = array_reverse($vocabstemp);
foreach ($vocabstemp as $vocabstemp2key => $vocabstemp2)
{
if ($vocabstemp2key < count($vocabstemp)-1)
{
$vocabsfinal .= $vocabstemp2.">";
}
else
{
$vocabsfinal .= $vocabstemp2;
}
}
$vocabs[$vocab->name]['links'][] = $vocabsfinal;
}
else
{
$vocabs[$vocab->name]['links'][] = l($term->name, "taxonomy/term/$term->tid");
}
}
}
if ($printvocab) //We are using a string called $termcontent because it was the easiest way to make sure that the commas all fell in the right place.
{
$termcontent = '<div class="taxonomy"> See Also: ';
foreach ($vocabs as $vocabcatkey => $vocabitems)
{
if (count($vocabitems['links'])>0)
{
foreach ($vocabitems['links'] as $vocabkey => $vocablinks)
{
$termcontent .= $vocabitems['vocab'];
$termcontent .= $vocablinks;
$termcontent .= ", ";
}
}
}
$trimmed = rtrim($termcontent,", ");
$trimmed .= '</div>';
print $trimmed;
}?>

节点内容属于多个词汇表,词条有多层继承关系

<?php
$termtoexclude=arg(2);
$printvocab=FALSE;
$vocabtermtoskip = taxonomy_get_term($termtoexclude);
$vocabtoskip = taxonomy_get_vocabulary($vocabtermtoskip->vid);
$vocabs = array();
foreach($node->taxonomy as $term )
{
$vocab = taxonomy_get_vocabulary($term->vid);
$parents = taxonomy_get_parents_all($term->tid);
if ($vocab->name != $vocabtoskip->name)
{
$vocabs[$vocab->name]['vocab']=l($vocab->name, "taxonomy/vocabulary/$term->vid").">";
}
else
{
$vocabs[$vocab->name]['vocab']="";
}
if ($term->tid != $termtoexclude)
{
$printvocab=TRUE;
if (count($parents) > 0)
{
$vocabstemp = array();
$vocabsfinal='';
if ($vocab->hierarchy==2 && count($parents) > 1)
{
$vocabstempmulti = array();
$parentsmulti = taxonomy_get_parents($parents[0]->tid);
foreach ($parentsmulti as $parentmulti)
{
$parentstrees = taxonomy_get_parents_all($parentmulti->tid);
if (count($parentstrees) > 1)
{
foreach ($parentstrees as $parentstree)
{
$vocabstemp[] .= l($parentstree->name, "taxonomy/term/$parentstree->tid");
}
$vocabstemp = array_reverse($vocabstemp);
foreach ($vocabstemp as $vocabstemp2key => $vocabstemp2)
{
if ($vocabstemp2key < count($vocabstemp)-1)
{
$vocabsmultifinal .= $vocabstemp2.">";
}
else
{
$vocabsmultifinal .= $vocabstemp2;
}
}
$vocabstempmulti[] = $vocabsmultifinal;
}
else
{
$parentnumber=$parentstrees[0]->tid;
$vocabstempmulti[] = l($parentstrees[0]->name, "taxonomy/term/$parentnumber");
}
}
$termnumber=$parents[0]->tid;
foreach ($vocabstempmulti as $vocabstempmultikey => $vocabstempmulti2)
{
if ($vocabstempmultikey < count($vocabstempmulti)-1)
{
$vocabsfinal .= $vocabstempmulti2.">".l($parents[0]->name, "taxonomy/term/$termnumber").", ";
}
else
{
$vocabsfinal .= $vocabstempmulti2.">".l($parents[0]->name, "taxonomy/term/$termnumber");
}
}
}
else
{
foreach ($parents as $parent)
{
$vocabstemp[] .= l($parent->name, "taxonomy/term/$parent->tid");
}
$vocabstemp = array_reverse($vocabstemp);
foreach ($vocabstemp as $vocabstemp2key => $vocabstemp2)
{
if ($vocabstemp2key < count($vocabstemp)-1)
{
$vocabsfinal .= $vocabstemp2.">";
}
else
{
$vocabsfinal .= $vocabstemp2;
}
}

}
$vocabs[$vocab->name]['links'][] = $vocabsfinal;
}
else
{
$vocabs[$vocab->name]['links'][] = l($term->name, "taxonomy/term/$term->tid");
}
}
}
if ($printvocab)
{
$termcontent = '<div class="taxonomy"> See Also: ';
foreach ($vocabs as $vocabcatkey => $vocabitems)
{
if (count($vocabitems['links'])>0)
{
foreach ($vocabitems['links'] as $vocabkey => $vocablinks)
{
$termcontent .= $vocabitems['vocab'];
$termcontent .= $vocablinks;
$termcontent .= ", ";
}
}
}
$trimmed = rtrim($termcontent,", ");
$trimmed .= '</div>';
print $trimmed;
}
?>

译者:Viiiix7210

如需转载,请注明官方英文文档及本人译文的出处,谢谢。查看英文原文: Expand term display to show complete paths of related terms


收藏与分享

添加评论


Security code
换一张图