I have a taxonomy drop down in a content type. Now when I select a particular taxonomy I want to show all nodes related to that taxonomy in another select box.

How can I implement this? If I need to do using Ajax, how can I get all contents after selecting a taxonomy? I am new to Drupal. Is there any module available for this purpose?

Or Please tell me how to get all contents using taxonomy programmatically? I have also to sort via price in a particular content type itself.

I need to do this in Drupal 7.



Yes, you need to implement this using hook_form_alter() and #ajax. The following is an example code you could try. The field field_category and the table field_data_field_category would be your taxonomy field. The field_nodes would be your another drop-down where you want to list the selected nodes related to the selected taxonomy.

The example code would sort the nodes by content types. To sort price of each particular content type, you would need to join another table in the query, for example, field_data_field_price and add your price field to the order-by clause. HTH.

function mymodule_form_alter(&$form, &$form_state, $form_id) {
    if( $form_id == 'your_node_form' ){

        $form['field_category']['und']['#ajax'] = array(
            // #ajax has two required keys: callback and wrapper.
            // 'callback' is a function that will be called when this element changes.
            'callback' => '_mymodule_ajax_dropdown_alter',
            // 'wrapper' is the HTML id of the page element that will be replaced.
            'wrapper' => 'replace_field_div',

        $form['field_nodes'] = array(
            '#type'     => 'select',
            '#title'    => t('Nodes'),
            '#options'  => array('' => '- Select -'),
            '#required' => FALSE,
            '#prefix'   => '<div id="replace_field_div">',
            '#suffix'   => '</div>'

        // An AJAX request calls the form builder function for every change.
        // We can change how we build the form based on $form_state.        
        if(isset($form_state['values']['field_category']) && $form_state['values']['field_category']['und'][0]['tid'] ){
            # when the respective taxonomy is selected, get the nodes related to the selected taxonomy
            $tid = $form_state['values']['field_category']['und'][0]['tid'];

            $query = db_select('node', 'n');
            $query->join('field_data_field_category', 'c', 'n.nid = c.entity_id');
            $query->condition('n.status', 1, '=')
                    ->condition('c.field_category_tid', $tid)
                    ->fields('n', array('nid', 'title'))
                    ->orderBy('n.type', 'ASC');
            $result = $query->execute();
            foreach($result as $row){       
                $form['field_nodes']['#options'][$row->nid] = $row->title;      
            # No taxonomy selected
            $form['field_nodes']['#options'] = array('' => '- Select -');

function _mymodule_ajax_dropdown_alter($form, $form_state){
    return $form['field_nodes'];

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.