Hi gang,

New to Drupal and loving it (I was previously a DIY PHP/mysql junkie). I have a question for the Views gurus.

My scenario:
I have a list of nodes I am returning via Views. Two fields needed are [title] and [alternative_title]. [alternative_title] will often be empty, but [title] is always required.

I would like to have views output something like this under the one column:
[title] ([alternative_title])

However, the ( and ) need to be hidden if [alternative_title] is empty.

I have placed [title] first and hidden that field. I then "rewrote" the output of [alternative_title] with the above code example. It works, however, we get empty brackets if the [alternative_title] field is empty. Using the "Empty text" field for [alternative_title] to display only [title] if the [alternative_title] field is empty was no help, because "Empty text" does not interpret fields unlike "Rewrite the output of this field" does. Otherwise that would have been a good option.

Any suggestions for how to do this?

Kind regards,
Jason

Comments

finex’s picture

You should configure the "alternative field" prefix and suffix with " ( " and " )". After doing that you should exclude this field from output and put it before the title field.

At this point you can rewrite the output of the title field and add the [alternative_field] token.

entilza72’s picture

Ha! Wow. I really couldn't see the forest for the trees. I can't believe I didn't see that.

Perfect. Thank you Finex, I feel embarrased now! :-)

Cheers,
Jason

finex’s picture

Don't worry, and good luck with your project :-)

finex’s picture

Don't worry, and good luck with your project :-)

brad.bulger’s picture

Excuse jumping in on this long after the fact, but what prefix where? I have just the same problem but I'm still stuck in the forest.....

finex’s picture

On the field settings

jcarlson34’s picture

Wow I've been looking for this answer for forever, really. Thank you, thank you, thank you, FiNeX!

Brad, Prefix is located in the field setting's REWRITING section:

Add a field to your view and configure it's view options

In the REWRITING section, click the checkbox for "Output this field as a link"

Put the replacement pattern you want in the Link path.

Put the other stuff you want in the Prefix text and/or the Suffix text.

Check Hide if empty (I also check Count the number 0 as empty)

Exclude from display and save.

Add another field and add the replacement pattern from the one above into REWRITING section.

Hope that helps!

solona’s picture

I get the concept, but this isn't working for me.

1) With nothing in the link path, the prefix/suffix don't show.
2) With something in the link path, the entire thing is rendered as a link.

I'm not sure how this worked for you.

jcarlson34’s picture

Which version of Drupal and Views are you using solona?

solona’s picture

Drupal 6 and Views 2.

jcarlson34’s picture

Try stripping the HTML tags option and see if that stops it from rendering as a link.

solona’s picture

Thanks for the suggestion. I got it working. Trying to do something a bit different with jQuery and it works (in browsers that like jQuery anyway).

Kamal Prasad’s picture

So simple a solution but not very apparent. Thanks FiNeX!!

djmmuir’s picture

I have a problem with something I know should be extremely simple. I can get the text version described above to work just fine. What I think should be EASILY done, yet stumps me, is this:

  • A boolean field in a node says something like "Available". Obviously the opposing value represents "Not available".
  • I want to show a graphic (decorator) on the node to represent availability.
  • After many gyrations I have come to believe that the right way to do this is with views, but if I do a rewrite or try to apply the above process to an HTML image tag, it breaks the line because it adds tons of div tags around the different parts of the output.

No matter what I try to substitute with rewrite, it either ALWAYS shows up (even when there is no field value) or it breaks the line. I can get it to work perfectly with plain text.

You'd think Boolean page decorators would be a really common and easy thing to do, but I haven't seen a solution anywhere (without rewriting templates).

gamelodge’s picture

This module may help you, it allows for php.
http://drupal.org/project/views_customfield
Read this thread to get a better idea of usage.
http://drupal.org/node/467190

djmmuir’s picture

Thanks for your response. I installed the module and will commence playing around with it with help from the usage post. So far it seems more painful than it needs to be. :-)

ZenLax’s picture

djmmuir - I am trying to accomplish exactly the same thing now. Were you able to solve this? I'm going to look at views_customfield next, but have just started and hope to save some time. Thanks!

Slovak’s picture

Use the rewrite functionality to set the field's class and rewrite the output to blank. The result will be <div class="field-value"></div> Now you can apply background with CSS (and proper margins for offsets).