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.
Hi guys,
I create a patch to make the translate feature of i18n working.
My patch depend of replicate & replicate_paragraph.
You can see it here:
Index: sites/all/modules/contrib/paragraphs/paragraphs.info
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- sites/all/modules/contrib/paragraphs/paragraphs.info (revision 32de12b3ca2aa39f4fcc154352d66cbeb64e6b5e)
+++ sites/all/modules/contrib/paragraphs/paragraphs.info (revision )
@@ -3,6 +3,8 @@
core = 7.x
dependencies[] = entity
+dependencies[] = replicate
+dependencies[] = replicate_paragraphs
files[] = ParagraphsItemEntity.inc
files[] = ParagraphsItemMetadataController.inc
Index: sites/all/modules/contrib/paragraphs/paragraphs.module
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- sites/all/modules/contrib/paragraphs/paragraphs.module (revision 32de12b3ca2aa39f4fcc154352d66cbeb64e6b5e)
+++ sites/all/modules/contrib/paragraphs/paragraphs.module (revision )
@@ -345,6 +345,12 @@
$entity->setHostEntity($host_entity_type, $host_entity, $langcode, FALSE);
}
+ if ($host_entity->nid == NULL) {
+ $new_entity = replicate_clone_entity('paragraphs_item', $entity);
+ $new_entity->setHostEntity($host_entity_type, $host_entity, $langcode, FALSE);
+ $entity = $new_entity;
+ }
+
// If the host entity is saved as new revision, do the same for the item.
if (!empty($host_entity->revision)) {
$entity->revision = TRUE;
@@ -888,21 +894,5 @@
}
}
}
- }
-}
-
-/**
- * Implements hook_field_prepare_translation().
- *
- * @see field_attach_prepare_translation()
- */
-function paragraphs_field_prepare_translation($entity_type, $entity, $field, $instance, $langcode, &$items, $source_entity, $source_langcode) {
- list($id, , ) = entity_extract_ids($entity_type, $entity);
-
- // field_attach_prepare_translation() copied the entity ids from the source,
- // as we need a new entity for a new translation, we cannot reuse that.
- // @todo clone existing paragraphs to new translation
- if (empty($id)) {
- $items = array();
}
}
Comment | File | Size | Author |
---|---|---|---|
#30 | paragraphs-i18n_duplicate_native_support-2331273-30.patch | 4.12 KB | recrit |
#27 | paragraphs-i18n_duplicate-2331273-27.patch | 3.39 KB | ciss |
Comments
Comment #1
killerpoke CreditAttribution: killerpoke commentedHey!
Your patch works fine for attaching paragraphs only to nodes. However if you attach a paragraph field to any other entity (in my situation a paragraph field on a paragraph field) the field data gets lost on save. This is because of you only checking the nid of the host_entity but there are entities with another key.
To solve this, a wrote a little patch to patch your one.
Comment #2
xiaodoudou CreditAttribution: xiaodoudou commentedIndeed that was an issue.
I will do more test to validate your solution then we could submit it to the repo.
Thanks anyway :)
Comment #3
xiaodoudou CreditAttribution: xiaodoudou commentedComment #5
xiaodoudou CreditAttribution: xiaodoudou commentedFind in attachment another implementation as a sub module.
The new key feature of this implementation that now the nested paragraphs are also duplicated.
Comment #7
xiaodoudou CreditAttribution: xiaodoudou commentedChanged the patch return line format to unix style.
Comment #8
xiaodoudou CreditAttribution: xiaodoudou commentedComment #10
xiaodoudou CreditAttribution: xiaodoudou commentedChange patch path.
Comment #12
xiaodoudou CreditAttribution: xiaodoudou commentedPath patch again... (Note to myself: Never use again phpstorm for creating patch :D)
Comment #13
xiaodoudou CreditAttribution: xiaodoudou commentedComment #14
jeroen.b CreditAttribution: jeroen.b commentedThanks for the patch, looks good!
Committed to dev, please test!
Comment #16
jeroen.b CreditAttribution: jeroen.b commentedComment #17
jeroen.b CreditAttribution: jeroen.b commentedComment #18
eigentor CreditAttribution: eigentor commentedThis seems to work. I installed the last Dev version. Paragraphs attached to nodes get cloned correctly and can be translated. I did not test nested Paragraphs, though.
Comment #20
ciss CreditAttribution: ciss commentedThis patch has a major problem: hook_field_prepare_translation() gets invoked by translation_node_prepare() whenever the form for a new translation is displayed.
This means that a simple reload of the form page is enough to create additional detached paragraphs_item entities that will never be removed from the database.
Comment #21
ciss CreditAttribution: ciss commentedSee attached patch for a fix. I also removed the unnecessary id check.
Comment #22
ciss CreditAttribution: ciss commentedI still have some issues with this line. Should we even set a host entity at this point?
Comment #23
ciss CreditAttribution: ciss commentedThe answer is no.
Comment #26
ciss CreditAttribution: ciss commentedThe test failures are unrelated to the patch: https://qa.drupal.org/pifr/test/668608
Comment #27
ciss CreditAttribution: ciss commentedOK, here we go. This patch should be as clean as it gets. We're now properly using replicate_paragraphs, and paragraphs_i18n only exists to provide a dependency to replicate_paragraphs. Tested for nested paragraphs items.
Expect the patch to fail, as the tests on the 7.x-1.x branch haven't been fixed yet.
Comment #28
ciss CreditAttribution: ciss commentedComment #29
seanBI already posted a comment to #2152931 but it seems to make sense to post it here as well.
In the field_collection module there was a similar problem, they fixed it without having to rely on the replicate module:
http://cgit.drupalcode.org/field_collection/commit/?id=1972527
I don't have time to test this now, but it might be worth checking if the paragraphs module can solve it in the same way.
Comment #30
recrit CreditAttribution: recrit commentedThe attached patch provides native support similar to field_collection's method using hook_field_insert to detect the host change. This eliminates the dependency for the replicate modules.
Changes:
* Function paragraphs_field_prepare_translation() is replaced with paragraphs_field_insert() similar to field_collection.
* Removed paragraphs_i18n_field_prepare_translation().
Future considerations:
* I kept the paragraphs_i18n module in case any installs have it as a dependency; however, it has been reduced to a stub module with no functions.
* Tests: A test similar to FieldCollectionContentTranslationTestCase could be created for paragraph items. The original paragraphs and paragraphs_i18n did not have a test so I did not add it to the patch in order to keep it simple. A separate issue could be created for this addition.
Comment #31
Petr IllekHi,
thanks for this effort, is really much appreciated.
I've tested the latest patch against 7.x-1.0-rc4 (as its the most recent release ATM).
And I've found one problem.
Basically there is synchronization between the "source" and "destination" language when adding the translation.
Step to reproduce:
* Have three languages enabled.
* Create new content in first language.
* Translate to second language (paragraphs are pre-filed with first language text –> great)
* Look at first language –> paragraph content is in second language
* Translate the first language to third language (paragraph is pre-filed with second language)
* Look at the first language –> paragraph content is in third language
* Look at the SECOND language –> paragraph content is in SECOND language (no synchronization –> great)
* Edit first language and rewrite it to first
* All paragraphs are now in correct languages and there is no more synchronization
If you need more information, I will do my best to provide them.
Comment #32
recrit CreditAttribution: recrit commented@Petr Illek
I cannot reproduce that issue which makes sense since the paragraphs_field_insert() creates a new paragraph item (not a revision of the original).
Can you provide a list of modules that you have enabled? i18n_sync, node clone, etc... Trying to eliminate any other module that could alter the field value.
Comment #33
seanBFinally did a multilang project again. Patch in #30 worked perfectly. As stated before I think this is the way to go.
Marking as RTBC.
Comment #34
botelho CreditAttribution: botelho commentedHi, i've replicate_paragraphs because i need it for paragraphs_defaults, and its conflicting with this patch.
it gives me an error cant find "paragraphs_item_load_multiple" function.
When i turn paragraphs_defaults and replicate_paragraphs off everything work just fine.
Thanks
Comment #35
rv0 CreditAttribution: rv0 at Coworks.be commented@botelho
That's a weird error, paragraphs_item_load_multiple is part of paragraphs.module and should always be available.
http://cgit.drupalcode.org/paragraphs/tree/paragraphs.module
Comment #36
bmx269 CreditAttribution: bmx269 commentedThanks for this patch. What is needed to make it work on nested paragraphs, aka paragraphs inside paragraphs? Thanks
Comment #37
drupal_beast CreditAttribution: drupal_beast commentedPatch # 30 worked for me.
Comment #38
ngocketit CreditAttribution: ngocketit commentedI have the same problem with field data synchronization of the source and the translation. This happens only when adding a new translation. The source field is updated with value of the translation.
Comment #39
ngocketit CreditAttribution: ngocketit commentedThis seems to happen only when the paragraph field is nested inside, for example, a field collection or another paragraph field.
Comment #40
ngocketit CreditAttribution: ngocketit commentedSo the issue is exactly the same as what @@Petr Illek has. It happens when a paragraph item is attached, for example, to another paragraph item or a field collection item. I believe in that case, we still need to keep the
field_prepare_translation
hook implementation.Comment #41
ngocketit CreditAttribution: ngocketit commentedSo I managed to get it work, not with the patch #30 but #27. Like I said earlier, #30 works only if paragraph item is not attached to another paragraph item or a field collection. If it's attached to a paragraph item, #27 works well. In case it's attached to a field collection, this patch for field collection won't work, we need something similar to what's is done in replicate_paragraphs module:
Comment #42
danilo_formagio CreditAttribution: danilo_formagio at CI&T commentedI noticed that when adding a translation to a node without saving it, orphan paragraphs are generated and the field's table contains empty references to paragraphs items (in the field_***_value column).
When you click on "add a translation" in a node translate tab without saving it, all the paragraphs are persisted in the database and the field data, but the node is persisted only when clicking on save button.
Steps:
- Click to add a translation for a node that contains paragraphs.
- Don't click on save button.
- Close this tab on the browser or go to another page without saving this translation.
- Check in the database that there are paragraphs in the "paragraphs_item" table are not referenced in the paragraph field's table.
The attached patch fixes the issue above, the translation is working fine, including for nested paragraphs, also there is no synchronization issue between the "source" and "destination" that was commented on #31.
Basically the change was:
- Remove the code
$current_entity->save(TRUE);
from paragraphs_i18n_field_prepare_translation.It was saving the paragraph when translating a node before click on save button, creating orphan paragraphs.
- Change the code
to
It will replicate the paragraph to the new translated node.
Using the 'entity' key, the paragraph items are stored in memory instead of having to save them to the database and referencing them by its id. This solves the issue mentioned above.
The are other changes in the patch but they are related to coding standards.
Thanks @nbouhid for the help.
Comment #43
ciss CreditAttribution: ciss as a volunteer commented@danilo_formagio This issue already has two patches (#27, #30) that fix the behavior you describe. Is your patch an update to either of those? If not, please make sure to read the past comments before posting a completely new patch.
Comment #44
ciss CreditAttribution: ciss as a volunteer commentedDue to a lack of feedback I'm assuming that #42 ignored the previous work done in this issue. Hiding #42 to avoid confusion.
Please review either the patch in #27 or #30 (the latter removes the dependency on replicate).
Current reviews:
There are other reviews that I could not associate with a particular patch. You may want to revisit those as well.
When reviewing or updating a patch please specify which patch you are referring to.
Comment #45
danilo_formagio CreditAttribution: danilo_formagio at CI&T commentedSorry for the late reply.
#42 It is not an update for #27 or #30, it's a different way to solve it, it's a simple change, but it also has dependency with replicate module.
I tested the other patches with nested paragraphs:
#27 - It is working fine for me.
I would suggest just to check if
$entity->{$field['field_name']}
is set before use it, I saw some notices about undefined index "und" when translating a node with the paragraph field empty in another project, but testing with a clean Drupal I could not reproduce this message anymore.#30 - I have the same problem with field data synchronization commented on #31.
@ciss feel free to proceed with patch #27.