Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By fininho on
Hi,
I've tried to search in the APIs but I didn't find anything helpful. I've installed the upload_element module, it works fine and uploads the images. I've altered a form (it's a Ubercart 'add to cart' one) in order to have it displayed as a new field into the form, but now I'd like to hook the form submit in order to associate that image file to something else (in this case, with the order being created with the form). How do I catch the image's data on submit? Is there a hook that lets me process any data after the submit function?
Thanks,
m.
Comments
..any hint on the submit
..any hint on the submit hook?
Forms in drupal can have
Forms in drupal can have multiple submit functions. Because you probably want to let the original submit function do its thing as well, you will want to add your function to the $form['#submit'] array rather than just replace it. Here is an example:
Then in your new submit function you can do whatever you like. Make sure you do not include the ?> in your code, it breaks drupal.
- Ryan
Thanks, it's a useful hint.
Thanks, it's a useful hint. I've tried to use it, I've altered the original form adding an image upload field, and every new #submit is added to the original form submit chain. I want to catch the moment in which the image file is uploaded, and I don't see any embedded form ito the original one with its own form_id, so I can't modify the image's form. Even hook_insert is not useful, I just can see the image uploaded in 'files' table and I can't catch its parameters.
The original form containing the image is updated with the image's thumbnail, but it does not have any other parameter linked to the image uploaded.
So, how can I link that image to the other fields in the form?
Thanks,
m.
You can catch all the info
You can catch all the info for your file by declaring a variable in your submit function, something like this:
- Ryan
Thanks again, Ryan. I didn't
Thanks again, Ryan. I didn't understand at first, but it has been a help. Following the solution to my problem for anyone needing it.
The code to add the image field and upload to the original form:
The function to manage data, here it just sets a message with some useful params but you should use this to store data:
And this is valid for every form using upload_image, apart from uc_cart_id param. Now some more info, as I'm using this to add an image to an order.
Since I'm managing an order with Ubercart, here you can find other data caught while adding to cart. Again, just setting a message here:
The rest of the code will be the insertion of uc_cart_id, fid and order_id in a custom table, to have the image(s) linked to the order.
Hope it helps,
m.
Drupal Form Submit Functions
Just thought I'd weigh in on this as it has helped me considerably... (thanks, Ryan)
The submit functions are executed in order with the original function at the head of the array.
Ryan's code above will add your new submit function to the end of the array.
If you need to pre-process some of the form data before the original submit function sees it, the array_unshift function will prepend an element to the submit function array, allowing your added submit function to be first in the execution order.