This is a continuation of #399982: Simplify and standardize returning an AHAH response. #544418: Integrate CTools AJAX framework with Drupal to extend (and replace) existing ahah framework has been committed, yay!

However, poll.module still has this:

function poll_choice_js($form, $form_state) {
  ...
  return theme('status_messages') . drupal_render($choice_form);
}

However, the rendering of the form might produce additional status messages, so this probably should be changed to:

function poll_choice_js($form, $form_state) {
  ...
  $output = drupal_render($choice_form);
  return theme('status_messages') . $output;
}

But why make implementers of AJAX callbacks worry about that? Why not:

function poll_choice_js($form, $form_state) {
  ...
  return drupal_render($choice_form);
}

And let ajax_form_callback() be responsible for the status messages?

I don't yet have a proposal for what the best way of handling it is. @quicksketch: do you have a recommendation?

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

sun’s picture

rfay’s picture

subscribing

effulgentsia’s picture

Status: Active » Needs review
FileSize
1.44 KB

I know we can do better than this patch, something more along the lines of full separation of messages all the way to the browser, like quicksketch proposes in http://drupal.org/node/399982#comment-1807684. But I'm not yet sufficiently up to speed on the new AJAX framework to create that patch, so here's a less ambitious solution. If we can't get the better one in on time, let's at least get this one in.

The thinking behind this patch is to make the simple use-case of a AJAX callback returning a string of html result in status messages automatically handled without the AJAX callback doing anything. However, to allow special AJAX callbacks the flexibility of NOT returning status messages, nothing is done if the AJAX callback returns an array of commands instead of an html string. For, example, if poll.module did not want status messages included with its result, it could change

function poll_choice_js($form, $form_state) {
  ...
  return drupal_render($choice_form);
}

to

function poll_choice_js($form, $form_state) {
  ...
  return array(ajax_command_replace(NULL,  drupal_render($choice_form)));
}

A little complicated perhaps, but it moves the more complicated code needed to the exception (not wanting status messages) rather than having extra complication for the default situation (wanting status messages).

Status: Needs review » Needs work

The last submitted patch failed testing.

effulgentsia’s picture

Something else that should be included with this issue is a fix to a bug introduced into HEAD from patch 72 of #367567: Use AJAX framework for "Add more" / load include file containing form / introduce $form_state['build_info']. When you click "add another item", if there's a validation error, that gets shown as a status message at the top of the new content. If you click "add another item" again, and if the same validation error occurs again, it gets duplicated. Or, if no validation error occurs the second time, the old one is still there. This is because the returned AJAX content (including the status messages) replace a previously defined "wrapper" div, but the status messages are outside of the *new* wrapper div, so a subsequent "add another item" doesn't replace the prior status messages. This can be easily tested by setting the "body" field of the "page" content type to "unlimited" number of values and having a maximum length of 1.

sun’s picture

@effulgentsia: That particular issue is tackled in #370537: Allow suppress of form errors (hack to make "more" buttons work properly). See you over there :)

effulgentsia’s picture

