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.


Thursday, December 13, 2012

Drupal 101: Lesson 1

Ok boys and girls, here begins Drupal 101!

:)

First assignment:

  • Get a notebook to use for this "class".  You will be writing down passwords and user id's that you will need to have later, and it will be very annoying to have forgotten. ;)
  • Now go to:  https://docs.acquia.com/user/register
  • Create an account.  You are not buying anything, I promise. ;) Write down the user id and password
  • Now go to: https://www.acquia.com/downloads  and download the Dev Desktop Drupal 7 package.  This is free, Drupal is open source, and you should not ever have to pay for any of its components. 
  • There are installation instructions at this link: https://docs.acquia.com/dev-desktop/install
  • Write down anything that you have to fill out during the installation process.  User ID, email, password, Database name, etc...
In my opinion, and after trying out several different setups, I've decided that Acquia is by far the easiest way to get started with a local development environment. I don't work for them, and I am not affiliated with them in any way, for what it is worth. 

* I will not ever tell you to download anything that has to be paid for!!!  Everything that you will be using is open source, i.e. FREE!

EXTRA CREDIT

  • Open Terminal (Applications->Utitlities->Terminal)
  •  Type drush status 
    • What do you see? (answer is at the bottom of the page)

EXTRA EXTRA CREDIT

  • Read this: http://soundpostmedia.com/article/5-reasons-youll-love-using-drush-drupal
  • Watch this: http://www.leveltendesign.com/tutorial/video/drupal-7-overview

ANSWERS:

You should see something that looks like this:

 NAME OF YOUR COMPUTER:~ nameofyouruser$ drush status
 PHP configuration     :  /Applications/acquia-drupal/php5_3/bin/php.ini
 Drush version         :  5.7                                           
 Drush configuration   :

Getting started with Drupal

Drupal is a wonderful thing.  If you are reading this, you probably already have some idea of what it is, and are interested in actually learning how to use it. If you don't know what Drupal is, and wish to, here is an excellent round-up of resources:

http://drupal.ucdavis.edu/blogs/triskal/2009/04/13/how-learn-drupal

There are *tons* of tutorials out there already, mostly screencasts.  Screencasts are awesome, I've watched many of them.  However, I found them (initially) to be less helpful then written instructions.  It is possible to find out how to do almost anything with Drupal, via a simple google search.  The trick is knowing what exactly you need to learn to do, and when you need to learn it.  I came to Drupal without any background in programming, css (or anything useful, really).  What I wanted to find was a series of posts that laid out, in detail, the steps that one needed to take to get up and running with Drupal.  (Spoiler alert: I couldn't)

I couldn't find one.  (I hope I didn't ruin that for you with the spoiler) Now that I have a (small and tenuous) grasp on building a website with Drupal, I am going to make one. (Not to mention that my Aunt asked me to help her get started with Drupal, so I've already written most of this stuff up in a series of e-mails. I'm always in favor of getting the most out of my work...)

Here are a few caveats:
  • I work on a Mac, and this will all be Mac specific information.  I don't have anything against PC's, but not all of this stuff will be the same on a PC.
  • All of this will be D7 specific.
  • I am in love with the concept of using Panels Everywhere, and will therefore be gearing all of the site setup towards using that.
  • I am not an expert!!  It is entirely possible that something I write will be less then 100% correct.  If it is, and you realize it, PLEASE mention that in the comments, and I will fix the error.
  • I like bullet points.  I will probably be overusing them.
  • Because I wrote this up as a series of back-and-forth e-mails, there are often questions in a section.  I will post the answers at the end of a blog post for any questions that are in the post.
Thus ends the introduction.  Stay tuned for the first lesson!