2

In a content type, I want to have two select fields eg:

Select list 1 contains the Mobile phone brands names

Nokia
Samsung
HTC
Apple

Select list 2 contains the phone type like:

Lumia 520
Lumia 620
Galaxy S3
Galaxy S4
Hero
One
iPhone 4
iPhone 5

While adding new content, I want to make a Hierarchical select in this case, When a user selects Nokia from Select 1, then only Nokia phones are available for selection in Select 2 like:

Lumia 520
Lumia 620

But if nothing is selected in Select 1, then Select 2 doesn't show any options but still visible.

So far I tried Hierarchical select Module, but works only when I put the taxonomy term in hierarchical form instead of lists like:

Nokia
    Lumia 520
    Lumia 620

Samsung
    Galaxy S3
    Galaxy S4

HTC
    Hero
    One

Apple
    iPhone 4
    iPhone 5

and it only stores one value in the database (the phone Name). I want to be able to store both Brand and Phone name in Database as fields, so, I could display both fields using Views and also have exposed filter to filter through this.

Any suggestions how should I approach ?

4
  • Please don't use code blocks for things that are not code :) How is your system supposed to know Lumias are from Nokia?
    – Mołot
    Nov 20, 2013 at 10:48
  • Good question actually, I am speechless, may be 2 select lists doesn't work together, but even if I put it together, can I have two select filters?
    – user12947
    Nov 20, 2013 at 10:56
  • Probably. All depends on implementation of brand <-> model relationship.
    – Mołot
    Nov 20, 2013 at 11:00
  • I think this can be done through inline entity form Nov 20, 2013 at 14:54

3 Answers 3

1

Is the Conditional Fields module of any help to you? Here is a quote about it (from the module's project page):

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.

1
  • This wasn't the exact thing I was asking for, but after playing with this module it did everything I was looking for, just had to change the architecture. Thanks
    – user12947
    Feb 2, 2014 at 19:33
2
+25

Hope this will help you

function mymodule_form_alter(&$form, &$form_state, $form_id){

//put your form id
if ($form_id == 'two_state_node_form')  {

    $options_first = dynamic_add_options();

   $value_dropdown_first = isset($form_state['triggering_element']['#value']) ? $form_state['triggering_element']['#value'] : key($options_first);

    $form['#validated'] = TRUE;
    //put your first field name
    $form['field_select_list_1']['und']['#options'] = $options_first;
    $form['field_select_list_1']['und']['#default_value'] = $value_dropdown_first;
    $form['field_select_list_1']['und']['#ajax'] = array(
    'event' => 'change',
    'callback' => 'dynamic_ajax_callback',
    'wrapper' => 'dropdown_second_replace',
 );
    //put your secon field name
  $form['field_select_list_2']['und']['#prefix'] = '<div id="dropdown_second_replace">';
  $form['field_select_list_2']['und']['#suffix'] = '</div>';
  $form['field_select_list_2']['und']['#options'] = dynamic_second_dropdown_options($value_dropdown_first);
  $form['field_select_list_2']['und']['#default_value'] = isset($form_state['values']['field_select_list_2']) ? $form_state['values']['field_select_list_2'] : '';
 }
}



function dynamic_ajax_callback($form, $form_state) {

    return $form['field_select_list_2'];
}


function dynamic_add_options() {
  return array(
        'None' => 'None',
        'Nokia' => 'Nokia',
        'Samsung' => 'Samsung',
        'HTC' => 'HTC',
        'Apple' => 'Apple'
    );
   return $options;      
}

function dynamic_second_dropdown_options($key = '') {
    $options = array(
        'None' => array(
            'None' => 'none',
             ),
        'Nokia' => array(
            'Lumia 520' => 'Lumia 520',
            'Lumia 620' => 'Lumia 620'
        ),
        'Samsung' => array(
            'Galaxy S3' => 'Galaxy S3',
            'Galaxy S4' => 'Galaxy S4'
        ),
        'HTC' => array(
            'Hero' => 'Hero',//trancate the code
            'One' => 'One'
        ),
        'Apple' => array(
            'iPhone 4' => 'iPhone 4',
            'iPhone 5' => 'iPhone 5'
        ),
    );
    if (isset($options[$key])) {
        return $options[$key];
    }
    else {
        return array();
    }
}

this is demo for you demo

2
  • This is nice, but I am looking to get the data in the array dynamically in that function dynamic_second_dropdown_option()
    – user12947
    Nov 26, 2013 at 22:00
  • 1
    What do you mean with dinamically? You can build a taxonomy with 2 depth level and the call the taxonomy_get_tree to load the terms in dynamic_add_options() and dynamic_second_dropdown_options() using the tid like $key
    – Shyghar
    Nov 27, 2013 at 14:52
0

I would use a hierarchical taxonomy and use hierarchical_select. It only stores one value in the database, but you can still create a view that filters on brand by using a parent term filter.

1
  • but it doesn't support the export filter
    – user12947
    Nov 26, 2013 at 1:22

Your Answer

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