Friday, December 21, 2012

How to display one of two possible Drupal fields in a node template (hint: it's easier then you think!)

Yesterday at work, I was asked to set up a field in a node template panel that would display a field called 'story' if it had content, but default back to the nodes' 'body' field if the 'story' field was empty.  This is possible using views, but it was going to require an argument to be passed from the panel to the view to allow the view to only display the content from the node being viewed. (Or edited)  In theory, I knew what to do, but I did a little research anyway, as I like to do, and I ran across this excellent post: http://blog.urbaninsight.com/2012/05/14/how-to-conditionally-display-value-from-two-fields-in-views

That confirmed my plan for the fields, but I also needed to pass an argument from the panel to the view.  This, in the end, is how I set it up:

(I am assuming here that your content type already contains *both* fields that you are trying to conflate.  I am also assuming that they are both text fields, if one of them is something fancy, like a user reference field, you will need to use a relationship to be able to add the field into your view.)

Create a view, name it, select the content type that you will be displaying fields from.  Do not create a page or a block.  Click Continue & Edit.  Click the +Add button at the top of the page, and add a content pane.  This is important.  It needs to be a content pane.  Content pane.  Got it?  Good.  Now add your body field.  In its settings, select, Exclude from display.  Now add your story field.  The order is important.  I promise.  In the story field settings, expand the 'No results behavior' section.  In the 'No results text box', you are going to put the replacement pattern for your field.  If you don't happen to know it, in the 'Rewrite the output of this field' section, there is an expandable list of replacement patterns, in which you will be able to find your previous field.  This is why the order is important.  You cannot rewrite a field in a view with content from a later field.  At any rate, the text that I needed in my 'No results text' box were this:
[body]

That's it. The Body field will display if there is text in it, and the Story field will display instead if the Body field is empty.  Bada-bing, bada-boom. (Don't forget to save your view)

Now, how, you ask, does one set the view to only display the field from the node being viewed.  Well, in my case, I passed an argument from the panel to the view content pane, like so:

Add a contextual filter: Content:Nid
Under Pane settings, click on edit for the Argument input.  Select 'Input on pane config'.
Now go to your panel, in my case, the node variant for that content type, and add your view.  (It will be in the View panes catagory.) The only setting that will be available for the field will be the argument text field.  In it (at the advice from a much wiser colleague) I put '&node:nid'.  This is very specific.  Save the panel, and you should be good to go.


No comments:

Post a Comment