A sample display of the address view and visibility selector on the node form.A sample display of the address view and visibility selector on the node form.

In the course of working with Views, we often would like to provide conditional checks for display on a field per row. For example, imagine a directory site that allows users to opt out of displaying their address. In older implementations we’ve achieved this functionality through php in a field or in templates.

Fortunately, the current versions of Views include a quick solution with flexible implementation!

This solution requires that you have Views (Well, yeah.) and CCK (Always for D6, depends on method for D7)

For our directory example, the basic method is as follows:

  1. Add address field(s) to Content Type or Entity.
  2. Add control field (e.g. “Opt in”) to Content type or Entity.

A sample display of the address view and visibility selector on the node form.

  1. Create a View to display the content.
  2. Add the address field(s) to the view and mark “Do not display”
  3. Add the control field to the view and choose to rewrite.
  4. Add your address field(s) token(s) to the rewrite field.
  5. In “No Results Behavior,” select “Hide rewriting if empty” (Views 3)/“Do not rewrite if empty” (Views 2) and “Count the number 0 as empty.

Highlighted display of the rewrite settings necessary for hiding information.

  1. Done! (sorta...)

Sample results showing one item with content and the other without.

The trick is that your control field needs to return a value of ‘0’ if you want the content hidden.

In Drupal 6 the easiest path is to create an integer on the content type and choose “Single on/off checkbox” as the form element. You’ll need to specify allowed values of ‘0’ and ‘1’ and since this type of field doesn’t display a label it’s best to include a key|value pair for the ‘on’ value; for this example something like “1|Opt in” is probably best.

Initial CCK field settings for integer with Single On/off checkbox display. Sample setup for integer with Single On/off checkbox display.

The above works in Drupal 7 so long as CCK is installed and enabled (to provide the “Single on/off checkbox” element), but the Entity system gives us some other options for providing the value. One quick example uses Taxonomy:

  1. Create a Vocabulary with two terms, "Hidden" and "Display"
  2. Create a new integer field for the Vocabulary.
  3. Assign the terms Titles that correspond with the values you want to provide, e.g. “Hidden” has a ‘Value’ of ‘0’ and “Display” has a Value of ‘1’.
  4. Create a new taxonomy field on the entity containing the field you want to display.
  5. Create the basic view as above.
  6. Instead of adding the control field directly, you'll first need to add a relationship to the taxonomy field you added to the entity.

Views settings for using a taxonomy term as the control field.

  1. Now add the integer field with the relationship set.
  2. Setup the rewrite on the integer field.
  3. Done!

This type of solution is a little trickier to set up in views, but it's great if you want to require the user to make a conscious choice because we can make selecting a term required to submit the content without a default behavior (unlike the Single on/off checkbox).

This is just a basic little trick that we stumbled across recently and we hope it’ll be useful for someone else. If it helped solve a problem you’re finding or you’ve got a particularly rad implementation, let us know in the comments below!


jQuery UI can do all these stuffs easily. Check this out at their website, their demo are given there and also you can download the total UI package...
o2 barrier


Saved me from doin yet another template-overrride..


Yeah, exactly what i was searching for. But instead of creating a visible/invisible field, i use relation module to spacify the relation between nodes, and boolean checkboxes to specify wich fields have to be mentionned in the views regarding to the relation.
What i really love in your solution is that it's not module dependent...
Thanks !

Add new comment

The content of this field is kept private and will not be shown publicly.

Filtered HTML

  • Web page addresses and email addresses turn into links automatically.
  • Allowed HTML tags: <a href hreflang> <em> <strong> <blockquote cite> <cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h1> <h2 id> <h3 id> <h4 id> <h5 id> <p> <br> <img src alt height width>
  • Lines and paragraphs break automatically.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.