0

how do I set value for the mobile_number field using entity wrappers? Currently, this field is attached to an entity and I'm trying as below.

$entity_type = 'mp_customer';
$entity = entity_create($entity_type, array('type' => 'mp_customer')); $wrapper = entity_metadata_wrapper($entity_type, $entity); $wrapper->field_phone = "0777123456";

This gives an PDO Exception .... field_phone_value cannot be null....

Adding more This field needs more data to store in field_data_field_phone, those are: {field_phone_value} {field_phone_country} {field_phone_local_number} {field_phone_verified} {field_phone_tfa}

This field was created using this module : https://www.drupal.org/project/mobile_number

  • Hi @Mahinda, Please do not leave your question with no answer accepted if you want others keep helping you. Alternatively, you can provide and accept your own solution. Whatever you do, just do not abandon your questions as next time less people want to react when you ask help. – AltaGrade Mar 23 '18 at 11:39
  • Hi there, Inserting values as an array didn't work. It throws an error as invalid data. So, I'm still looking for a solution save values using entity wrappers. Because using entity_save() is not actual solution. – Mahinda Mar 23 '18 at 17:15
1

To store a value with entity wrapper set() should be used. Please read https://www.drupal.org/docs/7/api/entity-api/entity-metadata-wrappers

So your code $wrapper->field_phone = "0777123456"; would be $wrapper->field_phone->set('0777123456'); if the field structure required just a simple string.

However, according to the error message, it is not a string as it requires additional values for {field_phone_value} {field_phone_country} {field_phone_local_number} {field_phone_verified} {field_phone_tfa}.

So to make sure I would first figure out its structure. You could do so, for example, saving any value via UI and then outputting the value of respecting variable using dpm(). However, most probably its an array and you could give a try to:

$phone = array(
  'field_phone_value' => '0777123456',
  'field_phone_country' => 'US',
  'field_phone_local_number' => 'some value',
  'field_phone_verified' => 'some value',
  'field_phone_tfa' => 'some value'
);
$wrapper->field_phone->set($phone);
  • It's not working, PDO Exception error throw with INSET INTO {field_data_field_phone} {entity_type}{entity_id}{revision_id}{bundle}{delta}{field_phone_value}{field_phone_country}{field_phone_local_number}{field_phone_verified}{field_phone_tfa} VALUES(); field_phone_value field_phone_country field_phone_local_number field_phone_verified field_phone_tfa saying these values are NULL – Mahinda Mar 17 '18 at 14:56
  • Check the edited answer. – AltaGrade Mar 17 '18 at 15:51
  • Finally, I figured out save this using entity_save()... But entity wrappers are not working..invalid data format. – Mahinda Mar 17 '18 at 17:57
  • Because as I tried explain above, your are feeding just a string instead of array. Read the edited answer. – AltaGrade Mar 17 '18 at 20:43

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.