Community Documentation

Conditional Fields

Last updated August 29, 2011. Created by peterpoe on May 28, 2009.
Edited by GoddamnNoise, mlncn. Log in to edit this page.

This documentation applies only to Conditional Fields for Drupal 6. The D7 version is in development and still undocumented, with the exception of upgrade instructions (see below).

Content fields and groups visibility based on the values of user defined "controlling" fields.

Overview

The Conditional Fields module allows set fields with allowed values as "controlling fields" for other fields and groups. When a field or group is "controlled", it will only be available for editing and displayed if the selected values of the controlling field match the "trigger values" assigned to it. You can, for example, make a custom "article teaser" field that is shown only if a "Has teaser" checkbox is checked.

When editing a node, the controlled fields are dynamically shown and hidden with javascript.

On node view, the controlled fields which were left untriggered are hidden.

Upgrading from Drupal 6 to Drupal 7

Read carefully these instructions since taking the wrong steps could lead to loss of dependencies data!

Note: Field groups dependencies will not be upgraded, since Field group is still not supported by Conditional Fields 3

  1. Before upgrading, ensure that you have the latest stable version of Conditional Fields for Drupal 6 installed and working.
  2. Follow closely the instructions on the Drupal 6 to Drupal 7 upgrade process.
  3. Most importantly, you have to migrate your old CCK fields to the new format BEFORE updating Conditional Fields, so do not omit step 14. Upgrade fields! Failing to do so will give an error when trying to run the subsequent update on step 15. Update contrib modules and themes and could lead to loss of dependencies data.
  4. After step 14, leave the Content Migrate module activated. You can safely disable it after step 15.
  5. Note that Content Migrate in certain cases changes the allowed values of fields: you will have to manually edit the dependencies to match the new allowed values if this happens.

Dependencies

  • Drupal core: 6.x or 5.x (note that the 5.x branch of Conditional Fields is very outdated, while the 6.x branch is actively maintained).
  • Content Construction Kit (tested up to version 2.6) -> http://drupal.org/project/cck.
  • OPTIONAL: Fieldgroups (included in CCK) > If enabled you can also set groups as controlled fields.

Installation

  1. Copy the unpacked folder "conditional_fields" in your modules folder (usually [base_path]/sites/all/modules).
  2. Go to the modules administration page (admin/build/modules) and activate the module (you will find it in the "CCK" package)
  3. Assign the "Administer conditional fields" permission to the desired user roles.

Usage instructions

(for Conditional Fields 2)

Field configuration

Once the module is activated, a new set of options will appear in the editing form of every cck field. The path is admin/content/node-type/CONTENT_TYPE_NAME/fields/FIELD_NAME, where the options appear in a collapsed fieldset after the "Default value" fieldset. Note that the options will not appear if there are no candidate "controlling" fields, as explained below.

If there are candidate controlling fields you can select which of the allowed values of those fields will make the field "controlled". If - Not controlling - or no value is selected, the field will be shown as usual.

These are the requisites to make a field controllable:

  • The controlling field widget must be single or multiple select list, radio buttons, checkboxes, text field or text area.
  • The controlling field must have allowed values.
  • If a group is controlled, fields inside it can be controlled only by other fields in the same group.

Example:

- Field A
- Field B
- Group C
    - Field D
    - Field E

We assume that A and D have allowed values and are select lists, radio buttons, or checkboxes. B and E are text fields with no allowed values.

A can control B, C, D and E.

D can control A, B and E.

Caveat: if A controls Group C, then D can only control E (and, vice versa, E can only be controlled by D).

Content type settings

