[support] D7: Loading full objects attached with Field API?

Hadubard hadubard at gmail.com
Sat Sep 29 15:40:52 UTC 2012


Hi,

I'd say you could simply call user_load($user->uid). If that doesn't do the trick have a look at field_attach_load().
Btw: I consider it bad practice to overwrite the global $user object.

Am 29.09.2012 um 16:36 schrieb Georg Jaehnig:

> Hi,
> 
> I have a question about loading fields attached to an entity.
> 
> I have a field called 'field_custom_namespaces' attached to the user
> entity. field_custom_namespaces is a taxonomy term reference.  Whats
> the best and shortest code to load all the attached field objects,
> i.e. term objects?
> 
> Currently I do it that way which seems totally bloated:
> 
>  global $user;
>  $user = entity_load_single("user", $user->uid);
>  if (!empty($user->field_custom_namespaces)) {
>    $namespace_ids_wrapped = $user->field_custom_namespaces['und'];
>  } else {
>    $namespace_ids_wrapped =  array();
>  }
>  foreach ($namespace_ids_wrapped as &$namespace_id_wrapped) {
>    $namespace_id_wrapped = $namespace_id_wrapped['tid'];
>  }
> 
>  $namespaces = taxonomy_term_load_multiple($namespace_ids_wrapped);
> 
> So is there an easier, maybe "official" way with a proper function?
> 
> 
> -- 
> Georg | http://serchilo.net - command the web
> -- 
> [ Drupal support list | http://lists.drupal.org/ ]



More information about the support mailing list