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?
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/ ]
Hi,
On Sat, Sep 29, 2012 at 5:40 PM, Hadubard hadubard@gmail.com wrote:
I'd say you could simply call user_load($user->uid).
No, it eventually also calls entity_load_single("user", $user->uid);
If that doesn't do the trick have a look at field_attach_load().
Yeah, also tried that: http://api.drupal.org/api/drupal/modules%21field%21field.attach.inc/function...
"field_attach_load($entity_type, $entities, $age = FIELD_LOAD_CURRENT, $options = array())
Loads fields for the current revisions of a group of entities. Loads all fields for each entity object in a group of a single entity type. The loaded field values are added directly to the entity objects."
... but it does nothing here. And I actually can't see how this "loaded field values are added directly to the entity objects" shall happen, as you don't pass $entities as a reference. So how could the function change $entites?
Btw: I consider it bad practice to overwrite the global $user object.
Oh right, yeah, good point!