This patch contains a consolidation and improvement of the code in Form Builder that outputs JSON. It was written by Alex Bronstein (I'm just posting it).

The issue we ran into is that if you add something to Drupal.settings in an AJAX callback in Form Builder, it doesn't get used on the JavaScript end. This patch fixes that, and in doing so also creates a helper function called form_builder_render_json() to add the JSON data to the page.

Comments

quicksketch’s picture

This patch seems slightly questionable to me. Even Drupal 7 we don't normally override what's in Drupal.settings with a normal AJAX request (though I think there is a parameter to make it do that). Could you describe what the purpose of updating the Drupal.settings variables is?

David_Rothstein’s picture

I believe our use case for this was that our module altered the configuration form for a form builder property (in this case, for the webform file component). In the form alter function, we add a JS setting with some information about the form, which is then used by some custom JavaScript we have running. The problem is that when the configuration form is loaded via the Form Builder AJAX, those JavaScript settings never got added to the page.

To be honest, I'm not sure if we even need this anymore ourselves, but I think it still makes sense. I looked at what the AJAX system in D7 core does, and although you are right that ajax_command_settings() does not override Drupal.settings by default (the $merge parameter defaults to FALSE), when this function is called by ajax_render() it always sets $merge to TRUE, so I think that means that Drupal core does wind up merging things into Drupal.settings by default during an AJAX request (although I haven't stepped through the entire code to confirm that).

David_Rothstein’s picture

Updated the patch to chase HEAD.

quicksketch’s picture

Status: Needs review » Fixed
StatusFileSize
new3.88 KB
new3.81 KB

Well even though this is slightly questionable, I do think it's a good idea for us to have a centralized AJAX response function. This decreases differences between branches and also gives us a good place to start with updating the responses to fully utilize the D7 AJAX system, which supports this kind of thing directly anyway. Committed attached patches with minor changes. Though I did rename "form_builder_render_json" to "form_builder_json_output" to make it match drupal_json_output. In any case we're not actually rendering JSON, we're rendering something else into a JSON format.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

mstef’s picture

StatusFileSize
new3.87 KB

Simple reroll of #4 needed for drush make purposes: