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

Alan D.’s picture

Does #1458938: Add schema columns properties help?

Then something like:

  $edits = entity_metadata_wrapper('user', $account);
  foreach($edits->field_countries->getIterator() as $delta => $country_wrapper) {
    $label = $country_wrapper->name->value();
    drupal_set_message("$delta -- $label");
  }
jelo’s picture

Hi 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.

Alan D.’s picture

Did you try that patch? It drops this line.

i.e.

  // Do not use the type "country" here because that would reference the entity
  // type. THIS BREAKS THE ENTITY TO ENTITY RELATIONSHIP
  $property['type'] = ($field['cardinality'] != 1) ? 'list<countries>' : 'countries';

  // This was the local change I done which helped... I didn't personally try the
  // patch, but it also results in the above line being removed.
  $property['type'] = ($field['cardinality'] != 1) ? 'list<country>' : 'country';

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.

sreher’s picture

Try this one:

$account_wrapper = entity_metadata_wrapper('user', $account);
$account_wrapper->field_land2->get('iso2')->set($land);
$account_wrapper->save();

Works for me.

Alan D.’s picture

Status: Active » Fixed

Note #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.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

kovacsaba’s picture

Issue summary: View changes

It 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.

$node_entity = entity_metadata_wrapper('node', $node);
$country = countries_country_lookup($country_value, 'iso3');
$node_entity->field_countries->set($country);
sano’s picture

@kovacsaba thanks for sharing. Exactly what I was looking for.