0

Does the #AJAX FAPI property work on the body field? I've got it to work with things like submit buttons and textfields such as the title. When I alter the node edit form, and attach an #ajax array to the body field though, nothing happens. I've tried multiple event types (keyup, keypress, focus). Is there anything special that needs to be done to get #ajax to work on the body field?

  // This works (creates backend xhr call).      
  $form['title']['#ajax'] = array(
    'wrapper' => 'autopreview',
    'callback' => 'autopreview_ajax_callback',
    'method' => 'replace',
    'event' => 'focus',
  );

  // Doesn't work.
  $form['body']['#ajax'] = array(
    'wrapper' => 'autopreview',
    'callback' => 'autopreview_ajax_callback',
    // 'method' => 'replace',
    'event' => 'focus',
  );

  // Doesn't work.
  $form['body']['und'][0]['#ajax'] = array(
    'wrapper' => 'autopreview',
    'callback' => 'autopreview_ajax_callback',
    'method' => 'replace',
    'event' => 'focus',
  );
1
  • Are the Drupal ajax js libraries loaded on your page? You can check by opening a javascript console and typing Drupal.ajax.
    – cam8001
    Commented Aug 23, 2012 at 15:52

1 Answer 1

1

According to the FAPI docs textareas do support #ajax, yes.

I'd imagine the problem is because you'll be using a WYSIWYG editor on that field. Such an editor will replace the textarea with an iFrame, and the AJAX events aren't bound to that iFrame (or its inner textarea), so won't get fired when you change the text.

If that is the case, I don't think there's any easy way to overcome the problem except to turn of the WYSIWYG editor for that field.

7
  • I do have a WYSIWYG editor on the field...but after disabling it temporarily (by clicking on the switch to plain text link), still no luck. Maybe I should give it a try after disabling the WYSIWYG completely. Or I could use the CKEditor's JS API... Commented Aug 23, 2012 at 14:45
  • If the editor isn't active it shouldn't really interfere...seeing your form alter code would be helpful if you're able to post it (and the callback function) in the question?
    – Clive
    Commented Aug 23, 2012 at 14:46
  • I forgot...I deactivated the WYSIWYG altogether for my test content type...and no luck. I updated the question w/ the code. Commented Aug 23, 2012 at 15:07
  • Try $form['body']['und'][0]['value']['#ajax'] = ...
    – Clive
    Commented Aug 23, 2012 at 15:12
  • 1
    To help make the field API functions more generic, every field array is accessed as if it contained multiple values. It's just that if the cardinality is one for the field, only $entity->field_name[LANGUAGE_NONE][0] will contain any data. What makes it confusing is when it comes to entity forms, some field-type elements are accessed using $form['field_name'][LANGUAGE_NONE], but only when on an entity add form, not on an edit form. I haven't looked into it enough to work out what the pattern is but there's definitely one there
    – Clive
    Commented Aug 23, 2012 at 17:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.