1

I have a content type with a bunch of fields attached. When displaying the node edit form I want to hide or display one field ( using $form['some_field']['#access'] = FALSE; ) if another field has a specific value.

I am using hook_form_alter() and would expect to find existing field values within $form_state['values'] but this key isn't present even though the form fields do contain existing previously saved field values.

What am I missing ?

Thanks

2

In node edit form $form_state['node'] contains full node object , from which will get the existing field values.

0

As far as I know $form_state['values'] is for the submitted data, I'm not completely sure if it'll be present if some invalid data is submitted.

Check the $form array instead for the default value of the desired field, then use the states to add some behavior.

0

Instead of trying with new code you can try with Conditional Fields module

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.

or

if you still want try with code then you can use ajax wrappers with change events in this way

  $form['some_field']=array(
                '#type' => 'select',
                '#title' => 'location',
                '#options' => array(1,2,3),
                '#default_value' => 1,
                '#description' => t('select your news location.'),
                '#ajax' => array(
                        'event' => 'change',
                        'callback' => 'some_call_back_function',                            
                        'wrapper' => 'city-term',
                        'method' => 'replace',
                        'effect' => 'fade',
                ),
                );

and with callback function you can rebuild form with form rebuild.

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.