entity_metadata_wrapper accepts entity id as parameter, so entity_metadata_wrapper('node', 17) lazyloads node with id 17.
This does NOT work if the entity declares a name column, like Countries does. Then a name must be given, the id won't work.
I wasn't aware of that. So was the author of organic groups 2.x.
So we have dozends of lines like
return entity_metadata_wrapper($object->group_type, $object->gid);
which break for entities-with-names.
This is a general problem that always occurs if we if entity type is a variable: We can't rely on ID autoloading.
We can fix this in different ways:
1) don't use lazyloading. this will deny ease of use and merits of lazyloading for a big class uf use cases.
2) always expect id. this is the cleanest, but may break code.
3) if entity declares a name, still use id as fallback.
this will still create ambiguity for fieldless entities that use string ids but that's much less pain.
What do you think about 2) or 3)?
EDIT: i've seen that entity_load_single() says
In case the entity
* type has specified a name key, both the numeric id and the name may be
* passed.
so 3) is the way to go is uppose.
(parent issue: #1937784: Allow uppercase entity names)
| Comment | File | Size | Author |
|---|---|---|---|
| #2 | 0001-Entity-Issue-1937856-Added-Always-allow-entity-id-fo.patch | 1.57 KB | geek-merlin |
Comments
Comment #1
geek-merlinComment #1.0
geek-merlinfix
Comment #1.1
geek-merlinx
Comment #2
geek-merlinIn the light of entity_load_single() i tend to call this a major bug now and fixed it.
Comment #3.0
(not verified) commentedparent
Comment #4
alan d. commentedMarking #2183095: Countries entity implementation may be wrong; fails if countries are Organic Groups as a duplicate as they sound similar, albeit that I have no idea about the internals.
Steps to replicate are:
1) clean install of the latest Drupal
2) dev versions of OG, Entity, and Countries
3) use the OG Field configuration page to add a "group" field to the country entity, then set one or more countries to be groups.
4) go add an entity reference field to a content type, and make it use the OG widget. Choose the country you set as a group and click save.
The resulting error message on save.
EntityMetadataWrapperException: Invalid data value given. Be sure it matches the required data type and format. in EntityDrupalWrapper->set() (line 736 of /var/www/clean/sites/all/modules/entity/includes/entity.wrapper.inc).
The original reporter has stepped through the code a bit, but not sure if that would shed any more light on the issue
Comment #5
alan d. commented2: 0001-Entity-Issue-1937856-Added-Always-allow-entity-id-fo.patch queued for re-testing.
Comment #7
alan d. commentedI haven't the time to setup / check, but a workaround within the countries module could be to define the country name key type in countries_entity_property_info_alter(). No idea of the format, so start with the devel debug:
if set, add this:
Note that the token type fails as the module uses the ISO standard of using Uppercase and the token validation is the Drupal default of validating the machine codes lower case char followed by * lowercase chars, numbers or underscores :/
Please comment in the "duplicate" thread if this works and I'll throw that patch into the module and also mark this as fixed ;)
Comment #8
alan d. commentedNote, it was the iso2 column that needed to be altered:
This relaxes the validation allowing uppercase characters / cid to be saved at least. It was defaulted to token / aka a Drupal machine name default validation.
There is still an outstanding issue in that the ER widget is using the ID (cid) entity key rather than the NAME (iso2) entity key, and once things hit the Entity API system, the validation appears to be set to use the NAME entity key.
PS: This is all based on hearsay, I am assuming Entity Reference and not something custom to OG as the root cause here ;)
Comment #9
jassics commentedI have everything latest updates but still getting this error while writing comment to blog or forum.
EntityMetadataWrapperException: Unable to get the data property format as the parent data structure is not set. in EntityStructureWrapper->getPropertyValue() (line 438 of /home6/cusatalu/public_html/sites/all/modules/entity/includes/entity.wrapper.inc).
Here is the code: