代码之家  ›  专栏  ›  技术社区  ›  Steve Perks

如何在Drupal6中为多个节点引用创建一个逗号分隔的列表?

  •  0
  • Steve Perks  · 技术社区  · 16 年前

    我有一个作者内容类型,我将在文章中切换为用户名。问题是一个故事可以有多个作者,template.php中的以下代码将只生成第一个作者。我怎样才能把这个输出为“由约翰·史密斯,简·多伊和玛丽·波宾斯于2010年2月19日”的可读性呢?

    function mytheme_date($node) {
      return t('By !username | !datetime',
      array(
        '!username' => t($node->field_author[0][view]),
        '!datetime' => t($node->field_publish_date[0][view]),
      ));
    }
    

    请记住,我也希望将它用于视图,这样节点引用也将在那里正确输出。

    谢谢, 史蒂夫

    2 回复  |  直到 16 年前
        1
  •  3
  •   ashtonium    16 年前

    假设您给出的示例用于输出第一个作者,则在逗号分隔的列表中获取所有作者的最直接解决方案如下:

    foreach($node->field_author as $author) {
      $authors[] = $author[view];
    }
    $author_list = implode(', ', $authors);
    

    然后输出 $author_list 代替 $node->field_author[0][view]

    一种更“drupal”的方法是将modules/cck/theme/content-field.tpl.php复制到主题目录中,然后复制一个名为 content-field-field_author.tpl.php 也。然后,您可以对新文件进行更改,这将覆盖“author”字段的值显示方式。然后,您可以在自定义节点中输出主题字段“author value”的任何位置-[节点类型].tpl.php文件。(您可能需要通过上的按钮清除缓存数据 管理网站配置性能 以便第一次加载自定义模板。)

    如果视图的“行样式”设置为“节点”,那么它也将使用节点和字段模板。如果将其设置为“字段”,则需要在视图中单独设置该字段的主题。有关可以在主题中替代的视图模板,请参见视图的“主题:信息”。

    编辑:
    错过了你想要一个自然语言列表的事实。这需要更多,但这里有一个drupalized函数可以做到:

    function implode_language($array = array()) {
      $language_string = '';
      if (count($array)) {
        // get the last element    
        $last = array_pop($array);
        // create a natural language list of elements if there are more than one
        if (count($array)) {
          $language_string = implode(', ', $array) .' '. t('and') .' '. $last;
        }
        else {
          $language_string = $last;
        }
      }
      return $language_string;
    }
    

    然后,当然,使用下面的代码来代替上面第一个代码块的最后一行:
    $author_list = implode_language($authors);

        2
  •  0
  •   GmonC    16 年前

    用什么呢 Author Taxonomy 插件?

    作者分类法允许您分配 节点的一个或多个作者使用 作者姓名分类中的术语。 本模块还提供了 可替代您的副线 (用户名提交日期) 课文)。

    在序列化中显示作者名称 态度:“简和杰克”或“简, 杰克和乔。”

    检查 Multiple authors tutorial 如果你需要不同的方法。