Thursday, 24 February 2011

Using AHAH in CCK field


Using AHAH in CCK field

Recently I could work with some AHAH stuff for CCK, I think it would be very useful to ppl who are seeking for a solution on how AHAH works on CCK fields, in my problem what I wanted to do is I had a CCK fields one is a select field (SELCT 1, SELECt 2) and the other is Text field (Descrition), so I wanted to hide Descrition text field when I select SELECT 2. ok?
How I achieved it, its very somple work around btw.
1. Created a new module and implemented a hook_form_alter, add #prerender value to callback before render.
<?php/**
* Implementation of hook_form_alter().
*/
function MODULENAME_form_alter(&$form, &$form_state, $form_id) {
  switch (
$form_id) {
    case
'NODE_FORM': // NODETYPE_node_form
      // We force drupal to cache the form because it doesn't for a node add form

     
$form['FIELD_NAME_TO_HIDE']['#prefix'] = '<div id="type-wrapper">';
     
$form['FIELD_NAME_TO_HIDE']['#suffix'] = '</div>';
     
     
$form['#cache'] = TRUE;
     
// callback for the prerender
     
$form['FIELD_NAME_TO_ADD_AHAH']['#pre_render'][] = 'field_tipo_pre_render';
      break;
  }
}

function
field_tipo_pre_render($element) {
 
// now the real form elements are in here as children
 
$element['value']['#ahah'] = array(
   
'path' => 'ahah-change-type',
   
'wrapper' => 'type-wrapper',
  );
 
// this part may be naughty
 
form_expand_ahah($element['value']);

 
// $element doesn't pass by reference, so don't forget to return it
 
return $element;
}
?>
2. Create menu item for AHAH callback
<?php
 
// AHAH callback
 
$items['ahah-change-type'] = array(
   
'title' => 'AHAH Type',
   
'page callback' => '_custom_page_change_type',
   
'access callback' => 'user_access',
   
'access arguments' => array('access content'),
   
'type' => MENU_CALLBACK,
  );
?>
3. AHAH callback function
<?php/**
* Callback funciton for type
*/
function _custom_page_change_type() {
 
// The form is generated in an include file which we need to include manually.
 
include_once 'modules/node/node.pages.inc';

 
// We're starting in step #3, preparing for #4.
 
$form_state = array('submitted' => FALSE);
 
$form_build_id = $_POST['form_build_id'];
 
// Step #4.
 
$form = form_get_cache($form_build_id, $form_state);

 
// Preparing for #5.
 
$args = $form['#parameters'];
 
$form_id = array_shift($args);
 
$form_id = $args;
 
$form_state['post'] = $form['#post'] = $_POST;
 
$form['#programmed'] = $form['#redirect'] = FALSE;

 
// Step #5.
//  drupal_process_form($form_id, $form, $form_state);
  // Step #6 and #7 and #8.
//  $form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id);
  //change your element here
//  $form['field_valores_description']['#value'] = 'roses';
//  $form['field_valores_description']['#disabled'] = TRUE;
//  unset($form['field_valores_description']);
 
form_set_cache($form_build_id, $form, $form_state);
 
$form += array(
   
'#post' => $_POST,
   
'#programmed' => FALSE,
  );

 
$form = form_builder('node_form', $form, $form_state);//  print_r($form_state['post']['field_tipo']['value']);
  // Step #9.
 
$subform = array();
 
$subform['field_logo'] = $form['field_logo'];
 
$subform['field_valores_description'] = $form['field_valores_description'];
 
$subform['field_enlace_web'] = $form['field_enlace_web'];//  unset($choice_form['#prefix'], $choice_form['#suffix']);
 
if ($form_state['post']['field_tipo']['value'] == 'Distribuidor')
   
$subform = array();

 
$output = theme('status_messages') . drupal_render($subform);

 
// Final rendering callback.
 
drupal_json(array('status' => TRUE, 'data' => $output));
}
?>

No comments:

Post a Comment

Source base installation of php, mysql and apache in ubuntu/ linux

Compile and Install a LAMP(Linux/Apache/MySQL/PHP) Server from Source In the last post, I described the method to install a LAMP ser...