I'm trying to accomplish the seemingly simple task to add a value to a multi-value file field in Drupal 7. I have been trying for hours, but none of the answers I found would solve my problem.
My field is called field_video_ext
. Using the Remote Stream Wrapper module, I would like to add values like this (using some of the module's code):
<?php
// My videos and their respective nodes.
$videos = array(
array(
'nid' => '1903',
'uri' => 'http://example.com/movie.flv',
),
array(
'nid' => '3172',
'uri' => 'http://example.com/movie2.flv',
),
);
foreach ($videos as $video) {
// From the Remote Stream Wrapper module.
try {
$file = remote_stream_wrapper_file_load_by_uri($video['uri']);
if (!$file) {
$file = remote_stream_wrapper_file_create_by_uri($video['uri']);
file_save($file);
}
}
catch (Exception $e) {
drupal_set_message($e->getMessage());
}
if (empty($file->fid)) {
drupal_set_message(t('Unable to add file from URL %file.', array('%file' => $video['uri'])));
exit;
}
// Display needs to be set.
$file->display = 1;
// Get Entity Metadata Wrapper
$wrapper = entity_metadata_wrapper('node', $video['nid']);
// Write information into fields.
$wrapper->field_video_ext->set(get_object_vars($file));
// Save wrapper.
$wrapper->save();
}
?>
Now the problem is that this doesn't work if the field accepts multiple values. It throws the error
EntityMetadataWrapperException: Invalid data value given for wrapper chain
I then tried using $wrapper->field_video_ext[] = get_object_vars($file);
, but that doesn't work either. I think it was this error I got:
Fatal error: Cannot use object of type Project as array
I also tried the following instead of get_object_vars($file)
:
$file = (array) $file;
$items = array($file);
And then saving that array using set()
or the []
way. But to no avail. Can anybody tell me what I am doing wrong?