5

I wanted to add #states property to a select list field in cck input form created by content type. I ve tried the following code but the states property is not set to the field. Can anyone suggest on how to do it.

$form['field_ctype']['und']['#ajax']=array('callback' =>'_ajax_menu_check_boxes',
                                              'wrapper' =>'edit-field-sub-menu',
                                            'effect' => 'fade',
                                            'event' => 'change',
                                            );
$form['field_sub_menu']['und']['#states']=array(
                                  'visible' => array(
                                 ':input[name="field_ctype"]' => array('value' => '2')));

could anyone suggest me where I am going wrong. Is it possible to add ajax property ans states property to a same field?

Note : The Ajax property for the field_ctype works fine. Only the states property is not working.
3
  • did you checkout the examples at api.drupal.org/api/drupal/includes%21common.inc/function/…?
    – uwe
    May 24, 2012 at 15:34
  • @MotoTribe I did try the examples module but it didn help me much. I started playing around with states property by going through the examples but it didn work out well for me May 30, 2012 at 10:39
  • Another remark: I wanted to test on integer values like you try to do in your example (':input[name="field_ctype"]' => array('value' => '2')));). It only worked for me, when I was casting the integer to string: ':input[name="field_ctype"]' => array('value' => (string)'2'))); Maybe it will help someone.
    – Alina
    Mar 31, 2014 at 13:05

3 Answers 3

6

Fields always have have nested names like field_something[und][value] or something like that. I guess your query (:input[name="field_ctype"]) is wrong.

I find the easiest way to get it right is using the Firebug/Developer tools Console. Play around with jQuery(':input[...]') until it matches what you're expecting. Also check the name of the form element.

0

Instead of writing custom code, there is a module that provides a simple UI to the States API, Conditional Fields. Although it's currently available only as a dev release, I have used the module on a couple of my sites without trouble.

2
  • I have tried conditional fields as well but it didn suite my requirements. I have 3 select lists and all the values are dynamically populated from a different web service. May 5, 2012 at 10:13
  • Conditional fields doesnt always enforce the required state . I use to to make my fields visible or invisible , but had to add validation in my custom module to make the required fields really be required.
    – Diana
    Aug 7, 2015 at 17:29
-2

The following worked for me in order to show fields depending on the value of a select (which is a taxonomy field). On the code below, the select field is called 'template'. For convinience, the rightside array is the condition to check for and the leftside array is the action to take. This is just another way to write the standard code:

/* This function finds the term id from the term name on the select box */
function get_tid_by_name($name) {
  return db_select('taxonomy_term_data', 't')
    ->fields('t', array('tid'))
    ->condition('name', db_like(trim($name)), 'LIKE')
    ->addTag('term_access')
    ->execute()
  ->fetchField();
}

/* This functions hides fields on the node form of the 'basic page' content type,
 * depending on the selected template 
 */
function MYMODULE_form_alter(&$form, $form_state, $form_id) {
  if ($form_id ==  'basic_page_node_form') {
    $lang = $form['field_slogan']['#language'];
    $template = '#edit-field-template-' . $lang;  // template field selector
    $pp1 = get_tid_by_name ('PP1');
    $pp2 = get_tid_by_name ('PP2');
    $pp7 = get_tid_by_name ('PP7');
    $pp10 = get_tid_by_name ('PP10');
    $form['field_slogan']['#states']['visible'] = array($template=>array('value' => $pp1 ));
    $form['field_image']['#states']['visible'] = array($template=>array('value' => $pp7 )); 
    $form['field_links']['#states']['visible'] = array($template=>array('value' => $pp10 ));
    $form['field_pictures']['#states']['visible'] = array($template=>array('value' => $pp7 )); 
    $form['field_hightlights']['#states']['visible'] = array($template=>array('value' => $pp2 )); 
    $form['field_bullets']['#states']['visible'] = array($template=>array('value' => $pp2 )); 
    $form['field_video']['#states']['visible'] = array($template=>array('value' => $pp2 ));
    $form['field_quick_tip']['#states']['visible'] = array($template=>array('value' => $pp1 )); 
  }
}
2
  • but what does objet_get_tid_by_name do? May 30, 2012 at 10:37
  • @drupal-developer, I've change the function name to get_tid_by_name, added the function and added more comments so that it will be self explanetory.
    – Druvision
    Dec 31, 2012 at 9:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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