0

I am trying to update a user programmatically in Drupal 7.

User records have multiple custom fields, configured via admin interface #overlay=admin/config/people/accounts/fields

My code sets text and datetime fields fine, but fails to update a phone number field.

Error message:

EntityMetadataWrapperException: Unknown data property field_mobilephone.
in EntityStructureWrapper->getPropertyInfo()
(line 335 of /Applications/MAMP/htdocs/sites/all/modules/entity/includes/entity.wrapper.inc)

Code:

  $new_user = user_save(null, $fields);

  $obj = entity_metadata_wrapper('user', $new_user);
  $obj->field_firstname->set($_REQUEST['firstname']);
  $obj->field_lastname->set($_REQUEST['lastname']);
  $obj->field_mobilephone->set($_REQUEST['mobilephone']);

When I do var_dump($obj) I can see all configured text fields in the output, but not the phone number fields.

Field type is 'phone number', provided by the cck_phone-7.x-1.x-dev module (I couldn't find any other place where it could come from).

So question is how do I set phone number field via entity_metadata_wrapper?

4
  • What type of field phone number field is? Is text too? is a field type provided by a module? – sanzante Jul 9 '14 at 9:03
  • It is of type 'phone number', provided by cck_phone-7.x-1.x-dev. I've updated the question. – Paul Jul 9 '14 at 10:16
  • $obj->field_mobilephone->set(array('number' => $_REQUEST['mobilephone']));? – Clive Jul 9 '14 at 10:38
  • @Clive, that doesn't work because even simplest $obj->field_mobilephone; produces the same EntityMetadataWrapperException: Unknown data property field_mobilephone – Paul Jul 10 '14 at 4:23
0

I got around this problem by using the following snippet just before the user_save(..) call:

$fields['field_mobilephone'][LANGUAGE_NONE][0]['number'] = $_REQUEST['mobilephone'];
$fields['field_mobilephone'][LANGUAGE_NONE][0]['country_codes'] = 'au';

This approach bypasses the entity_metadata_wrapper altogether. Pretty sure there's a cleaner way, but I'm not yet aware of it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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