Problem/Motivation
Case you want to import a node with translations via HAL (for example via content_translation) the translation for the title disappears.
The failure happens in FieldItemNormalizer.
The problem is, that when you add a new translation (done via $entity->getTranslation()) defaultValues are applied already.
In that case $field_items_translation->isEmpty() will return FALSE, so we get a new field item.
In that case $field_items_translation now contains out of 2 separate field items, the first one with an empty string, the second one with the actual translation.
ON actual store time the second field item will then be ignored, as the first item is already !isEmpty().
Proposed resolution
Instead of getting a translation, manually create a new translation with the new value. In that case $field_items_translation will have a field item
with the new value already, so on save time everything is fine.
Remaining tasks
User interface changes
API changes
| Comment | File | Size | Author |
|---|---|---|---|
| #7 | 8.0.5.png | 46.55 KB | evilehk |
| #7 | a1a0cc8.png | 41.2 KB | evilehk |
| #1 | normalizer.patch | 2.3 KB | dawehner |
Comments
Comment #1
dawehnerHere is a patch,
Comment #3
R.Muilwijk commentedCould you provide a test which reproduces the problem? I tried to come up with a patch but was not able to reproduce. Also wouldn't the best solution be to change the isEmpty() method so that if it's default initiated value it returns TRUE?
Comment #4
wim leersComment #5
gábor hojtsyDoes this work regardless of field translatability?
Comment #6
wim leersMarked #2534424: Missing titles on multi-language posted nodes as a duplicate of this.
Comment #7
evilehk commentedIn an attempt to reroll the patch, I found that the similar changes to createTranslatedInstance were implemented in commit 5c0eb9b7. I executed a POST request similar to the one in the description of #2534424: Missing titles on multi-language posted nodes with a commit from the date this issue was originally posted on 9/24/14. The request created the content items but the titles for the translated nodes were not saved. Screenshot a1a0cc8.png is attached showing the created nodes.
After updating to 8.0.5 and running the same request, the nodes were created with the expected values. Screenshot 8.0.5 is attached showing the node titles. Note that I do think the issue was resolved prior to commit 5c0eb9b7 but I wasn't able to determine where.
Comment #8
clemens.tolboom@evilehk tnx
XREF for commit 5c0eb9b7 #2090983: ContentEntityInterface::getTranslation() should throw an exception when an invalid language is specified
Comment #9
gábor hojtsyThanks for verifying.