0

I have list of select box like Team and Player with category option on top of the form. if i select the cricket in that category it should load players and Teams node title based on the category selected using ajax, i have done the ajax code but its working for only one field, please can onyone help to solve this

here is my complete code

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

  switch ($form_id) {
   case "loadtest_node_form":  // If the current edited page is an article form
    /*
    * The wrapper location is the entity reference field that contains the node titles
    * because that's where nodes listed needs to be restricted based on selection
    */
    $form['field_loadtest_player']['#prefix']='<div id="jicustom_test_entity_wrapper">'; 
    $form['field_loadtest_player']['#suffix']='</div>';
  $form['field_loadtest_team']['#prefix']='<div id="jicustom_test_entity_wrapper1">'; 
    $form['field_loadtest_team']['#suffix']='</div>';

 $form['field_loadtest_category'][LANGUAGE_NONE]['#ajax'] = array( // adding an ajax attribute
      'callback' => 'jicustom_test_entity_callback1',
      'wrapper' =>'jicustom_test_entity_wrapper1',
      'method' => 'replace',  // the data would replace the field
      'effect' => 'fade',
      'event' => 'change',  // On change of the selection this action would take place
    );  
$form['field_loadtest_category'][LANGUAGE_NONE]['#ajax'] = array( // adding an ajax attribute
      'callback' => 'jicustom_test_entity_callback',
      'wrapper' =>'jicustom_test_entity_wrapper',
      'method' => 'replace',  // the data would replace the field
      'effect' => 'fade',
      'event' => 'change',  // On change of the selection this action would take place
    );


// dpm($form);
    // Only when values are set in field_article_test_entity_type, should the options change
    // Also works for nodes that already have entity type selected
    if(!empty($form_state['values']['field_loadtest_player'])){
      $content_type = $form_state['values']['field_loadtest_category']['und']['0']['tid'];
      $form['field_loadtest_player']['und']['#options'] = jicustom_get_nodes($content_type);
    }
    if(!empty($form_state['values']['field_loadtest_team'])){
      $content_type = $form_state['values']['field_loadtest_category']['und']['0']['tid'];
      $form['field_loadtest_team']['und']['#options'] = jicustom_get_team($content_type);
    }
    break;
  }
}

/*
* Get List of node titles based on the content type
*/
function jicustom_get_nodes($content_type){

$result = db_query("SELECT n.title, n.nid
FROM node n
INNER JOIN field_data_field_category  ft ON n.nid = ft.entity_id
WHERE ft.field_category_tid = $content_type AND n.type='player'
ORDER by n.title ASC");
  foreach ($result as $record) {
    $nodes[$record->nid]=$record->title;
  }
  return $nodes;
}
function jicustom_get_team($content_type){

$result = db_query("SELECT n.title, n.nid
FROM node n
INNER JOIN field_data_field_category  ft ON n.nid = ft.entity_id
WHERE ft.field_category_tid = $content_type AND n.type='team'
ORDER by n.title ASC");
  foreach ($result as $record) {
    $nodes[$record->nid]=$record->title;
  }
  return $nodes;
}
function  jicustom_test_entity_callback($form, $form_state){
  return $form['field_loadtest_player'];

}
function  jicustom_test_entity_callback1($form, $form_state){
 return $form['field_loadtest_team'];

}
0

Use reference &$form_state instead of $form_state in functions
hook_form_alter(&$form, &$form_state, $form_id)

Edit: Ohh yes, I got, there is one should be field_loadtest_player you used "field_loadtest_category" on both ajax $form['field_loadtest_player'][LANGUAGE_NONE]['#ajax'] = array( ... ... );
$form['field_loadtest_category'][LANGUAGE_NONE]['#ajax'] = array( ... ... );

  • did you understand my issue??? your answer is not relevant to me – Anaz May 31 '16 at 10:43
  • I just edit it. – Sanjay May 31 '16 at 11:02

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.