Question

$wrapper = entity_metadata_wrapper('myentity',$entity); 
$data[$key]['field_test'] = $wrapper ->field_test ->value();

the above code works fine but it doesn't work if i replace the chained property with a variable.

$field_name = 'myfield';
$wrapper = entity_metadata_wrapper('myentity',$entity); 
$data[$key][$field_name] = $wrapper ->$field_name ->value();

the error:'PHP Error: Function name must be a string'

How can i work around this? Thank you

Was it helpful?

Solution

change:

$data[$key][$field_name] = $wrapper->$field_name->value();

to

$data[$key][$field_name] = $wrapper->{$field_name}->value();

See:: Curly Syntax

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top