How to Display One Value from Two Fields in a Drupal View

How to Display One Value from Two Fields in a Drupal View

Ki Kim's picture

There is a situation where one field of a Drupal View needs to pull its content from two fields (aka cck fields) of a node. In my case, they were default author field and a custom field called contributor. This example uses Drupal 7 and Views 3.

If optional the contributor field was not empty, it would display the contributor and skip the author field. If there was no contributor, then the author, which cannot be optional, would be displayed by default.

Add two fields; order matters

This conditional display can be achieved with the View's configuration interface.

First, add those two fields in the Fields section in the View's edit mode; author first, then contributor. Note that in Views 3, author's name can be accessed only after a relationship for author is established (Philosophy of include-it-only-when-you-need-it).

Configure Author field

Enter the configuration for the author and check the checkbox for "Exclude from display".

Configure Contributor field

Now configure settings for the contributor field. The trick is to configure the "No results behavior" section. Enter "[name]" in "No results text".

With this logic, author will be displayed only if the contributor field is empty. Otherwise the contributor will be displayed and not the author. This method could be applied to three or more related fields as well.

Since Views allows you to pull fields from more than one content type, these related fields need not come from the same content type (aka bundle in D7 lingo).

Comments

I had to do this dos for a list of articles that had images and video. The requirement was to have a list with a thumbnail of the image as default with the trimmed body floated next to it, but if the node also had video, we had to show the thumbnail of the video instead. I did this using a field template, but this approach would have been far easier and more maintainable.

That's a great tip! I've been using Views 2 a lot recently for a project and needed exactly this logic but had to resort to the customfield (php) module to handle this. I wish Views 2 had the "no results behavior" option.

I had no idea this was an option.

For those looking for a solution in Views 2, a colleague wrote a small module called Views If Empty that adds this very functionality. He hasn't done a point release, but I've used it on a few production sites and it's stable:
http://drupal.org/project/views_ifempty

Can't you just put the token in the empty text field in views 2?

thanks for the nice tip. was searching for some like that, but didnt realize the relationship.

Post new comment

About Urban Insight

We create elegant, mobile-friendly websites.
We solve complex problems using Drupal and open source software.
Learn More

Snippet

http://fortawesome.github.com/Font-Awesome

This is really clever. "Font Awesome" is a new icon set meant for use with Twitter Bootstrap. You still call the icons the same way (ex. <i class="icon-flag"></i>), but behind the scenes its using a pictographic webfont instead of image sprites. Since fonts are vector, this means you can make your icons as big or small as you want.