Hi
I realise this issue has come up before, and I have tried every possible JSON format described in the following issues but we are still having problems with this. We are unable to PUT an image into the "Picture" field.
https://www.drupal.org/node/1309844
https://www.drupal.org/node/2061225
https://www.drupal.org/node/2424199
So steps to reproduce.
- We already have an image in the drupal system that we are using.
- We are using the user update form.
- We are using PUT not POST.
- We are using JSON.
- All other fields are edited without issue.
- But the picture never works.
- We have tried many different suggestions as shown in the 3 above issues, including naming picture or picture_upload.
- However the field is always empty.
So the problem I think is due to the picture_upload expecting to be an object, but via JSON this always ends up in the $account variable as an array. Consequently it fails.
So I'm proposing 2 fixes for this. Both of which I have written but I want the maintainers input into this before I upload them.
Patch 1. Simply checks if picture_upload is an array and if so turns it into an object. This is good but the difficulty here is that the JSON would have to be populated with the entire file object which the source providing the JSON may not have.
or
Patch 2. Propose a variable in the JSON called "picture_fid"
- If the picture_upload is already populated then it is ignored.
- Otherwise it will take the picture_fid if it exists, return the file object with file_load and populate picture_upload with it..
If I have missed something here and this is not a problem then could someone please let me know how to assign an image to this field with JSON.
Thanks
| Comment | File | Size | Author |
|---|---|---|---|
| #4 | picture_uploadfix-2657380-4.patch | 1.24 KB | DrupalChimp |
| #2 | picture_uploadfix-2657380-1.patch | 1.26 KB | DrupalChimp |
Comments
Comment #2
DrupalChimp commentedOk, So I have submitted this patch. It basically allows you to upload a fid as string or int. or the full file object as an object/array in the JSON.
So examples would be
Comment #3
kylebrowning commentedI like the patch in #2
Comment #4
DrupalChimp commentedMade a mistake.
Comment #5
DrupalChimp commentedComment #6
kylebrowning commentedComment #8
marcingy commentedMerged thanks
Comment #9
JMTorres commentedThis commit breaks my site on any page that loads user_resource.inc with:
"PHP message: PHP Fatal error: Can't use function return value in write context in .../sites/all/modules/services/resources/user_resource.inc on line 400"
According to http://php.net/manual/en/function.empty.php , empty() only supports variables prior to PHP 5.5.
Automated testing should be enabled for the project on drupal.org to catch these types of issues.
Comment #10
JMTorres commentedComment #12
kylebrowning commentedFixed in DEV!