2

I have an entity creation form and I have a radio button as a field. Now what I need is, if while adding the entity, if radio button is set, then one more field just below should appear otherwise not.

1 Answer 1

4

If looking for a module, you can use the D7 -dev version of conditional_fields

Define dependencies between fields based on their states and values.

Conditional Fields for Drupal 7 is an user interface to the new States API, plus the ability to modify fields appearance and behavior on certain conditions when viewing content.

Alternatively, you can tweak the forms directly in code using hook_form_alter and the States API

In this example, I show the UPC field when the specified operation value is chosen in a select box:

  // Define available batch operation callbacks
  $operations = array(
    'infinity_batch_sync_products' => 'Import and sync product details',
    'infinity_fetch_products' => 'Fetch list of available products',
    'infinity_fetch_product' => 'Fetch detail on single product'
  );

  $form['operation'] = array(
    '#type' => 'select',
    '#title' => t('Select operation'),
    '#options' => $operations,
    '#required' => TRUE,
  );
  $form['upc'] = array(
    '#type' => 'textfield',
    '#title' => t('UPC'),
    '#description' => t('Enter the product UPC code (commerce product SKU)'),
    '#default_value' => 0,
    '#size' => 20,
    '#maxlength' => 20,
    '#states' => array(
      'visible' => array(
       ':input[name="operation"]' => array('value' => 'infinity_fetch_product'),
      ),
    ),
  );

The "UPC" field only shows when the operation is "infinity_fetch_product"

2
  • no i am not looking for a module. I am writing code and want to achieve this through code.Could you please elaborate a bit on your answer.And i want that additional field during the creating the entity i.e before saving the entity. Sep 29, 2013 at 9:59
  • @neetumorwani answer updated with states api example. Sep 29, 2013 at 10:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.