views:

213

answers:

2

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][view]),
  ));
}

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

A: 

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.

GmonC
I looked at this, but the problem is that all data imports, view structure etc. have already taken place and I don't have the luxury atm.
Steve Perks
Sorry about that. I think @ashtonium's answer is better fitted to your problem... Good luck!
GmonC
+2  A: 

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

ashtonium
Thanks ashtonium, How would I go about getting the "John, Jane and Mary" or "John and Mary", etc?
Steve Perks
I missed the fact that you wanted a natural language list, I've added to my answer to include this. Does that fully answer your question?
ashtonium
Thank you very much ashtonium. I had another solution going after attempting it myself, but this is a little more elegant.
Steve Perks