I've set up my own custom multi-step registration form, because I just couldn't find a way of making anything out there do what I want. I've found other questions on here like this one and this one which have basically got me 95% there. My main problem now is that I've got a series of field collections in my Profile2 fields, and I just can't seem to find out what my code needs to be in order to get it done properly.
*At this point in the code I've created a new user ($user), and their profile2 ($profile2) and I'm trying to format my fields from my form so that I can throw them all into the $profile2 object.
field_activities
- field_start_year
- field_end_year
- field_text
My code so far is:
$field_collection = entity_create('field_collection_item', array('field_name' => 'field_activities'));
$field_collection->setHostEntity('profile', $profile2);
$field_collection->field_start_year = $entry['start_year'];
$field_collection->field_end_year = $entry['end_year'];
$field_collection->field_text = $entry['text'];
$field_collection->save(FALSE);
And the error I'm getting is:
Fatal error: Call to a member function
setHostEntity()
on a non-object
Which would make more sense to me if $profile2
wasn't an object, but it is.
I've got too many unknowns to figure this out at the moment, is it the right code? can I build a create a field collection item on a newly created profile2 object? do I populate all the fields of the profile object first, and then once it's saved, then add the field collections, etc.
profile2
, notprofile
, so I would guesssetHostEntity('profile', $profile2);
needs to besetHostEntity('profile2', $profile2);
The error you're getting is saying that$field_collection
is not an object, it doesn't mention the$profile2
variable