I got a form with both Swfupload and Datepicker (from the Event module: sites/all/modules/contributed/event/contrib/datepicker/jquery.datePicker.js).

When the Datepicker's there without Swfupload, it works just fine. If Swfupload's there without Datepicker (like in other content types), it works just fine as well.

When the two are together on the same form, the whole thing goes weird. The datepicker shows a link "Select date". When you click on it, the datepicker appears. With Swfupload on the form, the moment the page finishes loading, that link gets added again (so you got: "Select dateSelect date"). Each time you click the mouse -- no matter where on the page -- it gets added again. After one click, it shows: "Select dateSelect dateSelect date".

Both the datepicker itself works (but only on the original link) and Swfupload work, so it's a purely aesthetical issue. It's still an issue, though, but can't find a way to fix it. Any help would be appreciated.

Comments

Raf’s picture

Ok, got a workaround, in case anybody is stuck on the same issue (highly doubtful, cause it's a very specific usecase, but you never know).

I pretty much put the two naughty boys separately. I first installed the Multistep module, then configured the content type that uses both to use Multistep, and separated both. It did require a bit of changes using hook_form_alter to put the datepicking part in a fieldgroup, and overwriting the validate function the Event module adds (using hook_form_alter as well) to handle this change properly. Now, it all works just fine. Of course, it's not a solution if you just have two fields. Using a multistep form'd be a bit stupid then.

guatebus’s picture

Hi Raf,

Could I ask you for some code (or pseudo code) as to how you managed to get Swfupload on your form? I'm trying to do this but I'm having lots of trouble.

Thanks!
gb

Raf’s picture

I got code from that project, but it's been a year and a half, so can't promise that the solution's still in it. Things have evolved big time since. But let's see...

function myFormAlterFunction(&$form, $form_state) {
  $datepick_fields = array(
      'start' => FALSE,
      'end' => FALSE,
  );

  drupal_add_js(array('datepick_fields' => $datepick_fields), "setting");  

  $key = array_search('event_form_validate', $form['#validate']);
  if($key !== FALSE) {
    $form['#validate'][$key] = 'myCustomValidationCallback';
  }

  $form['group_general']['event'] = $form['event'];
  $form['group_general']['title'] = $form['title'];
  $form['group_general']['title']['#weight'] = -10;
  $form['group_general']['body_field'] = $form['body_field'];
  $form['group_general']['field_location']['#weight'] = -9;
  $form['group_general']['event']['#weight'] = -8;
  $form['group_general']['body_field']['#weight'] = -7;

  unset($form['event']);
  unset($form['title']);
  unset($form['body_field']);  
  
  if(isset($form['#node']) && isset($form['#node']->event)) {
    $form['group_general']['event']['has_time']['#default_value'] != $form['#node']->event['has_time'];
  }
  else { $form['group_general']['event']['has_time']['#default_value'] = 1; }

  $form['group_general']['event']['has_time']['#title'] = t('All day event');
  unset($form['group_general']['event']['has_time']['#description']);

  global $base_url;

  $js_baseurl = array(
    'baseurl' => $base_url,
  );
  
  drupal_add_js(array('events' => $js_baseurl), 'setting');
}

And the validation callback:

/**
 * Special validate handler for the node form
 */
function myCustomValidationCallback($form, &$form_state) {
  if($form_state['storage']['step'] == 1) {
    $form_item['#parents'] = array('event', 'end_exploded');

    //form_set_value($form_item, $form_state['values']['event']['start_exploded'], $form_state); // CAUSES END DATE BUG (sets it to start date. No idea why it's actually used, so commenting it away)
    $path = drupal_get_path('module', 'event');
    drupal_add_js($path . '/event_node_edit.js');
    $path = drupal_get_path('module', 'datepicker');
    drupal_add_css($path . '/datePicker.css');
    drupal_add_js($path . '/date.js');
    drupal_add_js($path . '/jquery.datePicker.js');

    // Define the fields which should have a datepicker
    datepicker_add_picker('start');
    datepicker_add_picker('end');
    // datepicker.js will link each of the added fields to a datepicker
    drupal_add_js($path . '/datepicker.js','module','footer');
  }
}

I don't remember what it all does (really should have commented my code back then...). There's also been too much going on in that form over time. It could also be that I left some stuff in that isn't necessarily related to this specific issue.

So yeah, I'm afraid I can't be of much help, but I hope this can help you solve the problem nonetheless.