How can I disable AJAX for a file field in 7.12? I think I need to do something with hook_widget_process, but it's a bit daunting at the moment. I'm not even sure there is a hook_widget_process. Perhaps not. There is a file_field_widget_process.

My motivation here is that I need to work around a bug affecting AJAX functionality in file field.

Thank you

Comments

arnoldbird’s picture

Sorry, I should have said image field, not file field. It's the image field type I'm working with.

Jaypan’s picture

This is one of those questions where it sounds like you are asking how to do one thing, to solve a bigger problem, but that you are maybe going about solving that big problem the wrong way. Why is it that you want to disable Ajax on the image field?

arnoldbird’s picture

My motivation here is that I need to work around a bug affecting AJAX functionality in file field.

Really I should have said image field, not file field, to be precise.

The bug, if you're interested, is at http://drupal.org/node/1526418

Thanks for your response.

arnoldbird’s picture

@jaypan

Just to give a bit more context, I am working on a project where forms are embedded in various locations, often in the non-admin theme (for lack of a better expression). The filetest module that I uploaded in the bug report is a simplified version of the sort of thing I'm doing in various places throughout the site, and this is the heart of it:
drupal_render(drupal_get_form('some_form_id', $some_entity))

Of course, drupal_get_form doesn't care where you call it. It works everywhere. It works beautifully. But that doesn't mean that all the fields in the form are going to work everywhere. It appears that the image field (and perhaps also the file field) do care where they're used, and more specifically the AJAX functionality does not work in all situations. Naturally, javascript can never be expected to work in *all* situations, because sometimes there might be some other javascript that presents a conflict. Since this can be repro'd in various themes that come with the core, it appears this is a core bug (or conflict) and one that's not easy for me to fix. It's quite daunting for me to think about fixing a core AJAX bug in Drupal, and because I need the image field, I'm wondering if, for the time being, I can somehow disable the AJAX functionality through the use of some hook.

Thanks again.

ps -- If it turns out this is not a core bug, and instead is due to some mistake I'm making in the 30 or so lines of code in my filetest module, then I'd be thrilled to find that out. Unfortunately I don't think that's the case. I think it's something in the core.

Jaypan’s picture

arnoldbird’s picture

Thanks again, but that doesn't address the issue, unfortunately. I don't think the people in that thread are experiencing the same issue I am, but I did try the solutions there, including calling drupal_get_form first and then appending the settings. I'm still seeing the same error I mentioned in the bug report.

arnoldbird’s picture

@jaypan, if you happen back this way... do you happen to know if it's possible to use the "picture" field (the one that is a part of the core user module) in other contexts, like nodes and other fieldable entities? As it happens, that picture field provides what I need, and does it without AJAX. It's not quite as slick as the image field, but it works in a custom page.

Right now my worst case scenario looks like... write a module that provides a non-AJAX image field based on the "picture" field code in user.module. That seems awfully convoluted, though, to have to do that... and of course I would much rather just disable the AJAX functionality in the image field, if at all possible. My best case scenario is to find out I'm doing something wrong in that 30-line filetest module, and that the bug I've posted is invalid.

Jaypan’s picture

I've never specifically used the image field in ajax loaded content, but the other side of that is that I've also never found anything I couldn't get working after playing around with it. In some cases, it only requires loading the settings like shown, other times you also have to load some scripts with it. If it works without Ajax, it can also work with Ajax (though with a fair bit of work).

I've actually just ajaxified my entire site that I'm in the midst of rebuilding, so that there are never any page loads after the first one (with the exception of submitting forms, and I may even ajaxify that at some point).

arnoldbird’s picture

@jaypan

I don't think we're talking about AJAX loaded content here, unless I'm missing something. What I'm trying to do doesn't involve AJAX loaded content. I've mentioned AJAX only because the issue I'm trying to work around is an AJAX bug (as far as I can tell) in the image field, or related to the image field. The image field uses AJAX.

Jaypan’s picture

That sounds like you may have a javascript conflict on your page then. Use Firebug with Firefox, or the Javascript/Safari console to see if any errors are appearing on that page.

arnoldbird’s picture

Unfortunately, no errors in firebug, though the logging could provide clues. Whatever it is, it comes to a head in the PHP.

Jaypan’s picture

