Fix for use ahah to do ajaxy updates on individual feeds in a translation tool.

  1. // The story so far...
  2. function fringslate_menu() {
  3.  /*
  4.   * Lots of irrelevant menu entries
  5.   * ...and then:
  6.   */
  7.  
  8.   $items['strings/%/%'] = array(  // the two wildcards let me say which field and node I'm talking about
  9.     'type' => MENU_CALLBACK,
  10.    'title' => 'administer fring strings',
  11.     'page callback' => "fr_strings_admin_ajax",
  12.     'page arguments' => array(1,2),
  13.     'access arguments' => array('create strings'),
  14.   );
  15. }
  16.  
  17. function fr_strings_admin_form($form_id, $strings) {
  18.    foreach($strings as $string){
  19.      if(!empty($string['nid'])) {
  20.         $form['str_key'.$string['nid']] = array(
  21.         '#type' => 'textfield',
  22.         '#size' => '20',
  23.         '#default_value' => $string['str_key'],
  24.         '#ahah' => array(
  25.           'event' => 'change',
  26.           'path' => 'strings/str_key/'.$string['nid'],
  27.          )
  28.        );
  29.       /*
  30.        * some other form fields
  31.        * ...
  32.        */
  33.    }
  34.  
  35.   return $form;
  36. }
  37.  
  38. function fr_strings_admin_ajax($field, $nid){
  39.  
  40.  $cache = cache_get('form_'. $_POST['form_build_id'], 'cache_form');
  41.  $form = $cache->data;
  42.  $form_state = array('values' => $_POST);
  43.  
  44.   // update the db for JUST ONE ITEM
  45.   // on second thought this should be a module_invoke and I should use node to do the updatery here.
  46.  // this will do for now, though
  47.  db_query('UPDATE {fr_strings} set '.$field.' = "'.$form_state['values'][$field.$nid]. '" WHERE tnid = '.$nid);
  48.  
  49.   print drupal_to_js(array('status' => TRUE));
  50.   exit;
  51.  
  52.  
  53. }