0

// sample code
/**
 * Implementation of hook_form_FORM_ID_alter()
 */
function demo_form_sport_node_form_alter(&$form, &$form_state, $form_id) {
    $form['field_winter_sports'] = array(
    '#states' => array(
        'visible' => array(   // action to take.
            ':input[name=field_season]' => array('value' => t('winter')),
        ),
    ),
);

}

i am trying to alter a form from a "sport" node type. my "sport" node type has 2 fields.

  1. field_season List (text) Check boxes/radio buttons
  2. field_winter_sports List (text) Check boxes/radio buttons

there are two options in field_season, summer or winter.

if someone selects winter in field_season, i want to make field_winter_sports visible, otherwise remain hidden.

with my current code, when i select winter right now, nothing happens.

  • 1
    You're overwriting the field completely with that code, could you explain how you're still able to see the select list at that point? It shouldn't be in the form if that's really the code you're using – Clive Sep 8 '13 at 11:12
2

There are two things to consider:

  • The name of the input element is visible in the HTML output of the page, and that could not match with the ID used for that form element from the form API. That happens with form elements created with the field API too.
    I created a field_season field, which allows just a value to be select (which means it is rendered with radio buttons); the input element name that appears in the HTML output, which is the one jQuery sees, is field_season[und], not field_season.

    screenshot

    screenshot

If I change the field to allow three choices (which means the form field is rendered as checkboxes), the input element name changes to field_season[und][1], field_season[und][2], and field_season[und][3].

screenshot

screenshot

  • When you want to tell to Drupal to alter the state of a form element when a radio buttons are used, you should not use t(), and the string to use as value in the state is not the string that appears to the users, but the value assigned to the value attribute of the <input type="radio"> tag. For example, for the form element added from the following code, those values are 0, 1, and 2.

    $form['registration_cancellation']['user_register'] = array(
      '#type' => 'radios',
      '#title' => t('Who can register accounts?'),
      '#default_value' => variable_get('user_register', USER_REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL),
      '#options' => array(
        USER_REGISTER_ADMINISTRATORS_ONLY => t('Administrators only'),
        USER_REGISTER_VISITORS => t('Visitors'),
        USER_REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL => t('Visitors, but administrator approval is required'),
      ),
    );
    

    screenshot

Since the input element name contains und, which is the string value for the LANGUAGE_NONE constant, I suspect the input element name could change basing on the language associated with the field, even if I suspect it would always be und, for radio buttons and checkboxes.

As side note, learningxm is correct when it says that you should use `:input[name=""]' since that is what the example in the drupal_process_states() page shows.

$form['toggle_me'] = array(
  '#type' => 'checkbox',
  '#title' => t('Tick this box to type'),
);
$form['settings'] = array(
  '#type' => 'textfield',
  '#states' => array(
    // Only show this field when the 'toggle_me' checkbox is enabled.
    'visible' => array(
      ':input[name="toggle_me"]' => array('checked' => TRUE),
    ),
  ),
);

As noted by Clive, you are overriding the form element added using the field API; to change the states associated with the form element, you should just use the following code.

$form['field_winter_sports']['#states'] = array(/** Add here your states. **/);

I assume field_winter_sports is the correct form element ID.

I would also investigate using hook_field_widget_form_alter(), which is the hook thought to alter the form elements used for a field.

-2

change ':input[name=field_season]' => array('value' => t('winter')),

add commas "" to field_season like this:

':input[name="field_season"]' => array('value' => t('winter')), 
  • adding "" did not work. – Rockitdev Sep 8 '13 at 3:33

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.