Really? I thought that issue was just to make sure that *some* validation get bypassed (for example, validation of the node title field when you're adding another item of the body field). Are we saying that we plan on not validating *anything* when using the "add another item" button?

If that's the case, then what about other status messages (not validation errors)? Should those persist across multiple uses of "add another item", or get replaced each time?

effulgentsia’s picture

I'm having a hard time getting CVS to work for me right now. It's EXTREMELY slow (like taking 10 minutes per file to do a cvs update and not managing to do a cvs diff). I'm not sure if it's something funky with my room at Paris (here for DrupalCon), or if the server is crawling due to final stretch of code-freeze. @sun or @rfay, if you're having more luck with cvs right now, would you be willing to re-roll patch from comment #3? Do either/both of you agree that in the absence of some other solution being developed within the next few hours (what time and time zone is code-freeze?), this small patch is better than nothing?

rfay’s picture

Status: Needs work » Needs review
FileSize
1.13 KB

Per @effulgentsia's request, here is the patch from #3 re-rolled to fit the current HEAD.

@effulgentsia, the last rumor I heard was that we have until Dries's keynote to get it all together. Look forward to meeting you tomorrow.

effulgentsia’s picture

This one solves the issue from comment #5. See comment #7 for why I believe this is important even if #370537: Allow suppress of form errors (hack to make "more" buttons work properly) lands. Note also that if a ajax callback wants to handle status messages differently, it can do so by returning an array of ajax commands instead of a string of html.

rfay’s picture

Status: Needs review » Needs work

Why don't we fix the ajax_form_callback() while we're in here. It's using $html when really it should be named $commands, perhaps, or $content. Calling it $html masks the fact that it can be a list of commands.

It looks to me like this is an excellent cleanup that should go in.

The change to field.form.inc shows how this simplifies normal handling of status messages. Also, with this patch, the ordinary user doesn't necessarily even have to think about status messages, and they often don't anyway.

effulgentsia’s picture

Status: Needs work » Needs review
FileSize
3.21 KB

Thanks, @rfay, but I didn't follow closely all the work merlinofchaos and others were putting into the D7 ajax library, and I don't feel sufficiently empowered to override the decision on that variable name. I agree that it's an unclear name, and if someone wants to submit a separate patch for that, I'll be happy to +1 it.

For this patch, I reverted field.form.inc to return an array of commands instead of just the content, because I talked with yched, and he indicated a concern about not leaving an example of returning an array anywhere in core. Ajax.inc supports returning an array so that contrib modules, like views, can do fancier AJAX stuff than simply returning content.

rfay’s picture

Status: Needs review » Reviewed & tested by the community

I think this is fine. It

  • Makes most AJAX use slightly simpler (and more comprehensive if the code doesn't handle status messages).
  • Allows existing code to work unchanged.
  • Allows more sophisticated code to explicitly handle status messages when necessary.

Marking RTBC.

moshe weitzman’s picture

Status: Reviewed & tested by the community » Needs review

THis looks sensible but I'm not really qualified to RTBC it. Code looks clear and proper.

rfay’s picture

Status: Needs review » Reviewed & tested by the community

Setting back to RTBC, assuming @moshe & I cross-posted. If that's not the case, please set it back again.

sun’s picture

Issue tags: +API clean-up

Introducing a new tag for feature freeze: API clean-up.

Dries’s picture

I looked at this patch and it looks like an improvement -- with some bugfixing even. Would be nice to get @quicksketch to look at this too.

Status: Reviewed & tested by the community » Needs work

The last submitted patch failed testing.

rfay’s picture

Status: Needs work » Needs review

Setting back to needs review and requesting retest.

rfay’s picture

Status: Needs review » Reviewed & tested by the community

Setting back to RTBC, which was the status it belonged in. Still waiting for @quicksketch to chime in, though.

sun’s picture

+++ includes/ajax.inc	1 Sep 2009 14:44:24 -0000
@@ -214,14 +214,18 @@ function ajax_form_callback() {
     if (is_string($html)) {
-      $commands = array();
       $commands[] = ajax_command_replace(NULL, $html);
+      $commands[] = ajax_command_prepend(NULL, theme('status_messages'));

$commands need to be initialized here, so please revert the removal.

+++ modules/field/field.form.inc	1 Sep 2009 14:44:28 -0000
@@ -362,9 +362,13 @@ function field_add_more_js($form, $form_
-  $output = theme('status_messages') . drupal_render($field_form);
-
+  // Everything below this comment could be replaced with
+  //   return drupal_render($field_form);
+  // But then we wouldn't have an example in core of an AJAX callback that
+  // returns an array of commands.
+  $output = drupal_render($field_form);
   $commands = array();
   $commands[] = ajax_command_replace(NULL, $output);
-  ajax_render($commands);
+  $commands[] = ajax_command_prepend(NULL, theme('status_messages'));
+  return $commands;

Instead of providing an example usage somewhere deeply hidden in core, we should move that example into the PHPDoc of ajax_command_replace() and just keep ajax_render(drupal_render($field_form)) here.

This review is powered by Dreditor.

webchick’s picture

Status: Reviewed & tested by the community » Needs work

Looks like this needs some more work.

sun’s picture

Status: Needs work » Reviewed & tested by the community
FileSize
3.85 KB

Fixed the remaining issues.

Dries’s picture

Status: Reviewed & tested by the community » Needs work

I re-reviewed sun's latest patch and it looks good so I committed it to CVS HEAD. Thanks all.

Too bad @quicksketch didn't provide input (re #17), but let's move forward. We can always revisit this when we get some extra feedback from quicksketch. :)

Let's update the documentation in the handbook and mark this as fixed.

rfay’s picture

Assigned: Unassigned » rfay

I'll plan to take care of the docs on this on a full AJAX docs sweep.

rfay’s picture

Status: Needs work » Fixed

Documentation followup on this is moving to #610068: Document AJAX no-js and use-ajax. Many issues can be combined there.

Status: Fixed » Closed (fixed)
Issue tags: -API clean-up

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