Problem/Motivation
On a D7 site that's being migrated to D8, everything works fine except that a few nodes that have faulty JSON in their body fields because somehow some newlines characters were added to e.g. an embedded description.
The following media_wysiwyg token in paragraph nid [nid] does not have valid JSON:
Steps to reproduce
Have a D7 site with contents that have a body, has Media WYSIWYG enabled, and has one node with a file embedded via WYSIWYG that contains an errant linebreak in its JSON string.
Run Media Migration, it will output the error above.
Proposed resolution
Any suggestions for handling this?
Remaining tasks
?
User interface changes
?
API changes
?
Data model changes
?
Comments
Comment #2
damienmckennaDisclaimer: yes, going back and fixing the source data is probably the best option.
Comment #3
trobey commentedI tracked down the faulty JSON to the node having " instead of the double quote. This was hard to figure out because it is displayed as a double quote so it looks correct. The newlines were more obvious but did not cause a problem. I suppose it might be possible to have the error message be more specific but I am not sure it is worthwhile.
Comment #4
trobey commentedBelow is sample code to correct this problem. If you know the node IDs then you can use $nid = $row->getSourceProperty('nid'); to restrict the fix to just those nodes.
Comment #5
damienmckennaThanks @trobey.
Would it be worth adding this as documentation? What do you think, maintainers?
Comment #6
wim leersIMHO it'd be better to incorporate automatic fixes like #4 for "likely JSON malformations".
Based on #4, it looks like we're dealing with a HTML encoding problem? Which actually makes me wonder if there is a deeper rooted problem here.
It'd be really helpful if both of you could share examples of the input that is crashing, because that may or may not confirm my hypothesis 🤓🤞
Comment #7
wim leersTo clarify:
… because automatically fixing what we can is better than expecting users futzing around with migration definition tweaks!
Comment #8
wim leersLet's assume we move forward here with what I wrote in #6 — which means this becomes a feature request.
OTOH, to then do this, we really should have some concrete examples. Can you provide some, @DamienMcKenna and @trobey? 🙏
Comment #9
huzookaEspecially, you could also add it to the process plugin's test here: https://git.drupalcode.org/project/media_migration/-/blob/2fe44b9b32/tes...
Comment #10
damienmckennaThanks for the pointer, huzooka.
I went back to the source data with the problem and mirrored a new item in providerTransformTest() based off it.
Comment #12
wim leersPerfect, thanks so much @DamienMcKenna! 👍
Comment #13
huzookaBTW does this work in Drupal7 Media?
Comment #14
damienmckennaNo it doesn't display on D7, the page is left with some dirty JSON rendering on the page.
Comment #15
wim leersThat is reassuring on the one hand and disturbing on the other hand 😅 But hey, of course you want your new site to actually work as expected unlike the old one, so …
Comment #16
huzookaLast time I touched this was when I fixed #3098833: Handle media_wysiwyg token JSON with line breaks, but that broken JSON (with linebreaks) could be "fixed" in Drupal 7 after an entity resave performed on Drupal UI.
Comment #17
wim leersI just paired with @huzooka to figure whether we could safely automatically fix this kind of broken JSON. Good news 🤓🥳
See it in action at https://3v4l.org/cD86J.
Comment #18
huzookaComment #19
huzookaAdded #10 and #17, and converted
MediaWysiwygFilterTestto a unit test.Comment #20
wim leersWhy doesn't this need the
anymore?
Ahhh … this pre-existing line:
👍
I don't see why we would not ship this hardening to
media_migrationusers right away — the test coverage proves that it works. So … 🚢!Comment #21
huzookaComment #23
damienmckennaExcellent work, thank you!