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.
Latest dev of entity_api and field_collection.
I have a node containing a field collection, with revisions enabled.
function mymodule_node_view($node, &$view_mode, $langcode) {
print 'NODE<br />';
print 'vid: ' . $node->vid . '<br />';
foreach ($node->field_field_collection[LANGUAGE_NONE] as $key => $collection) {
print $collection['revision_id'] . '<br />';
}
print '<br />WRAPPER<br />';
$wrapper = entity_metadata_wrapper('node', $node);
print 'vid: ' . $wrapper->vid->value() . '<br />';
foreach ($wrapper->field_field_collection as $key => $collection) {
print $collection->revision_id->value() . '<br />';
}
die;
}
Output:
When viewing the latest node revision:
NODE
vid: 273
255
256
257
WRAPPER
vid: 273
255
256
257
When viewing an older node revision:
NODE
vid: 271
221
222
223
WRAPPER
vid: 271
255
256
257
entity_metadata_wrapper is loading the latest revisions for the field collection, not the field collection revisions specified by the wrapped node entity.
Comment | File | Size | Author |
---|---|---|---|
#39 | interdiff.txt | 9.8 KB | joelpittet |
#39 | 1788568-39.patch | 9.41 KB | joelpittet |
| |||
#30 | entity-1788568-30-entity_metadata_wrapper_revisions.patch | 8.23 KB | james.williams |
| |||
#30 | interdiff-1788568-22-30.txt | 831 bytes | james.williams |
Comments
Comment #1
DamienMcKennaAm running into this too with an entityreference field when used with #1837650: Allow referencing a specific revision ID.
Comment #2
attiks CreditAttribution: attiks commentedChanging title, since this is for all entities
Comment #3
Jelle_SInitial patch. This might need work though. There is no caching mechanism for
entity_revision_load
. If you consider following code with this patch:$wrapper2
will re-load the node (not fetch it from cache) because entity_revision_load has no caching whatsoever.Anyway, here's the patch:
Comment #4
Jelle_SImproved patch, the updateparent logic wasn't correct in the previous patch
Comment #6
Jelle_SThis one should pass tests.
Comment #7
Jelle_SComment #9
Jelle_SAnother try, this time it *really* should work :-)
Comment #10
Cadila CreditAttribution: Cadila commentedJust appled patch from #9, working fine, I think, it should definetely be in contrib!
Comment #11
joelpittetDoesn't quite apply anymore:
Comment #12
jamsilver CreditAttribution: jamsilver commentedLovely! This patch worked for me when trying to get field collections working with workbench moderation (and as such the currently published version of the node actually references non-current revisions of field collections in certain cases). The patch needed a re-roll though - attached.
Comment #13
DamienMcKenna@jamsilver: Thanks for the reroll.
Comment #14
pfrenssenI tested this patch while working on #1837650: Allow referencing a specific revision ID. It still needs work. If a new revision for an entity is created the cache of EntityMetaDataWrapper is not cleared, causing the old revision to still be returned. This also needs tests to prove that it works.
Comment #15
artis.bajars CreditAttribution: artis.bajars commentedFound an issue while testing the patch at #12 while also working with Organic Groups (7.x-2.7). When attempting to save a node that's configured as group content a fatal error is thrown - ( ! ) Fatal error: Maximum function nesting level of '100' reached, aborting! in /sites/all/modules/contrib/entity/includes/entity.property.inc on line 250
Comment #16
anonPatch works fine, tho we can't relay on the "vid" name. We have to use the name from the
Comment #17
Cadila CreditAttribution: Cadila commentedanon, actually it doesn't matter I think. It's just how EntityMetadataWrapper names its property to store revision id.
Comment #18
Cadila CreditAttribution: Cadila commentedartis.bajars, Drupal usualy needs PHP with xdebug running with this limit increased to 140 or even 200.
For doing this you need to add ini_set('xdebug.max_nesting_level', '200'); in your settings.php file.
P.S: This is not PHP limit, It is xdebug's limit.
Comment #19
zhangyb CreditAttribution: zhangyb commentedNot sure if related but running this code:
* Note: field_products is a field_collection
Gives 2 different revision IDs:
The 2nd revision ID by entity_metadata_wrapper actually links to a totally different entity.
Comment #20
lwalley CreditAttribution: lwalley commented@artis.bajars I am also experiencing the max function nesting level reached with Organic Groups. There appears to be an infinite loop resulting from this patch's addition of the call to
entity_load_single()
withinEntityDrupalWrapper->set()
on line 781 of includes/entity.wrapper.inc.My basic understanding is that when
entity_load()
is called to load a node that is group content, this in turn callsNodeController->attachLoad()
which leads toentity_reference_field_load()
being invoked for Organic Group reference fields. OG's Entity Reference behavior handler's load functionOgBehaviourHandler->load()
usesentity_metadata_wrapper()
(see og/plugins/entityreference/behavior/OgBehaviorHandler.class.php:23) which gets us toEntityDrupalWrapper->set()
and with the addition ofentity_load_single()
we loop back toentity_load()
.Comment #21
lwalley CreditAttribution: lwalley commentedRegarding the infinite loop when using Organic Groups, I've created a new issue in OG queue #2363599: Infinite loop after applying patch to Entity API to fix revision loading.
Comment #22
hefox CreditAttribution: hefox commentedCurrent patch breaks saving organic groups, as og group behaviour calls the entity wrapper functionality early in insert, and the entity_load_single logic fires and the cache gets set way too early for field (e.g. before field values are present), meaning an empty field load until those caches get cleared.
what is the purpose of the parent logic around that entity load single (which is also causing the other logic issues)? Don't already have the entity being passed in, can't use that?
For now added to check to see if the entity was actually being changed by seeing if previous_id was set.
Comment #23
joelpittet@hefox thanks for the new patch, could you provide an interdiff? That change was tricky to see as much of the patch was exactly the same.
Comment #24
DamienMcKennaQueuing hefox's patch for review.
Comment #25
NancyDruHas this fallen into a black hole? A year and the test still hasn't run?
Comment #26
DamienMcKenna@NancyDru: The tests show green for me? It might be worth re-uploading it to have the tests run again.
Comment #27
NancyDruIn #22 they show green, but #24 says "queuing". I'm just getting tired of running into this every couple weeks.
Comment #28
gnucifer CreditAttribution: gnucifer commentedTrying out this patch (together with #1837650: Allow referencing a specific revision ID.) and seems to work so far. Will post and update in case I run into any issues. Would be nice if this could be committed.
Comment #29
james.williams CreditAttribution: james.williams at ComputerMinds commentedI've found a possible issue - new (unsaved) entities without IDs are not well handled, as every item in the list gets entity-loaded or as a revision, whereas those cannot have that done, as there is no entity to load yet, but it could still be passed through for use elsewhere. Patch to follow...
As an example of when this might happen, if there is code using the EMW in a hook_node_presave, and there are referenced entities that have been created on the fly, such as a new field collection item, they can be accessed by directly accessing each one, one at a time, but not as a whole list. So this can be made to work fine:
...but the following will not work, causing a fatal error:
(For field collection, this still needs a property callback anyway - #2393627 is dealing with that - but the point here is that there is no way any property callback could work anyway with this patch's changes if the latter snippet above is used.)
Comment #30
james.williams CreditAttribution: james.williams at ComputerMinds commentedHere's a patch to solve the issue I outlined in my previous comment. This will only try and load numeric IDs (including revision IDs), so any unsaved entities (or other data) is left untouched.
Comment #31
james.williams CreditAttribution: james.williams at ComputerMinds commentedComment #32
ameymudras CreditAttribution: ameymudras as a volunteer commentedHad similar issue with referenced paragraphs, the patch seems to work fine except for the fact that i had to make a small change to the paragraphs_field_property_get function. The function was returning entity id without revision id which would cause the entity_metadata_wrapper property to load the default revision
Comment #33
johnennew CreditAttribution: johnennew at Deeson commentedI had the same problem for field_collection module.
field_collection_field_property_get() function:
Comment #34
johnennew CreditAttribution: johnennew at Deeson commentedI've raised this as issues on both paragraphs and field_collection modules:
#2830714: Incorrect revision loading when using entity_metadata_wrapper
#2830713: Incorrect revision loading when using entity_metadata_wrapper
Comment #35
SocialNicheGuru CreditAttribution: SocialNicheGuru commentedThis caused major issues with CER
EntityMetadataWrapperException: Unable to get the data property chain as the parent data structure is not set. in EntityStructureWrapper->getPropertyValue() (line 458 of modules/all/entity/includes/entity.wrapper.inc).
I get the above error: admin/config/content/cer/add when I try to add a left field.
Comment #36
Elin Yordanov CreditAttribution: Elin Yordanov commentedThe patch works and fixes the issues with revisions of referenced entities.
@SocialNicheGuru if it doesn't work with combination of another contrib module, maybe the contrib module should also be updated/patched to get it work, as it is the case with the Field Collection module. Maybe it is better to open an issue on the mentioned module's page.
Comment #37
fagoPatch makes sense, however I dislike the 'vid' key. That's a node-centric key and does not make sense generally. Let's use 'id' and 'revision_id' as keys.
Also, as the tag suggests - this new functionality needs test coverage.
Comment #38
Chris Charlton+1
Comment #39
joelpittetNot sure when to look for 'vid', so just took the last patch and replaced all
vid
changes torevision_id
.This still needs tests but also needs a double check where I should have left the vid, created this patch to help keep this issue moving.
Comment #40
geoffreyr CreditAttribution: geoffreyr commented+1 for #39. I've been using it to help fix up some issues in RESTWS, RESTWS Entity Reference and Paragraphs, so I'd be excited to see this one make it through.
Comment #41
Chris Matthews CreditAttribution: Chris Matthews commentedThe year old patch in #39 still applied cleanly to the latest 7.x-1.x dev snapshot. As there are several other issues dependent on this issue being fixed/committed, is anyone here able review/test this patch and RTBC?
Comment #42
DamienMcKennaPlease be careful with the tags, it can be easy to accidentally remove them. Happy holidays!