There is a "Conditional fields" tab in every content type admin page (the path is admin/content/node-type/CONTENT_TYPE_NAME/conditional) with the following options:

  • User Interface options
    • Javascript: You can decide if you want to use javascript to dynamically disable (grey out) or hide the fields when editing a node.
    • Animation: There are three animations currently available: show/hide (default), slide down/slide up, and fade in/fade out. You can also set the speed of the animation.
  • Orphaned controlled fields settings
    These settings control the visibility (on node view) and editability (on node edit) of controlled fields when the controlling fields are not visible (e.g.: set to 'Hidden' in the fields display settings) or not editable (e.g.: by an access control module).
  • Reset untriggered fields to default values.
    When unchecked, untriggered controlled fields are not reset to their default values when saving a node.
  • Administrators see all fields
    If checked, users with 'administer conditional fields' permission will see all controlled fields of a node, even if the weren't triggered.
  • Reset
    If checked, all conditional fields configurations for this content type will be reset (though the fields themselves will remain untouched).

Rules integration

When the module is activated, a new condition is available in Rules: "Controlled field is triggered".

Limitations and incompatibilities

  • Supported controlling fields are for now only core CCK widgets (that is: select, checkbox, radio and text). Other field types might work but are not supported: there are confirmed incompatibilities with non-core CCK modules (Date, Multigroup, etc). A full plugin system for compatibility will be implemented in the next major version. Follow this issue for ongoing development.
  • Single on/off checkboxes as controlling fields only work with the "on" value. So, you can't show a field when a single checkbox is "off". See the related issue.

Comments

i found this comment of Peter helpful:

peterpoe - February 2, 2010 - 14:21
The conditional fields settings in the field edit page will appear only if there are other fields in the content type that can control the
Fields with "allowed values" in the same group (or outside any group if the edited field is outside too) should be available.

Check this and if nothing happens provide an export of your content type definition please

Joel Box - Mondial-IT

I've tried searching the web and the Drupal site here but I'm not understanding how to set up a conditional based form that appears as content within a node.

Does anyone know of a good reference that could walk me through setting up something simple like selecting A or B and if B selected unhide a C field?

I'm also wondering if conditional formatting is the best way to implement something like a simple diagnostic workflow. Any help would be appreciated.

Not for use with Single On/Off Checkbox Widget

Until the #360824: Option to negate trigger values and #718138: Per field settings (instead of per content type) issues get implemented, suffice it to say you can't use the Javascript options of a conditional field if the controller field uses a Single On?Off Checkbox Widget. This is actually discussed in #759634: How does it work? Need assistance but it is easy to miss from the title.

My solution (not pretty) was to switch the Widget for the controller field to a radio button and it worked as advertised. [Edited to add] I did make my solution a bit less ugly by switching to two very short results (Yes/No) and using the Multi-column checkboxes radios module (http://drupal.org/project/multicolumncheckboxesradios)

Thanks! You saved my day. Also note...

Great module; I spent a whole day trying to code custom forms with ajax. My searches also did not yield much as I forgot to add the 'conditional' keyword but I came upon a solution in this

For anyone planning to use this module, please note the following tips that may not be so obvious initially:
- For two fields to be controller-controlled or vise-versa, they need to be in the same Group
- A field shall not have the 'Conditional fields settings' link enabled in it's edit form if it is the only field in its group

that would be great

if taxonomy was part of the possible conditionning fields.
nice module so far, thanks

and..

..that a user reference field -and some other specific cck fields I suppose- could be part of conditionned ones.
hum right, said nothing : use groups :)

the missing link

I set this up and it worked great. I used a select list that is populated by a database call to produce my topic list, and then made a number of subtopic fields also doing database calls in php to get the subtopic list. Works well.

BUT

I can't help but think I"m missing something. Is there no way to pass the topic search variable into my subtopic php so that I only have to make one subtopic list? I went through the code and could not even identify the variable itself from the form. I'm also new to php, so things got blurry quickly.

I appreciate any suggestions that are out there.

How the forms will be processed?

How does the output of the form will be processed? Does the server will load all the fields and hide it using javascript or AJAX based?

The fields are hidden using

The fields are hidden using Javascript, there is no Ajax involved.

Page status

No known problems

Log in to edit this page

About this page

Drupal version
Drupal 5.x, Drupal 6.x, Drupal 7.x
Audience
Developers and coders, Site administrators
Drupal’s online documentation is © 2000-2012 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License.