I have been able to use the great answers at https://forum.backdropcms.org/forum/entity-metadata-wrappers-usage-questions for all my other fields, but am having problems with a file field.

In this case, what is the proper value to set it to? I am doing something like:

$wrapper->my_filefield = $my_value;

I have tried FID and an actual file object for $my_value, but it didn't like either one. This does work:

$wrapper->my_filefield = (array) $file;

Is this correct? Or is there a better way to do it?

Most helpful answers

OK, another workaround, but using wrappers. This works:

$wrapper->field_a_file = array( 'fid' => 1,  'display' => 1 ); 

The property "display" is required in Backdrop.

If this is a file reference field, you have found a bug!! In D7, core's file reference fields were a lot simpler than in Backdrop (Backdrop integrated more advanced file entities into core). 

I'll look more closely into this. In theory, the wrapper should be a "struct wrapper" or structure wrapper, similar to date fields, and you should be able to specify something like

$wrapper->field_a_file = array( 'fid' => 1 ); 

But this doesn't work .

As a workaround for now avoid using wrappers. Set directly on the node object:

$node->field_a_file['und'][0]['fid'] = 1;
$node->field_a_file['und'][0]['display'] = 1;

Comments

Leeksoup, can you verify what type of field that is (file reference?) and its cardinality?

 

It is an "Image" field with a cardinality of 1.

If this is a file reference field, you have found a bug!! In D7, core's file reference fields were a lot simpler than in Backdrop (Backdrop integrated more advanced file entities into core). 

I'll look more closely into this. In theory, the wrapper should be a "struct wrapper" or structure wrapper, similar to date fields, and you should be able to specify something like

$wrapper->field_a_file = array( 'fid' => 1 ); 

But this doesn't work .

As a workaround for now avoid using wrappers. Set directly on the node object:

$node->field_a_file['und'][0]['fid'] = 1;
$node->field_a_file['und'][0]['display'] = 1;

OK, another workaround, but using wrappers. This works:

$wrapper->field_a_file = array( 'fid' => 1,  'display' => 1 ); 

The property "display" is required in Backdrop.

@argiepiano - OK, this will work for me. Thank you. I appreciate all the other info, too.