1

Trying to make the second field only visible when there is a value in the first controller field. If nothing is selected in the initial load of the screen, the second field should be hidden.

Because the controller api_title is a sub array I'm not sure how to set the comparison value in condition name='api_title'

$form['api_title_filter_widget'] = array('#tree' => true, '#weight' => '10');
$form['api_title_filter_widget']['api_title'] = array(
        '#type' => 'select',
        '#title' => 'API Title',
        '#default_value' => $api,
        '#options' => ["" => "- Select -"] + $published_api_proxies,
        '#ajax' => array(
            'event' => 'change',
            'callback' => 'callback_api_product_filter',
            'method' => 'replace',
            'wrapper' => 'api_products_controlled_by_filter',
        ),
    );

$form['api_product']['#states'] = array(                                              
                                   'visible' => array(   
                                        '**select[name="api_title"]' =>** 
                                          array('!value' => ''),  
                                            ),
                                         );       
1

Maybe you can try to add an id to your select field

$form['api_title_filter_widget']['api_title'] = [
  '#type' => 'select',
  '#title' => 'API Title',
  '#default_value' => $api,
  '#options' => ["" => "- Select -"] + $published_api_proxies,
  '#ajax' => [
    'event' => 'change',
    'callback' => 'callback_api_product_filter',
    'method' => 'replace',
    'wrapper' => 'api_products_controlled_by_filter',
  ],
  '#attributes' => [
    'id' => 'my_api_title'
  ],
];

Then you should be able to target it through its id :

$form['api_product']['#states'] = [
  'visible' => [
    ':select[id="my_api_title"]' => ['filled' => TRUE],
  ],
];
2
  • Good news is that adding '#attributes' => [ 'id' => 'my_api_title' hid the field but now the ajax call in the controller doesn't work. – dfish17 Jun 2 '17 at 2:35
  • There are two issues with #states and AJAX drupal.org/node/1091852 and drupal.org/node/2226405 – 4k4 Jun 2 '17 at 7:24

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.