4

I have a custom field called phone_number for the user. I need to retrieve the data in it from the database and use it to call other functions. I've looked through the Field API and I can't find the specific function. I read somewhere in the Field Attach API that most of the functions there take $entity as a variable but none of the functions there gives me what I need. Please help.

3 Answers 3

1

There are two solutions: use Entity API (http://drupal.org/project/entity) and create an object wrapper around the entity and just $phone = $wrapper->phone_field.

Or use the field_extract module (http://drupal.org/project/1158878) which provides two functions for extracting data from Field API fields.

Or third, code it yourself, but you don't want to do that.

(I wrote the field_extract module so I am biased.)

2

Use Field data extraction API module to accomplish your job of field data extraction.

As per the documentation, you have to write this in yourmodule

  $phonenumbers = field_extract_values('node', $node, 'phone_number');

  $result = someCustomFunction($phonenumbers);

  function someCustomFunction($user_phonenumbers){

   // Do something here and return value

   }
0

The entities already loads a field data. if you have attached some fields to user you can use this as $user->myfield but sometimes in my practice field data has not appered. In that case you can load a entity load functions like node_load() or user_load()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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