3

I have a profile2 profile type. In profile type i have created the collection for storing the information of form fields.

I have used the phone module to create the phone number fields. This collection contains the two phone number fields, one for office and one for mobile.

So my scenario is give below

--Profile type

--- Field collection to store the phone numbers

---- Two fields to store  phone numbers
    1. field_office_phone
    2. field_office_mobile

Now i want to save the data of the field programmatically. I have used below snippet from here to create and save the field collection programmatically. In the given below code company is my profile2 profile bundle.

module_load_include('inc', 'field_collection', 'field_collection.pages');
  // Load the profile we're adding the field to
  $profile_entity = profile2_load_by_user($account, 'company');

  // Define the values of the new field
  $values = array();
  $values['field_name'] = 'field_contact_number'; // field collection name
  $values['field_office_phone'][LANGUAGE_NONE][0]['#value']['number'] = 1234;
  $values['field_office_mobile'][LANGUAGE_NONE][0]['#value']['number'] = 12374;

  // Create the field collection entry and associate it with the profile
  $field_entity = entity_create('field_collection_item', $values);
  $field_entity->setHostEntity('profile2', $profile_entity);
  $field_entity->save();

The above code snippet only creates field but it is not storign the values in the field. I am not getting the reason why it is not setting the values.

1 Answer 1

2

This code works fine for user entities under Drupal 7:

  $values['field_office_phone'][LANGUAGE_NONE][0]['number'] = '0412345678';
  $values['field_office_phone'][LANGUAGE_NONE][0]['country_codes'] = 'au';

Adapt countrycode to your user's country. I don't think I'm using Profile2 and there are multiple different modules that implement phone number fields. My server has cck_phone-7.x-1.x-dev installed.

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.