I have a added data to the user using:

taxonomy_term_reference

when creating the

field_create_instance($instance)

and everything is working fine, I can add and store values and these are added to the specified vocabulary.

The problem is, I cannot extract the value added as a term to the $user object using field_get_items and field_view_value since field_get_items('user', $user, 'field_name') is empty.

Is field_get_items and field_view_value not applicable to $user?

Code example:

hook_install

function _customers_fields() {
  return array(
    'phone' => array('text', 'text_field'),
    'contact' => array('text', 'text_field'),
    'city' => array('text', 'text_field'),
    'postalcode' => array('text', 'text_field'),
    'address' => array('text', 'text_field'),
    'company' => array('taxonomy_term_reference', 'taxonomy_autocomplete'),
  );
}

function customers_install() {
  # Here we install the role
  $roles = _customers_roles();

  foreach ($roles as $role) {
    if (!user_role_load_by_name($role)) {
      $item = (object) array('name' => $role);
      
      user_role_save($item);

      watchdog('Customers Module', 'Role '. $role .' being created');
    }
  }
  # TODO: Term reference to location
  $fields = _customers_fields();

  foreach ($fields as $name => $type) {
    if (!field_info_field($name)) {
      $field = array(
        'field_name' => $name,
        'type' => $type[0]
      );

      field_create_field($field);

      watchdog('Customers Module', 'Field '. $name .' being created');

      $instance = array(
        'field_name' => $name,
        'entity_type' => 'user',
        'label' => t('Customer '. ucfirst($name)),
        'bundle' => 'user',
        'required' => FALSE,
        'settings' => array(
          'user_register_form' => TRUE
        ),
        'widget' => array(
          'type' => $type[1]
        )
      );

      field_create_instance($instance);

      watchdog('Customers', 'Instance '. $name .' being created');
    }
  }
}

?>

and then inside different module

$company = field_get_items('user', $user, 'company');

debug($company, null, true);

is empty. I can add, edit and view the "company" term using /admin

Comments

Bagz’s picture

Yes, field_get_items is applicable to $user. It probably would help if you showed some code that we could follow and perhaps spot where the problem may be.

kristian’s picture

I have added the code snippet that I wrote to create the fields on the user

Bagz’s picture

Are the fields visible in admin/config/people/accounts ?
Is this the only field that has this problem, does your code work for the other fields?

kristian’s picture

Yes they are, I can add and edit data when I access the fields using /user/*/edit, I can see them and edit the data, I'm using the default display for all these fields

Bagz’s picture

Drupal usually adds 'field_' to the start of the field name, have you tried field_get_items('user', $user, 'field_company') ?

Other than that, I don't know why it would not work..

kristian’s picture

you can get the field like this field_get_items('node', $node, 'event_customer') (no 'field-') that is how I do it else where in the same application and it's working.

jazzdrive3’s picture

I was experiencing the same issue. field_get_items was always empty. The problem was that the $user object you get when you do a

global $user

isn't the full entity. So when you pass it into the field_get_items function, it doesn't match anything.

Do a full user_load on the $user->uid and THEN pass the resulting object to field_get_items, and everything should work.

kapadokis’s picture

Thank you, your suggestion fixed my problem.