0

I am creating a form which dynamically changes based on the value of a select element. What happens is this:

  1. The user selects a value from the select element
  2. The select element uses Drupal's ajax methods to reload the form, now with some new elements for further configuration
  3. Some of the new elements need to be turned into jQuery widgets.

Now, the obvious solution here is to use behaviors to run the widget initialization code. The issue is that I don't know ahead of time which elements need to have what code run on them. I will only know that at the time the ajax portion of the form loads.

Currently, my solution is to have a very short JavaScript file which is solely responsible for adding new behaviors which in turn run the jQuery UI code that I need.

An alternate solution is to pre-load all of the possible configuration elements and inject all of the behaviors ahead of time. For various reasons this isn't a good idea, not least because I rather like lazy loading.

My question is: is there some way to attach behaviors in php through Drupal's ajax mechanism, like there is a way to modify the settings?

1

Yes you can attach Drupal Behaviors to newly added elements (DOM nodes):

You can check ahah.js (in Drupal.ahah.prototype.success):

// Attach all javascript behaviors to the new content, if it was successfully
// added to the page, this if statement allows #ahah[wrapper] to be optional.
if (new_content.parents('html').length > 0) {
  Drupal.attachBehaviors(new_content);
}

Or if your script creates the elements (answer on stackoverflow about drupal-behaviours):

var newNodes = $('<a href="#">Hello</a> <a href="#">World</a>').appendTo('#someDiv');
Drupal.attachBehaviors(newNodes);
  • Thanks for your answer, but this isn't quite the question I asked. I know how to add behaviors in javascript. I am wondering if there is a way to tell Drupal to do it from php, through something like drupal_add_js(). – Daniel Yule Mar 30 '12 at 16:39
0

Just to know. Are you using #states from Form API?

I think you should use drupal_add_js through a hook_form_alter in order to make sure that the widget plugin will be already loaded in the page and then use the behavior just to call the method responsible to property convert the element into a JQuery UI element. Using this approach I think you shouldn't worry about adding a behavior on the ajax response.

  • I'd use #attached instead of drupal_add_js, reduces some of the overheads and easier to alter from other functions later through the lifecycle. – Anthony Leach Sep 26 '15 at 14:11
0

This item answers your question if you was dealing with a custom callback: drupal_add_js within AJAX callback

djayp34's answer should help you in your scenario though:

drupal_add_js doesn't work properly in an AJAX callback. If you need to execute a JavaScript function within an AJAX callback, here's a way :

First in your form, you have to create (you can just import it) the JavaScript function :

function my_module_form($form, &$form_state) {
// [...]
  drupal_add_js(
    'Drupal.ajax.prototype.commands.myFunction = function(ajax, response, status) {
     // Whatever you want to do.
        }',
        'inline'
     );
 // [...]
 return $form;
}

Then in your AJAX callback, you need to call the function you have created :

function my_module_my_ajax_callback($form, &$form_state) {
  // [...]
  $commands[] = array("command" => 'myFunction',);
  // [...]
  return array('#type' => 'ajax', '#commands' => $commands);
}

Your Answer

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

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