0

I wanted to post to @Clive comment on How can I quickly re-save all users on my site? and ask about how to do this for fields in a field collection but couldn't because I've never posted to this site before so I had to ask a new question. He provided the following code to run as a php script in VBO on all users:

$items = field_get_items('user', $entity, 'field_name'); 
if (empty($items)) { 
   $entity->field_name[LANGUAGE_NONE][0]['value'] = 1; 
}
user_save($entity);

This solution works great for a single field of a user. I need a similar solution for setting default field values in a field collection after importing users.

I migrated 6000+ users and have set default values for fields in a field collection after the fact that don't show up unless I re-save a user account. I want to bulk re-save all 6000+ users so that the default field collection fields are saved and are visible. It's almost identical to the original question except the field is not just a singular field associated with the entity -- the field(s) in question are part of a field collection. How do I access the fields from my default field collection to then force re-save of users?

Is the proper way to access field data through entity_metadata_wrapper? If this is the case, how do I get/set the values of fields in a field collection that I want to associate with users using this wrapping class?

closed as unclear what you're asking by Mołot, 4life, Free Radical, monymirza, niksmac Nov 12 '13 at 3:53

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

1

This finally worked for me -- may it save someone hours of headaches:

// load the user
$userwrapper = entity_metadata_wrapper('user', $entity);
// apparently you have to create the field collection entity
$fieldCollection = entity_create('field_collection_item', array('field_name' => 'field_yourCollectionName'));
// attach your field collection to the user
$fieldCollection->setHostEntity('user', $entity);
// set the value of one of your fields in your field collection - in my case it's a boolean
$userwrapper->get('field_yourCollectionName')->field_fieldNameInYourCollection->set(0);
// save the entity
$userwrapper->save();

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