0

I have a custom entity with a file field accepting multiple values. I would like to set file values according to https://www.drupal.org/docs/7/api/entity-api/entity-metadata-wrappers#example-value-set-save but none of the two methods are working here. I always get this error

error  php  Error: Call to a member function set() on null in include().

I am running Drupal 7, file_entity module enabled. Here follows the code:

$file = file_load(8172);
$wrapper = entity_metadata_wrapper('sys_ind', 13718);
$wrapper->field_allegati->file->set( $file );
$wrapper->save();

2 Answers 2

1

Fixed! Thanks to this post Save a new value with entity_metadata_wrapper to an entity field which is an array

$file = file_load(8172);
$node = entity_load('sys_ind', array(13718));
$wrapper = entity_metadata_wrapper('sys_ind', '13718');

$wrapper->field_allegati[NULL]->file->set( $file ); # This worked

$wrapper->save();

so the fix is using NULL (at least in multivalue field like mine is), not 0 or any other index nor [].

0

Try this to see if it works:

$wrapper->field_allegati[0] = (array) $file;

The wrapper seems to struggle with file fields

1
  • nope, just tried, error changes to EntityMetadataWrapperException: Invalid data value given. Be sure it matches the required data type and format. Value at [error] sys_ind(13718)->field_allegati: array ( 0 =>
    – rmorelli74
    Jan 10, 2019 at 15:47

Your Answer

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

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