1

I am trying to get a remote pdf file from a web service and save it as a field's value using entity_metadata_wrapper, but somehow I couldn't get it working.

I can successfully retrieve the file using system_retrieve_file and save it locally. However, I couldn't set the file field's value to this newly retrieved file.

Here is my code (simplified):

// Retrieve the file from the web service.
// This works, I can see and open the retrieved file.
$file = system_retrieve_file($file_url, 'public://path/', TRUE);
$file = (array) $file;
var_dump($file);

$ewrapper = entity_metadata_wrapper('node', $nid)

// Set this file as a value for the node's file field.
// I've tried following lines individually and they both failed.
$ewrapper->field_pdffile->set($file);
$ewrapper->field_pdffile->set(array($file));

Both methods I've tried raised the following error:

EntityMetadataWrapperException: Invalid data value given. Be sure it matches the required data type and format. in EntityMetadataWrapper->set() (line 122 of /drupal/sites/all/modules/entity/includes/entity.wrapper.inc).

I'm setting in the same code block other fields as well and they are set and saved without any error if I skip the file field.

For example, the following lines are working in the same code block for other fields:

// I'm getting the text from the web service
$ewrapper->field_text->set($some_text);
// I'm getting a term from web service
// Creating it if it doesn't already exist, and saving the 'term id' like:
$ewrapper->field_term->set($tid);
// A few more various fields are successfully set like that...
// Except that file field, which returns the above exception:
$ewrapper->field_pdffile->set($file);

What could it be, that I'm doing wrong?

UPDATE:

I've added a var_dump($file) and I'm getting the following output, which seems alright to me:

array(17) {
  'fid' => string(5) "13037"
  'uid' => string(1) "1"
  'filename' => string(31) "sample.pdf"
  'uri' => string(54) "public://path/sample.pdf"
  'filemime' => string(15) "application/pdf"
  'filesize' => string(6) "129804"
  'status' => string(1) "1"
  'timestamp' => string(10) "1409924961"
  'type' => string(8) "document"
  'uuid' => string(36) "a06f6bf7-61e1-48fd-bfc7-8bff6e72705f"
  'metadata' => array(0) {}
  'alt' => string(0) ""
  'title' => string(0) ""
}
  • Why not to use FileField Sources or "borrow" it's code? ;) – Mołot Sep 5 '14 at 14:04
  • @Mołot I didn't know about this module, but it wouldn't help me much since I'm not only saving this field, I am actually importing the whole node from a webservice. I can successfully save all other fields except this file field using entity_metadata_wrappers. – Елин Й. Sep 5 '14 at 14:07
  • Well, I have no experience with this kind of job, I just know this module works in the part you seem to have problem with, and it is open source, so maybe you could use the fact it is open source? Not an answer, just a rough idea. – Mołot Sep 5 '14 at 14:08
  • 1
    If it would used entity_metadata_wrapper to save the field, I could get some help from their code, but it doesn't. As you would already know, there are many ways to save fields programmatically, but the recommended way is using entity_metadata_wrapper. Since I'm using this method for all other fields and it works, I would be happy to save this file field as well using EMW, so that I don't have to rewrite the whole module using field arrays, which is, let's admit, ugly, such as: $field_name['und'][0]['fid']. And inconsistent for all fields. – Елин Й. Sep 5 '14 at 14:20
  • @ЕлинЙ. Did you try to display the output of $file variable (with var_dump or devel)? Are you sure you're getting the file object with system_retrieve_file()? this error can be seen If you set the field as required and if it gets null value, or wrong type (you can see it in validate method in entity.wrapper.inc). Check this, set method is working fine with typecasted object (stdclass).. – xurshid29 Sep 5 '14 at 17:59
1

Since @xurshid29 never post the correct answer here, I am posting it cause I find it "hard" to retrieve the comments:

(from @ЕлинЙ post)

// Retrieve file from web service (this works, I can see and open the retrieved file)
$file = system_retrieve_file($file_url, 'public://path/', TRUE);
$file = (array) $file;
$file['display'] = 1;
$file['description'] = '';

$ewrapper = entity_metadata_wrapper('node', $nid)

// Set this file as a value for the node's file field.
$ewrapper->field_pdffile->set($file);

More info on system_retrieve_file

And thanks to both of you for your work!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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