1

So I want to update a user image field (let's say field_picture for the sake of convenience).

Preferably I only want to make one AJAX call rather than two, so I'd prefer to not have to create the file, get the fid back and pass that to the user.

Is there anyway to just pass in a local URI or anything like that, or do I need to essentially do two calls?

2
  • Overwrite the original file so the FID will still the same to the new file? Not sure if I catch what you wanted.
    – Gulok
    Commented Sep 5, 2016 at 2:22
  • No, I want to upload a completely new file, but without an FID. I believe I found a solution and will post it later.
    – Jack Ryan
    Commented Sep 6, 2016 at 3:21

1 Answer 1

0

Ok I am going to post what I did to make this work with services. I'll also include a bit of information about my specific use-case because I don't see any list of steps anywhere else on the internet to make this happen, so maybe it'll make it easier for someone along the line.

Here's the optional part of the answer:

I was doing this in PhoneGap. To pass the image data from a mobile device via PhoneGap to a user, I did the following:

navigator.camera.getPicture(saveProfile,
                            function(message) { alert('get picture failed'); },
                            { quality: 50, 
                            destinationType: navigator.camera.DestinationType.DATA_URL,
                            sourceType: navigator.camera.PictureSourceType.PHOTOLIBRARY }
                            );

function saveProfile(image) {
json.image = image;
}

                $.ajax({
                    url: "http://drupal/services_endpoint" + uid,
                    type: 'PUT',
                    contentType: 'application/json',
                    data: JSON.stringify(json)
                }
                );

And now the actual answer:

After finding this link here: Create file through services

I released I could do something similar:

function MY_MODULE_services_request_preprocess_alter($controller, &$args, $options) {
  switch ($controller['callback']) {
    case '_user_resource_update':
      if (isset($args[1]['image'])) {
        $data = base64_decode(trim($args[1]['image']));
        $finfo = finfo_open();
        $mime_type = finfo_buffer($finfo, $data, FILEINFO_MIME_TYPE);
        finfo_close($finfo);
        $ext = $mime_type ? str_replace('image/', '', $mime_type) : 'png';
        $file_name = uniqid();
        $file = file_save_data($data, 'public://users/' . $file_name . '.' . $ext, FILE_EXISTS_REPLACE);
        file_usage_add($file, "user", "user", $args[0]);
        $args[1]['field_picture'] = array(
          'und' => array(
            0 => array(
              'fid' => $file->fid,
            ),
          ),
        );
      }
  }
}

This will generate a random file name. I'm not sure if that'll work perfectly for everyone, but it worked for my use-case.

Anyway, that is a basic way to get image data from a mobile device (iPhone in this case) into a Drupal user in one RESTful call.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.