I just dug through with the example module you attached, and the code for processing the form, and it does appear to be a bug. The form API should really have an attribute for attaching required files, that would solve this problem.

arnoldbird’s picture

I've been looking at file_ajax_upload() (line 236 of file.module).

We loop through $form_parents twice in the function, and the second time we do...

foreach ($form_parents as $parent) { 
    $form = $form[$parent];
 }

...it contains...

array(3) { [0]=> string(11) "field_image" [1]=> string(3) "und" [2]=> string(1) "0" }

So it seems the code expects $form['field_image']['und']['0'] to be set to some value, but there isn't a field_image index in $form at that point. The last line in the traceback is an undefined index error, complaining that there is no field_image in $form. I suppose drupal_process_form should have added the missing field_image_data by the time we loop through $form_parents the second time, but it hasn't. I haven't had a chance to look into why that is. I'm not sure if that's what you're talking about above.

Jaypan’s picture

The problem is that the AJAX callback is trying to call the function user_profile_form(). This function exists in user.pages.inc, and since the error is saying that it's not finding this function, it stands to reason that the file is not properly being included. This means that in the callback to the managed_field element (of which the image field is a type of) is not properly including the file.

I tried using a #pre_render on the form, and then on the element itself, including the user.pages.inc in the pre render function, but this didn't work either.

arnoldbird’s picture

The form API should really have an attribute for attaching required files, that would solve this problem.

Why do you suppose that's not needed somewhere like ?q=user/1/edit

Jaypan’s picture

Why do you suppose that's not needed somewhere like ?q=user/1/edit

The menu definition for that path includes the file user.pages.inc, which means that it doesn't need to be automatically included.

But actually, your comment gave me the idea to come up with your solution. Change your menu definition to the following and clear the cache, and it works now:

  $items['myadmin/%'] = array(
    'title' => 'Admin',
    'page callback' => 'filetest_page_myadmin',
    'access callback' => TRUE,
    'page arguments' => array(1),
    'file path' => drupal_get_path('module', 'user'),
    'file' => 'user.pages.inc',
  ); 
Scrotos’s picture

Is there a way to do this but off a node? I am currently using profile2 and entity to load a profile2 edit form and attach it to the bottom of a node and am getting this same error.

arnoldbird’s picture

It's a hack, but this cures the problem: http://drupal.org/node/1336212#comment-5859652

Jaypan’s picture

So does the solution I posted above, without needing to hack core.

arnoldbird’s picture

@jaypan

Thanks! I hadn't seen your solution before I posted last. Yes, that's much better than my hack.

I was beginning to think something like that could be done in hook_menu, though I thought I would have to override the hook_menu entry for the AJAX callback. Never occurred to me that doing that in my module's hook_menu would make the user.module code available for the subsequent AJAX callback.

Jaypan’s picture

Actually, it appears that the best solution is one I forgot existed - it came up in a different thread tonight and I realized that this is the proper solution. Instead of using module_load_include() in your form definition, you use form_load_include().

arnoldbird’s picture

That does seem to work. In my case:

$form_state['build_info']['args'] = array($account);
form_load_include($form_state, 'inc', 'user', 'user.pages');
$form = drupal_render(drupal_get_form('user_profile_form', $account));
arnoldbird’s picture

form_load_include does not work here, at least not how I've implemented it directly above. With that approach I get AJAX errors in the file upload widget. Best solution, at least in my situation, still seems to be referencing the include in hook_menu.

shahjay.750’s picture

image upload through ajax gives error in cms area. In console throws 404 for "http://mysite.com/file/ajax/contact_image/und/0/form-j0CWtBriSxwcqgBqLAH..." post request via ajax and in page "An unrecoverable error occurred. The uploaded file likely exceeded the maximum file size (100 MB) that this server supports." while adding only 20kb image file.

Other posts says js conflicting. It is difficult to find which js conflicting from all.

Is there any hack or solution?

Thanks in adv.

Svm’s picture

Did you find a solution to this problem ? I get the same error on a form that gets rebuild after submission.
Regards,
Serge

leymannx’s picture

// Disable ajax upload for file field
drupal_add_js("jQuery(function() { jQuery('#edit-field-image :submit').unbind(); });", 'inline');

Source: How do I disable ajax on file fields?