Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
My $user object is wrapped in entity_metadata_wrapper, but I can't use $user->realname->value(). We need to add a getter callback to the entity property info in order for this to work.
Comment | File | Size | Author |
---|---|---|---|
#11 | realname-getter-callback-2512394-11.patch | 583 bytes | luizps |
Comments
Comment #1
mortona2k CreditAttribution: mortona2k at Forum One commentedHere's a patch. I'd like feedback on the naming of the getter callback, and if there's any more checking we should do on $account within it.
Comment #2
heddnMe thinks this can just use entity_property_verbatim_get.
Nit: Missing trailing new line.
Comment #3
mortona2k CreditAttribution: mortona2k at Forum One commentedThanks very much for your feedback, entity_property_verbatim_get works great.
Comment #4
mortona2k CreditAttribution: mortona2k at Forum One commentedComment #5
heddn+1 on #3
With a visual review, everything looks good. But I haven't actually tested the module with entity api either.
Comment #6
heddnHad a chance to test #3 against a commons site. It works fine.
Comment #7
rakesh.nimje84@gmail.com CreditAttribution: rakesh.nimje84@gmail.com commentedfunction realname_entity_property_info_alter(&$info) {
$properties = &$info['user']['properties'];
$properties['realname'] = array(
'label' => t("Realname"),
'description' => t("The Realname of User."),
'type' => 'text',
'getter callback' => 'entity_property_verbatim_get',
);
}
Comment #8
heddnre #7: Is that a +1 on RTBC or some other type of feedback?
Comment #9
hass CreditAttribution: hass commentedI have no idea how this works. Can you add tests, please?
Comment #10
edaa CreditAttribution: edaa commentedMaybe the thing you really need is user's entity label rather than the realname, just instead use
wrapper->label()
, as$wrapper->label()
will take into account the realname.But If you update the user then reload the user, its label does not change as the 'realname_load_multiple' cache not updated,
see #2620882: Reset realname cache for the updated user
Comment #11
luizps CreditAttribution: luizps at CI&T for Pfizer, Inc. commentedI made some changes in the code because I had some problems with the entity module. I listed the errors below:
Notice: Undefined index: label in entity_views_field_definition() (line 191 of /modules/contrib/entity/views/entity.views.inc).
Notice: Undefined index: label in entity_token_token_info_alter() (line 93 of /modules/contrib/entity/entity_token.tokens.inc).
Comment #12
caminadaf CreditAttribution: caminadaf at CI&T for Pfizer, Inc. commentedPatch #11 worked fine without the mentioned errors.
+1 RTBC
Comment #13
hass CreditAttribution: hass commentedStill needs tests.
Comment #14
edaa CreditAttribution: edaa commentedWhy not instead use $user_wrapper->label()? label() returns the realname value.
Comment #15
NWOM CreditAttribution: NWOM commented#11 worked great. In my use-case I had to index the realname in a Search API index. This patch made that possible. Thank you!
Comment #16
caminadaf CreditAttribution: caminadaf at CI&T for Pfizer, Inc. commented@edwardaa
You are right, using ->label() works because of :)
However, there may be some cases where the label() is also modified by other modules and a developer may want to access what realname generates instead.
@hass
What kind of tests are you thinking of?
Comment #17
markusd1984 CreditAttribution: markusd1984 commentedStupid question but is this supposed to add [realname] as a field to the entity type user?
[base table field types] => Array
(
[uid] => int
[name] => varchar
[pass] => varchar
[mail] => varchar
[theme] => varchar
[signature] => varchar
[signature_format] => varchar
[created] => int
[access] => int
[login] => int
[status] => int
[timezone] => varchar
[language] => varchar
[picture] => int
[init] => varchar
[data] => blob
)