开发者

How can I create a comma separated list for multiple node references in Drupal 6?

I have an Author content_type that I'm switching out for username in articles. The problem is that a story can have multiple authors, and the following code in template.php will only generate the first author. How can I get this to output to be readable as "By John Smith, Jane Doe and Mary Poppins | Feb 19, 2010"?

function mytheme_date($node) {
  return t('By !username | !datetime',
  array(
    '!username' => t($node->field_author[0][view]),
    '!datetime' => t($node->field_publish_date[0][开发者_如何学Pythonview]),
  ));
}

Please bear in mind that I'm also wanting to use this for views too, so that a node reference will output correctly there too.

Thanks, Steve


Assuming that the example you've given works to output the first author, the most direct solution to get all the authors in a comma-sperated list would be the following:

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

Then you'd output $author_list in place of $node->field_author[0][view]

A more "Drupal" way of doing it would be to copy modules/cck/theme/content-field.tpl.php to your theme directory, then make a copy of it named content-field-field_author.tpl.php as well. You can then make changes to the new file which would override how the values are displayed for the "author" field specifically. Then you could output themed field_author value wherever you want in a custom node-[node_type].tpl.php file. (You may need to clear cached data via the button on Administer > Site configuration > Performance for the custom templates to be loaded the first time.)

If your view's "Row style" is set to "Node" then it will use the node and field templates as well. If you have it set to "Fields" then you'll need to theme the field in the view separately. See your view's "Theme: Information" for views templates you can override in your theme.

Edit:
Missed the fact that you wanted a natural language list. That takes a bit more, but here's a Drupalized function that will do just that:

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;
}

Then, of course, use the following in place of the last line my first code block above:
$author_list = implode_language($authors);


What about using Author Taxonomy plugin?

Author Taxonomy allows you to assign one or more authors to a node using terms from a taxonomy of author names. This module also provides a fully themable replacement for your byline (the "submitted by Username on date" text).

Displays author names in a serialized manner: "Jane and Jack" or "Jane, Jack, and Joe."

Check Multiple authors tutorial if you need a different approach.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