1

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?

2
  • What does your $file contain?
    – GwenM
    Commented Feb 5, 2016 at 21:50
  • $file is a full file object returned by remote_stream_wrapper_file_create_by_uri(). I checked it against other files and it looks exactly the same.
    – yotka
    Commented Feb 7, 2016 at 10:05

1 Answer 1

4

One issue may be that get_object_vars() returns an array with keys that the file field does not understand. The EntityMetadataWrapper documentation provides two options for setting file reference values, though it appears to use single value fields in the examples:

Setting the file directly:

$w_containing_node->field_attachment_content->file->set( $file_obj );

Providing an array with just fid:

$w_containing_node->field_attachment_content->set( array('fid' => $fid) )

One of the comments provides the following examples from the entity tests which apply to a multi-value field, though I'm not sure if setting the full file object will work appending a new item:

$wrapper->field_file[0] = array('fid' => $file->fid, 'display' => TRUE);
$wrapper->field_file[1]->file = $file;
2
  • Using an array with just fid I get the mentioned "Invalid data value given" error. Setting the file directly through file->set(), I get "Call to a member function set() on a non-object" (both with $file being an object or an array). But ´$wrapper->field_video_ext[0] = array('fid' => $file->fid, 'display' => TRUE);´ seems to work. I'll re-check that and get back.
    – yotka
    Commented Feb 7, 2016 at 10:03
  • Looks good, so I guess $wrapper->field_video_ext[0] = array('fid' => $file->fid, 'display' => TRUE); is the right answer. Thanks!
    – yotka
    Commented Feb 8, 2016 at 19:35

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.