1

I am creating a custom widget, which is just a wrapper of the managed_file field widget. The immediate goal is to modify the ajax behavior of the upload button, so that the value of a text field can be modified when a mouse-down event is registered.

My strategy involves adding a #process callback to the upload button. This means file_managed_file_process gets run first, follow by my own callback. My callback function modifies the $element['upload_button']['#ajax'] with my own ajax settings, which adjusts ['#ajax']['path'] to point to my own ajax callback function.

To test, I created a new content type with two fields. The first named 'field_sandbox_widget' which uses the custom widget, and the second is a text-field named 'field_text_widget'. In the edit-form of the aforementioned content type, I can see the text box refreshed when the button is clicked. However, the form has proven to be extremely "sticky", and nothing I do seems to change appearance of the text field.

I have been trying for weeks and getting nowhere. Could you point me to the right direction?

function sandbox_field_widget_info() {            
dsm('sandbox_field_widget_info');            
  return array(            
    'sandbox' => array(            
      'behaviors' => array(            
        'default value' => FIELD_BEHAVIOR_NONE,            
        'multiple values' => FIELD_BEHAVIOR_DEFAULT,            
      ),            
      'field types' => array('file'),            
      'label' => 'xxxxing sandbox',            
    ),            
  );            
}            

function sandbox_field_widget_form(&$form, &$form_state, $field, $instance, $language, $items, $delta, $element) {            
dsm('sandbox_field_widget_form');            

  $element_info = element_info('managed_file');            
  $element += array(            
    '#type' => 'managed_file',            
    '#process' => array_merge($element_info['#process'], array('sandbox_field_widget_process')),            
  );            

dsm($element, '  element');            

  // Add ajax wrapper so that the entire form can be replaced.            
  // This is the only way to replace multiple fields.            
//  $form['#prefix'] = '<div id="' . 'sandbox-wrapper-all' . '">';            
//  $form['#suffix'] = '</div>';            


  return $element;            
}            


function sandbox_field_widget_form_alter(&$element, &$form_state, $context) {            
  $field_name = $context['field']['field_name'];            
  $wrapper_id = 'sandbox-' . $field_name . '-ajax-wrapper';            

  $wrapper_prefix = '<div id="' . $wrapper_id . '">';            
  $wrapper_suffix = '</div>';            

dsm($field_name, 'sandbox_field_widget_form_alter');            
// dsm($element, '  element');            

  // Adds a wrapper around each field.            
  if (isset($element['#prefix'])) {            
    $element['#prefix'] .= $wrapper_prefix;            
  }            
  else {            
    $element['#prefix'] = $wrapper_prefix;            
  }            

  if (isset($element['#suffix'])) {            
    $element['#suffix'] = $wrapper_suffix . $element['#suffix'];            
  }            
  else {            
    $element['#suffix'] = $wrapper_suffix;            
  }            
}            




function sandbox_field_widget_process($element, &$form_state, $form) {            
dsm('sandbox_field_widget_process');            

  // Replace ajax calls.            
  $ajax_replacement = $element['upload_button']['#ajax'];            
  $ajax_replacement['path'] = 'sandbox/ajax/' . implode('/', $element['#array_parents']) . '/' . $form['form_build_id]['#value'];            
  $ajax_replacement['wrapper'] = 'sandbox-wrapper-header';            

  $element['upload_button']['#ajax'] = $ajax_replacement;            

  // Toggle to see some changes!!!                
  if (!empty($form['field_text_widget']['und'][0]['value']['#title'])) {            
    $form['field_text_widget']['und'][0]['value']['#title'] = 'xxxx';            
  }            
  else {            
    $form['field_text_widget']['und'][0]['value']['#title'] = '';            
  }            

dsm($ajax_replacement);            
  return $element;            
}            





function sandbox_ajax_callback() {            
dsm('sandbox_ajax_callback');            
  $args = func_get_args();            

  list($form, $form_state, $form_id, $form_build_id, $commands) = ajax_get_form();            

  drupal_process_form($form['#form_id'], $form, $form_state);            
  $form_state['rebuild'] = TRUE;            

  $ajax = array(            
    '#type' => 'ajax',            
    '#commands' => array(),            
  );            



  $commands = array();            
  $commands[] = ajax_command_replace(NULL, theme('status_messages'));            
  $commands[] = ajax_command_replace('#sandbox-field_sandbox_widget-ajax-wrapper', render($form['field_sandbox_widget']));            
  $commands[] = ajax_command_replace('#sandbox-field_text_widget-ajax-wrapper', render($form['field_text_widget']));            

  $ajax['#commands'] = array_merge($ajax['#commands'], $commands);            

  return $ajax;            
}            




function sandbox_form_alter(&$form, &$form_state, $form_id) {            
  if (!isset($form['#prefix'])) {            
    $form['#prefix'] = '';            
  }            
  $form['#prefix'] = '<div id="sandbox-wrapper-header"></div>';            
}            

function sandbox_help($path, $arg) {            

  switch ($path) {            
    case 'admin/help#sandbox':            
      $output = '';            
      $output .= 'xxxx';            
      return $output;            
  }            
}            



function sandbox_menu() {            
dsm('sandbox_menu');            

  // Copied from file_menu            
  $items = array();            
  $items['sandbox/ajax'] = array(            
    'page callback' => 'sandbox_ajax_callback',            
//    'page callback' => 'file_ajax_upload',            
    'delivery callback' => 'ajax_deliver',            
    'access arguments' => array('access content'),            
    'theme callback' => 'ajax_base_page_theme',            
    'type' => MENU_CALLBACK,            
  );            

  return $items;            
}            

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.