Hi,
Does this module provide metadata that can be used in Entity Metadata Wrappers?
http://drupal.org/node/1021556
I use country of citizenship in a user profile (field_profile_country_citz) and have to programmatically adjust the value.
My code looks like this:
$edits = entity_metadata_wrapper('user', $user);
$edits->field_profile_country_citz->getPropertyInfo(); (returns an array with iso2 in it)
$country = $edits->field_profile_country_citz->value(); = returns Call to a member function value() on a non-object;
This works with taxonomies and nodes. I am not sure if country as an entity does not provide this information or what the right syntax is to retrieve it correctly.
Any suggestion how to get the name of the country or the iso2 code with the wrapper?
Thanks, J.
Comments
Comment #1
Alan D. CreditAttribution: Alan D. commentedDoes #1458938: Add schema columns properties help?
Then something like:
Comment #2
jelo CreditAttribution: jelo commentedHi Alan,
Thanks for your reply. I read the other issue and personally think it would be great to have more schema columns in the wrapper. However, at this stage I would be totally content with the iso2 value, but I am still not able to retrieve it with the metadata wrapper. I tried your suggestion above, but it produces the same error:
"Call to a member function value() on a non-object"
Neither ->value(), ->set($variable), ->getValue() seem to work on this field. These work on all other field though in my module. It must have to do with it being a separate entity and my syntax...
I have loaded the full user object, then use the wrapper:
$edits = entity_metadata_wrapper('user', $user);
How do I retrieve the current value (it is a single value field, hence I assume I do not need to iterate through an array), e.g.:
$edits->field_profile_country_citz->value();
$edits->field_profile_country_citz->iso2->value();
Both return the same error.
And I run into the same issue when I try to set the value. I can extract the current value from the user object, such as $user->field_profile_country_citz['und'][0]['iso2'], but when I try to set it with
$edits->field_profile_country_citz=$new_value;
$edits->field_profile_country_citz->set($new_value);
Both don't work...
Thanks, J.
Comment #3
Alan D. CreditAttribution: Alan D. commentedDid you try that patch? It drops this line.
i.e.
This line would stop the reference... So this and a couple other local modifications that I tried resulted in the code that I posted working.
Comment #4
sreher CreditAttribution: sreher commentedTry this one:
$account_wrapper = entity_metadata_wrapper('user', $account);
$account_wrapper->field_land2->get('iso2')->set($land);
$account_wrapper->save();
Works for me.
Comment #5
Alan D. CreditAttribution: Alan D. commentedNote #1458938: Add schema columns properties was committed, so any code may need to be looked at after upgrading, but these should work as per any other metadata wrapper now.
Comment #7
kovacsaba CreditAttribution: kovacsaba commentedIt was not too easy to figure out the way to set countries field value with entity_metadata_wrapper, but in the end this code snippet resolved the issue. So actually you need to set the whole county entity. For more info check the countries_country_lookup() function.
Comment #8
sano CreditAttribution: sano as a volunteer commented@kovacsaba thanks for sharing. Exactly what I was looking for.