0

I may be missing something really basic here but it has been bothering me for a couple of days now.

I have a page that basically contains a link which loads another page through AJAX. The page that is loaded is basically a simple node/add form for articles. The problem is that the form that is loaded does not contain any AJAX behavior (I even installed the AJAX example module and the simple ajax checkbox that it is adding does not work). I do not know if it relevant but the fieldsets in the node add form are not working either.

I did some research and I am suspecting that I need to do something on the AJAX callback that loads the form. I read about Drupal.attachBehaviors which I tried attaching to the element where I'm loading my form but it does not seem to be working.

I'd be happy to edit my question to add some code but I was wondering if I'm missing something really basic.

Edit (my current code):

PHP:

function aforms_menu() {
  $items = array();

  $items['aforms/article'] = array(
    'page callback' => 'aforms_get_form',
    'type' => MENU_CALLBACK,
    'access callback' => TRUE,
    'delivery callback' => 'aforms_ajax_callback',
  );

  return $items;
}

function aforms_get_form() {

  global $user;

  module_load_include('inc', 'node', 'node.pages');
  drupal_add_library('system', 'drupal.ajax');

  $node = (object) array(
    'uid' => $user->uid,
    'name' => (isset($user->name) ? $user->name : ''),
    'type' => 'article',
    'language' => LANGUAGE_NONE,
  );

  $form = drupal_get_form('article_node_form', $node);

  return $form;

}

function aforms_ajax_callback($form) {

  print drupal_render($form);

  drupal_page_footer();

}

JS:

(function ($) {

Drupal.behaviors.aforms = {
    attach: function(context, settings) {
        $('body').on("click", "#load-me-up", function() {

            $('#target-container').load('/aforms/article', function() {
                console.log('Before attach behaviors executed');
                console.log(context);
                console.log(settings);
                Drupal.attachBehaviors();
                console.log('After attach behaviors executed');
                console.log('Drupal object:');
                console.log(Drupal);
            });

        });
    }
}

})(jQuery);
1
  • 1
    This won't help but I had an awful lot of difficulty doing this before. Drupal.attachbehaviors got like 75% of the form working but not all and because of the size of the form, page load was 2 seconds longer. Instead, lazy loading the form in an iFrame ended up ended up being the perfect solution for me. Might be good for you as well. Dec 11 '16 at 4:32
1

You need to provide Drupal.attachBehaviors with the context (what was added) and the Drupal.settings. You should also use context in your script, so the JS should be rewritten something like this (not tested)

(function ($) {

  Drupal.behaviors.aforms = {
    attach: function(context, settings) {
      $('body', context).on("click", "#load-me-up", function() {
        $('#target-container').load('/aforms/article', function() {
          Drupal.attachBehaviors($(this), settings);
        });
      });
    }
  }

})(jQuery);

You need to use context on the $('body') to avoid the handle to be attached multiple times. In the load callback you need to add $('#target-container') to attachBehaviors, but in this case $(this) should be the same thing.

2
  • Thanks for your answer. The problem in this case is that the behaviors are not attached at all. When I inspect the Drupal object on the form there are a lot of behaviors and objects missing on the form that is loaded through AJAX compared to the one that is directly accessed via node/add/article. So I was trying to understand if the way that I'm loading the form is not attaching the necessary javascript (tried your solution and it still does the same thing).
    – user4979
    Dec 8 '16 at 4:06
  • @LimArctan It depends on what the url you are loading actually provide. If you render a form on aforms/article you will miss that data on Drupal.settings which could be you are facing now.
    – googletorp
    Dec 9 '16 at 20:58

Your Answer

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