3

EDIT: As per a suggestion below, I tried using hook_field_presave, but couldn't access any parameters of the field. I then tried hook_field_attach_presave, but my alterations aren't being saved. My current module code is :

function module_field_attach_presave($entity_type, $entity) {
  $field = $entity->field_user_location;
  $locality = $field['und'][0]['locality'];
  $locality = ucwords(strtolower($locality));
  dpm($locality);
  }

The transformation is present when Locality prints out via dpm, but the transformation is not saved to the field itself. Any idea why?

ORIGINAL: I'm using an AddressField field in my user profiles, but I'd like to make sure that the cities that users enter are correctly capitalized. How can I do this?

I'm assuming it's some implementation of str_replace, but I don't know what to do with it.

5

You'll need to implement hook_field_presave(). You can find out from the parameters passed in if it's being invoked for an address field saved to a user account and do the necessary transformation on the 'locality' of the address field value in the $items array.

  • As per drupal.stackexchange.com/questions/52448/…, it seems like I need to use hook_field_attach_presave since the field is being implemented by another module. However, with that hook I do not have access to $items, or anything besides $entity and $entity_type. None of my changes are persisting through the save though when I try and access the locality information through the field via $entity. Any suggestions? When I just use hook_field_presave, I can't even dpm any of the parameters. – Mrweiner Jul 5 '13 at 23:25

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.