Manipulating view with php

We encourage users to post events happening in the community to the community events group on https://www.drupal.org.
akanik's picture

I've come up against something that has me stuck.

I've found that in working for a news organization, certain things (most things) need to be precise. For example, the bylines for the stories. When there are multiple authors or multiple publishers, they syntax must be exact. It cannot be writen

By Laurie Snyder, Joanne Hamilton, Courntey Hunter | PublicSource | July 11, 2012

It must be written

By Laurie Snyder, Joanne Hamilton and Courntey Hunter | PublicSource | July 11, 2012

I have accomplished this using the following equation:

if ($vars['field_op_author'][0]['view']) {
    $str_author = t('By') . ' ';
    $i = 0;
    foreach ($vars['field_op_author'] as $author) {
      // check_plain is already run and will happen again with l(), hence the htmlspecialchars_decode
      //$vars['author_count'] = count($author);
      if ($i == count($vars['field_op_author'])-2){
          $str_author .= _openpublish_get_rdfa_author(htmlspecialchars_decode(strip_tags($author['view']), ENT_QUOTES), $author['nid']) . ' and ';
         $i++;
      }elseif ($i == count($vars['field_op_author'])-1){
       $str_author .= _openpublish_get_rdfa_author(htmlspecialchars_decode(strip_tags($author['view']), ENT_QUOTES), $author['nid']);
         $i++;
      }else{
         $str_author .= _openpublish_get_rdfa_author(htmlspecialchars_decode(strip_tags($author['view']), ENT_QUOTES), $author['nid']) . ', ';
        $i++;
          }
    }
    $vars['author_counting'] = $vars['author_count'];
    $vars['authors'] = $str_author;

That is, I count all of the values for the multi-value node reference field_op_author, and depending on their position in the string of values, they are either followed by a comma, an 'and', or nothing. (the rest of it prints the author name as a link to the author node)

This is all well and good for node templates, but how to accomplish this in views!!!

Any and all suggestions are most welcome!

Thanks!

Comments

I find one of the easiest

RobW's picture

I find one of the easiest ways to do something like this is to just have Views use the node template with a special view mode (view mode == display mode, like teaser, full, preview; no relation to views) and then theme that special view mode in a tpl, or to do all of it in a field--field-name.tpl, use preprocessing to do your value manipulating, and then have views output with that field template. Or sometimes a combination of the two.

Other methods...

visuaLatte's picture

There are two methods I can think of. One would be to add the author(s) field as a literal text string, which you could format however you want.

The second-- using your current method of a multi-value node reference field-- might be to check out the Text Formatter module. Using this, you can format multi-value fields as comma-separated with the word "and" before the last value. In order to get this whole string to line up properly with the "|" character between them, you could either (a) use Views, or (b) theme the node--type.tpl.php file in conjunction with Text Formatter. That is, you format the field (in the "Manage display" area) using Text Formatter, and then you concatenate the values of the different fields (e.g. author and date fields) using either Views or a .tpl.php file.