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.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

mortona2k’s picture

Here'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.

heddn’s picture

  1. +++ b/realname.module
    @@ -433,3 +433,23 @@ function realname_username_raw_token_validate(&$element, &$form_state) {
    +    'getter callback' => 'realname_getter_callback',
    

    Me thinks this can just use entity_property_verbatim_get.

  2. +++ b/realname.module
    @@ -433,3 +433,23 @@ function realname_username_raw_token_validate(&$element, &$form_state) {
    \ No newline at end of file
    

    Nit: Missing trailing new line.

mortona2k’s picture

Thanks very much for your feedback, entity_property_verbatim_get works great.

mortona2k’s picture

Status: Active » Needs review
heddn’s picture

+1 on #3
With a visual review, everything looks good. But I haven't actually tested the module with entity api either.

heddn’s picture

Status: Needs review » Reviewed & tested by the community

Had a chance to test #3 against a commons site. It works fine.

rakesh.nimje84@gmail.com’s picture

function 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',
);
}

heddn’s picture

re #7: Is that a +1 on RTBC or some other type of feedback?

hass’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs tests

I have no idea how this works. Can you add tests, please?

edaa’s picture

Maybe 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

luizps’s picture

I 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).

caminadaf’s picture

Status: Needs work » Needs review

Patch #11 worked fine without the mentioned errors.

+1 RTBC

hass’s picture

Status: Needs review » Needs work

Still needs tests.

edaa’s picture

Why not instead use $user_wrapper->label()? label() returns the realname value.

NWOM’s picture

#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!

caminadaf’s picture

@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?

markusd1984’s picture

Stupid 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
)