How to enable Ajax for CCK Fields in Drupal 7

The idea is to modify another CCK field based on selection of an item via a select list in another field.

Let us consider a content type called article containing 2 additional fields: article_test_entity - which is an entity reference that displays node titles of all nodes and article_test_entity_type - which is a select list that contains Content Type names list with its key as the actual machine name of the content type. The idea is to restrict the list of node titles displayed on article_test_entity based on the content type selected via article_test_entity_type.

<?php
 

/*
* An ajax attribute needs to be added to the field which provides the condition for selection,
* in our case field_article_test_entity
* The data returned via an ajax call needs to be placed at a location which provided as a wrapper which normally is a div location
* in the browser as ajax is a javascript call. The data returned by the ajax call would replac/add in the wrapper location
*/
function jicustom_form_alter(&$form, $form_state, $form_id) {
  switch (
$form_id) {
    case
"article_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_article_test_entity']['#prefix']='<div id="jicustom_test_entity_wrapper">'
   
$form['field_article_test_entity']['#suffix']='</div>';

   
$form['field_article_test_entity_type'][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_article_test_entity_type'])){
     
$content_type = $form_state['values']['field_article_test_entity_type'][LANGUAGE_NONE][0]['value'];
      
$form['field_article_test_entity'][LANGUAGE_NONE]['#options'] = jicustom_get_nodes($content_type);
    }
    break;
  }
}

/*
* Get List of node titles based on the content type
*/
function jicustom_get_nodes($type){
 
$nodes  =array();
 
$query=db_select('node', 'node')
    ->
fields('node', array('nid','title'))
    ->
condition('type',$type);
 
$result = $query->execute();
  foreach (
$result as $record) {
   
$nodes[$record->nid]=$record->title;
  }
  return
$nodes;

}
/*
* The callback method returns the form field which gets replaced - via method in ajax attribute.
*/
function  jicustom_test_entity_callback($form, $form_state){
  return
$form['field_article_test_entity'];
}
?>

Another Example For modifying a text area using the description field present in the taxonomy term selected:

<?php
/**
* Form alter in evaluation form to achieve ajax functionality to extract description data from
* from taxonomy field and attach the data to evaluation field in the Perf Review form
*/
function jicustom_form_alter(&$form, $form_state, $form_id) {
  switch (
$form_id) {
    case
'annual_performance_review_node_form':
     
$form['field_performance_evaluation']['#prefix']='<div id="jicustom_evaluation_entity_wrapper">';
     
$form['field_performance_evaluation']['#suffix']='</div>';

     
$form['field_performance_review_type'][LANGUAGE_NONE]['#ajax'] = array( // adding an ajax attribute
         
'callback' => 'jicustom_perf_review_entity_callback',
         
'wrapper' =>'jicustom_evaluation_entity_wrapper',
         
'method' => 'replace'// the data would replace the field
         
'effect' => 'fade',
         
'event' => 'change'// On change of the selection this action would take place
     
);
    break;
  }
}

/*
* The callback method returns the form field which gets replaced - via method in ajax attribute.
*/
function  jicustom_perf_review_entity_callback(&$form, &$form_state){
 
$perf_review_type_id = NULL;
  if(empty(
$form_state['values']['field_performance_evaluation']['und'][0]['value'])){
    if(isset(
$form_state['values']['field_performance_review_type']['und'][0]['target_id'])){
     
$perf_review_type_id = $form_state['values']['field_performance_review_type']['und'][0]['target_id'];
    }
    if(
is_numeric($perf_review_type_id)) {
     
$term = taxonomy_term_load($perf_review_type_id);
     
$form['field_performance_evaluation']['und'][0]['value']['#value'] = $term->description;
    }
  }
  return
$form['field_performance_evaluation'];
}
?>

--
http://www.metaltoad.com/blog/avoiding-drupal-7-ajax-pitfalls