diff --git a/paragraphs.module b/paragraphs.module index de2c975..8222028 100644 --- a/paragraphs.module +++ b/paragraphs.module @@ -121,7 +121,7 @@ function paragraphs_form_field_storage_config_edit_form_alter(&$form, \Drupal\Co /** * Implements hook_form_FORM_ID_alter(). * - * Indicate unsupported multilingual paragraphs field configuration. + * Indicates not recommended multilingual paragraphs field configuration. */ function paragraphs_form_field_config_edit_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) { $field = $form_state->getFormObject()->getEntity(); @@ -140,24 +140,18 @@ function paragraphs_form_field_config_edit_form_alter(&$form, \Drupal\Core\Form } // This is a translatable ERR field pointing to a paragraph. - $message_display = 'warning'; - $message_text = t('Paragraphs fields do not support translation. See the online documentation.', [ + $message_text = t('The recommended multilingual configuration is to not enable translation on Paragraph fields. See the online documentation.', [ ':documentation' => Url::fromUri('https://www.drupal.org/node/2735121') ->toString() ]); - if ($form['translatable']['#default_value'] == TRUE) { - $message_display = 'error'; - } - - $form['paragraphs_message'] = array( - '#type' => 'container', - '#markup' => $message_text, - '#attributes' => array( - 'class' => array('messages messages--' . $message_display), - ), + $form['paragraphs_message'] = [ + '#theme' => 'status_messages', '#weight' => 0, - ); + '#message_list' => [ + 'warning' => [$message_text], + ], + ]; } /** @@ -181,10 +175,7 @@ function paragraphs_module_implements_alter(&$implementations, $hook) { /** * Implements hook_form_FORM_ID_alter(). * - * Indicate unsupported multilingual paragraphs field configuration. - * - * Add a warning that paragraph fields can not be translated. - * Switch to error if a paragraph field is marked as translatable. + * Indicates not recommended multilingual paragraphs field configuration. */ function paragraphs_form_language_content_settings_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) { // Without it Paragraphs message are meaningless. @@ -193,15 +184,11 @@ function paragraphs_form_language_content_settings_form_alter(&$form, \Drupal\Co } $content_translation_manager = \Drupal::service('content_translation.manager'); - $message_display = 'warning'; - $message_text = t('(* unsupported) Paragraphs fields do not support translation. See the online documentation.', [ + $message_text = t('(* not recommended) The recommended multilingual configuration is to not enable translation on Paragraph fields. See the online documentation.', [ ':documentation' => Url::fromUri('https://www.drupal.org/node/2735121') ->toString()]); $map = \Drupal::service('entity_field.manager')->getFieldMapByFieldType('entity_reference_revisions'); foreach ($map as $entity_type_id => $info) { - if (!$content_translation_manager->isEnabled($entity_type_id)) { - continue; - } $field_storage_definitions = \Drupal::service('entity_field.manager')->getFieldStorageDefinitions($entity_type_id); /** @var \Drupal\Core\Field\FieldStorageDefinitionInterface $storage_definition */ @@ -219,30 +206,23 @@ function paragraphs_form_language_content_settings_form_alter(&$form, \Drupal\Co $bundles = Element::children($form['settings'][$entity_type_id]); } foreach($bundles as $bundle) { - if (!$content_translation_manager->isEnabled($entity_type_id, $bundle)) { - continue; - } // Update the label and if the paragraph field is translatable, // display an error message instead of just a warning. if (isset($form['settings'][$entity_type_id][$bundle]['fields'][$name]['#label'])) { - $form['settings'][$entity_type_id][$bundle]['fields'][$name]['#label'] = t('@field_label (* unsupported)', ['@field_label' => $form['settings'][$entity_type_id][$bundle]['fields'][$name]['#label']]); - } - if (!empty($form['settings'][$entity_type_id][$bundle]['fields'][$name]['#default_value'])) { - $message_display = 'error'; + $form['settings'][$entity_type_id][$bundle]['fields'][$name]['#label'] = t('@field_label (* not recommended)', ['@field_label' => $form['settings'][$entity_type_id][$bundle]['fields'][$name]['#label']]); } } } } } - $form['settings']['paragraphs_message'] = array( - '#type' => 'container', - '#markup' => $message_text, - '#attributes' => array( - 'class' => array('messages messages--' . $message_display), - ), + $form['settings']['paragraphs_message'] = [ + '#theme' => 'status_messages', '#weight' => 0, - ); + '#message_list' => [ + 'warning' => [$message_text], + ], + ]; } /** diff --git a/src/Plugin/Field/FieldWidget/InlineParagraphsWidget.php b/src/Plugin/Field/FieldWidget/InlineParagraphsWidget.php index 66697ad..89e6fb7 100644 --- a/src/Plugin/Field/FieldWidget/InlineParagraphsWidget.php +++ b/src/Plugin/Field/FieldWidget/InlineParagraphsWidget.php @@ -298,7 +298,7 @@ class InlineParagraphsWidget extends WidgetBase { } } - if ($paragraphs_entity) { + if ($paragraphs_entity instanceof ParagraphInterface) { // Detect if we are translating. $this->initIsTranslating($form_state, $host); $langcode = $form_state->get('langcode'); @@ -317,6 +317,13 @@ class InlineParagraphsWidget extends WidgetBase { } } } + elseif ($items->getFieldDefinition()->isTranslatable()) { + // If the field is translatable, host entity translation should refer to + // different paragraph entities. So we clone the paragraph. + if (!empty($form_state->get('content_translation'))) { + $paragraphs_entity = $this->createDuplicateWithSingleLanguage($paragraphs_entity, $langcode); + } + } else { // Add translation if missing for the target language. if (!$paragraphs_entity->hasTranslation($langcode)) { @@ -400,7 +407,7 @@ class InlineParagraphsWidget extends WidgetBase { $links = array(); // Hide the button when translating. - $button_access = $paragraphs_entity->access('delete') && !$this->isTranslating; + $button_access = $paragraphs_entity->access('delete') && (!$this->isTranslating || $items->getFieldDefinition()->isTranslatable()); if ($item_mode != 'remove') { $links['remove_button'] = [ '#type' => 'submit', @@ -900,7 +907,7 @@ class InlineParagraphsWidget extends WidgetBase { $host = $items->getEntity(); $this->initIsTranslating($form_state, $host); - if (($this->realItemCount < $cardinality || $cardinality == FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED) && !$form_state->isProgrammed() && !$this->isTranslating) { + if (($this->realItemCount < $cardinality || $cardinality == FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED) && !$form_state->isProgrammed() && (!$this->isTranslating || $this->fieldDefinition->isTranslatable())) { $elements['add_more'] = $this->buildAddActions(); } @@ -1349,6 +1356,50 @@ class InlineParagraphsWidget extends WidgetBase { } /** + * Clones a paragraph recursively. + * + * Also, in case of a translatable paragraph, updates its original language + * and removes all other translations. + * + * @param \Drupal\paragraphs\ParagraphInterface $paragraph + * The paragraph entity to clone. + * @param string $langcode + * Language code for all the clone entities created. + * + * @return \Drupal\paragraphs\ParagraphInterface + * New paragraph object with the data from the original paragraph. Not + * saved. All sub-paragraphs are clones as well. + */ + protected function createDuplicateWithSingleLanguage(ParagraphInterface $paragraph, $langcode) { + $duplicate = $paragraph->createDuplicate(); + + // Clone all sub-paragraphs recursively. + foreach ($duplicate->getFields(FALSE) as $field) { + // @todo: should we support field collections as well? + if ($field->getFieldDefinition()->getType() == 'entity_reference_revisions' && $field->getFieldDefinition()->getTargetEntityTypeId() == 'paragraph') { + foreach ($field as $item) { + $item->entity = $this->createDuplicateWithSingleLanguage($item->entity, $langcode); + } + } + } + + // Change the original language and remove possible translations. + if ($duplicate->isTranslatable()) { + $duplicate->set('langcode', $langcode); + foreach ($duplicate->getTranslationLanguages(FALSE) as $language) { + try { + $duplicate->removeTranslation($language->getId()); + } + catch (\InvalidArgumentException $e) { + // Should never happen. + } + } + } + + return $duplicate; + } + + /** * After-build callback for removing the translatability clue from the widget. * * If the fields on the paragraph type are translatable, diff --git a/src/Tests/Classic/ParagraphsAsymmetricTranslationTest.php b/src/Tests/Classic/ParagraphsAsymmetricTranslationTest.php new file mode 100644 index 0000000..93a4ffa --- /dev/null +++ b/src/Tests/Classic/ParagraphsAsymmetricTranslationTest.php @@ -0,0 +1,513 @@ +drupalPlaceBlock('local_tasks_block'); + $this->drupalPlaceBlock('page_title_block'); + + $this->adminUser = $this->drupalCreateUser( + [ + 'administer site configuration', + 'administer nodes', + 'create paragraphed_content_demo content', + 'edit any paragraphed_content_demo content', + 'delete any paragraphed_content_demo content', + 'administer paragraph form display', + 'administer node form display', + 'administer paragraph fields', + 'administer content translation', + 'translate any entity', + 'create content translations', + 'administer languages', + 'administer content types', + ] + ); + + $this->drupalLogin($this->adminUser); + + // Mark the paragraph entities as untranslatable and the paragraph field + // as translatable. + $edit = [ + 'entity_types[paragraph]' => TRUE, + 'settings[node][paragraphed_content_demo][fields][field_paragraphs_demo]' => TRUE, + 'settings[paragraph][nested_paragraph][translatable]' => TRUE, + 'settings[paragraph][nested_paragraph][settings][language][language_alterable]' => FALSE, + 'settings[paragraph][text][translatable]' => TRUE, + 'settings[paragraph][text][settings][language][language_alterable]' => FALSE, + 'settings[paragraph][nested_paragraph][fields][field_paragraphs_demo]' => TRUE, + ]; + $this->drupalPostForm( + 'admin/config/regional/content-language', + $edit, + t('Save configuration') + ); + } + + /** + * Test asymmetric translation. + * + * 1. Translate node and create different paragraphs. + * 2. Update the paragraphs on each translation. + * 3. Add different number of paragraphs on each translation. + * 4. Remove paragraphs from each translation. + * 5. Reorder the paragraphs on each translation. + */ + public function testParagraphsMultilingualFieldTranslation() { + // Edit widget to classic. + $this->drupalGet('/admin/structure/types/manage/paragraphed_content_demo/form-display'); + $this->drupalPostForm(NULL, array('fields[field_paragraphs_demo][type]' => 'entity_reference_paragraphs'), t('Save')); + + // 1. Translate node and create different paragraphs. + // Add an English node. + $this->drupalGet('node/add/paragraphed_content_demo'); + $this->drupalPostForm(NULL, NULL, t('Add Text')); + + $edit = [ + 'title[0][value]' => 'Title in english', + 'field_paragraphs_demo[0][subform][field_text_demo][0][value]' => 'Text in english', + ]; + $this->drupalPostForm(NULL, $edit, t('Save')); + + // Translate the node to French. + $node = $this->drupalGetNodeByTitle('Title in english'); + $this->drupalGet('node/' . $node->id() . '/translations/add/en/fr'); + + $this->drupalPostAjaxForm(NULL, [], 'field_paragraphs_demo_0_edit'); + + $edit = [ + 'title[0][value]' => 'Title in french', + 'field_paragraphs_demo[0][subform][field_text_demo][0][value]' => 'Text in french', + 'revision' => TRUE, + 'revision_log[0][value]' => 'french 1', + ]; + $this->drupalPostForm( + NULL, + $edit, + t('Save (this translation)') + ); + + // Check the english translation. + $this->drupalGet('node/' . $node->id()); + $this->assertText('Title in english'); + $this->assertText('Text in english'); + $this->assertNoText('Title in french'); + $this->assertNoText('Text in french'); + + // Check the french translation. + $this->drupalGet('fr/node/' . $node->id()); + $this->assertText('Title in french'); + $this->assertText('Text in french'); + $this->assertNoText('Title in english'); + $this->assertNoText('Text in english'); + + // Check the db. + $select = \Drupal::database()->select('node__field_paragraphs_demo', 'n'); + $select->innerJoin('paragraphs_item', 'p', 'p.id = n.field_paragraphs_demo_target_id'); + $select->addField('p', 'langcode'); + $select->condition('n.entity_id', $node->id()); + $paragraph_langcodes = $select->execute()->fetchCol(); + + $this->assertEqual( + $paragraph_langcodes, + ['en', 'fr'], + 'Translated paragraphs are separate entities' + ); + + // 2. Update the paragraphs on each translation. + // Try to edit the paragraphs, to see if the correct translation gets + // updated. Start with the english. + $this->drupalGet('node/' . $node->id() . '/edit'); + $this->drupalPostAjaxForm(NULL, [], 'field_paragraphs_demo_0_edit'); + $edit = [ + 'field_paragraphs_demo[0][subform][field_text_demo][0][value]' => 'The updated english text', + ]; + $this->drupalPostForm(NULL, $edit, t('Save (this translation)')); + // Check if only the english node had its paragraph text updated, and that + // there has been no mixing-up of the paragraph entities. + $this->drupalGet('node/' . $node->id()); + $this->assertText('The updated english text'); + $this->assertNoText('Text in english'); + $this->assertNoText('Text in french'); + + $this->drupalGet('fr/node/' . $node->id()); + $this->assertText('Text in french'); + $this->assertNoText('Text in english'); + $this->assertNoText('The updated english text'); + + // Now repeat for the french. + $this->drupalGet('fr/node/' . $node->id() . '/edit'); + $this->drupalPostAjaxForm(NULL, [], 'field_paragraphs_demo_0_edit'); + $edit = [ + 'field_paragraphs_demo[0][subform][field_text_demo][0][value]' => 'The updated french text', + ]; + $this->drupalPostForm(NULL, $edit, t('Save (this translation)')); + // Check if only the french node had its paragraph text updated, and that + // there has been no mixing-up of the paragraph entities. + $this->drupalGet('node/' . $node->id()); + $this->assertText('The updated english text'); + $this->assertNoText('Text in french'); + $this->assertNoText('The updated french text'); + + $this->drupalGet('fr/node/' . $node->id()); + $this->assertText('The updated french text'); + $this->assertNoText('Text in french'); + $this->assertNoText('The updated english text'); + + // 3. Add different number of paragraphs on each translation. + // Add 2 more paragraphs on the english node. + $this->drupalGet('node/' . $node->id() . '/edit'); + $this->drupalPostForm(NULL, NULL, t('Add Text')); + $this->drupalPostForm(NULL, NULL, t('Add Text')); + $edit = [ + 'field_paragraphs_demo[1][subform][field_text_demo][0][value]' => 'Second text in english', + 'field_paragraphs_demo[2][subform][field_text_demo][0][value]' => 'Third text in english', + ]; + $this->drupalPostForm(NULL, $edit, t('Save (this translation)')); + + // Confirm that the english node has the new paragraphs, and the french + // node is intact. + $this->drupalGet('node/' . $node->id()); + $this->assertText('The updated english text'); + $this->assertText('Second text in english'); + $this->assertText('Third text in english'); + $this->assertNoText('The updated french text'); + + $this->drupalGet('fr/node/' . $node->id()); + $this->assertText('The updated french text'); + $this->assertNoText('The updated english text'); + $this->assertNoText('Second text in english'); + $this->assertNoText('Third text in english'); + + // Repeat the same, this time adding 3 new paragraphs on the french node. + $this->drupalGet('fr/node/' . $node->id() . '/edit'); + $this->drupalPostForm(NULL, NULL, t('Add Text')); + $this->drupalPostForm(NULL, NULL, t('Add Text')); + $this->drupalPostForm(NULL, NULL, t('Add Text')); + $edit = [ + 'field_paragraphs_demo[1][subform][field_text_demo][0][value]' => 'Second text in french', + 'field_paragraphs_demo[2][subform][field_text_demo][0][value]' => 'Third text in french', + 'field_paragraphs_demo[3][subform][field_text_demo][0][value]' => 'Fourth text in french', + ]; + $this->drupalPostForm(NULL, $edit, t('Save (this translation)')); + + // Confirm that the french node has the new paragraphs, and the english + // node is intact. + $this->drupalGet('node/' . $node->id()); + $this->assertText('The updated english text'); + $this->assertText('Second text in english'); + $this->assertText('Third text in english'); + $this->assertNoText('The updated french text'); + $this->assertNoText('Second text in french'); + $this->assertNoText('Third text in french'); + $this->assertNoText('Fourth text in french'); + + $this->drupalGet('fr/node/' . $node->id()); + $this->assertText('The updated french text'); + $this->assertText('Second text in french'); + $this->assertText('Third text in french'); + $this->assertText('Fourth text in french'); + $this->assertNoText('The updated english text'); + $this->assertNoText('Second text in english'); + $this->assertNoText('Third text in english'); + + // 4. Remove paragraphs from each translation. + // Delete one of the paragraphs on the english node, and confirm that the + // rest are intact, and also that the french node is intact. + $this->drupalGet('node/' . $node->id() . '/edit'); + + $this->assertNotNull($this->xpath('//*[@name="field_paragraphs_demo_1_remove"]')); + $this->drupalPostAjaxForm(NULL, [], 'field_paragraphs_demo_1_remove'); + $this->assertNotNull($this->xpath('//*[@name="field_paragraphs_demo_1_confirm_remove"]')); + $this->drupalPostAjaxForm(NULL, [], 'field_paragraphs_demo_1_confirm_remove'); + $this->drupalPostForm(NULL, NULL, t('Save (this translation)')); + + $this->drupalGet('node/' . $node->id()); + $this->assertText('The updated english text'); + $this->assertNoText('Second text in english'); + $this->assertText('Third text in english'); + $this->assertNoText('The updated french text'); + $this->assertNoText('Second text in french'); + $this->assertNoText('Third text in french'); + $this->assertNoText('Fourth text in french'); + + $this->drupalGet('fr/node/' . $node->id()); + $this->assertNoText('The updated english text'); + $this->assertNoText('Second text in english'); + $this->assertNoText('Third text in english'); + $this->assertText('The updated french text'); + $this->assertText('Second text in french'); + $this->assertText('Third text in french'); + $this->assertText('Fourth text in french'); + + // Repeat the same for the french node, deleting 2 paragraphs now. + $this->drupalGet('fr/node/' . $node->id() . '/edit'); + + $this->assertNotNull($this->xpath('//*[@name="field_paragraphs_demo_1_remove"]')); + $this->drupalPostAjaxForm(NULL, [], 'field_paragraphs_demo_1_remove'); + $this->assertNotNull($this->xpath('//*[@name="field_paragraphs_demo_1_confirm_remove"]')); + $this->drupalPostAjaxForm(NULL, [], 'field_paragraphs_demo_1_confirm_remove'); + $this->assertNotNull($this->xpath('//*[@name="field_paragraphs_demo_3_remove"]')); + $this->drupalPostAjaxForm(NULL, [], 'field_paragraphs_demo_3_remove'); + $this->assertNotNull($this->xpath('//*[@name="field_paragraphs_demo_3_confirm_remove"]')); + $this->drupalPostAjaxForm(NULL, [], 'field_paragraphs_demo_3_confirm_remove'); + $this->drupalPostForm(NULL, NULL, t('Save (this translation)')); + + $this->drupalGet('fr/node/' . $node->id()); + $this->assertNoText('The updated english text'); + $this->assertNoText('Second text in english'); + $this->assertNoText('Third text in english'); + $this->assertText('The updated french text'); + $this->assertNoText('Second text in french'); + $this->assertText('Third text in french'); + $this->assertNoText('Fourth text in french'); + + $this->drupalGet('node/' . $node->id()); + $this->assertText('The updated english text'); + $this->assertNoText('Second text in english'); + $this->assertText('Third text in english'); + $this->assertNoText('The updated french text'); + $this->assertNoText('Second text in french'); + $this->assertNoText('Third text in french'); + $this->assertNoText('Fourth text in french'); + + // 5. Reorder the paragraphs on each translation. + // Reminder: This is our current content + // + // - English node + // -- The updated english text. + // -- Third text in english. + // + // - French node + // -- The updated french text. + // -- Third text in french. + // First check the english node, that "The updated english text" is indeed + // before the "Third text in english", as it has been set on the previous + // steps. + $this->drupalGet('node/' . $node->id()); + $regex = '/The updated english text.*Third text in english/s'; + $this->assertPattern($regex); + + $this->drupalGet('fr/node/' . $node->id()); + $regex = '/The updated french text.*Third text in french/s'; + $this->assertPattern($regex); + + // Reorder the paragraphs in the english node, and check if it applied + // correctly. Check also that the french node is intact. + $this->drupalGet('node/' . $node->id() . '/edit'); + $edit = [ + 'field_paragraphs_demo[0][_weight]' => 2, + 'field_paragraphs_demo[1][_weight]' => -2, + ]; + $this->drupalPostForm(NULL, $edit, t('Save (this translation)')); + + $this->drupalGet('node/' . $node->id()); + $regex = '/Third text in english.*The updated english text/s'; + $this->assertPattern($regex); + + $this->drupalGet('fr/node/' . $node->id()); + $regex = '/The updated french text.*Third text in french/s'; + $this->assertPattern($regex); + + // And now reorder the french node, and then confirm that the new order + // applied correctly, and that the english node is intact. + $this->drupalGet('fr/node/' . $node->id() . '/edit'); + $edit = [ + 'field_paragraphs_demo[0][_weight]' => 2, + 'field_paragraphs_demo[1][_weight]' => -2, + ]; + $this->drupalPostForm(NULL, $edit, t('Save (this translation)')); + + $this->drupalGet('fr/node/' . $node->id()); + $regex = '/Third text in french.*The updated french text/s'; + $this->assertPattern($regex); + + $this->drupalGet('node/' . $node->id()); + $regex = '/Third text in english.*The updated english text/s'; + $this->assertPattern($regex); + } + + /** + * Test paragraphs with nested multilingual fields. + */ + public function testParagraphsMultilingualFieldTranslationNested() { + // Edit widget to classic. + $this->drupalGet('/admin/structure/types/manage/paragraphed_content_demo/form-display'); + $this->drupalPostForm(NULL, array('fields[field_paragraphs_demo][type]' => 'entity_reference_paragraphs'), t('Save')); + + // 1. Translate node and create different paragraphs. + // Add an English node. + $this->drupalGet('node/add/paragraphed_content_demo'); + $this->drupalPostForm(NULL, NULL, t('Add Nested Paragraph'), array(), array(), ''); + $this->drupalPostAjaxForm(NULL, NULL, 'field_paragraphs_demo_0_subform_field_paragraphs_demo_text_add_more'); + $edit = [ + 'title[0][value]' => 'Title in english', + 'field_paragraphs_demo[0][subform][field_paragraphs_demo][0][subform][field_text_demo][0][value]' => 'Text in english', + ]; + $this->drupalPostForm(NULL, $edit, t('Save')); + + // Translate the node to French. + $node = $this->drupalGetNodeByTitle('Title in english'); + $this->drupalGet('node/' . $node->id() . '/translations/add/en/fr'); + + $this->drupalPostAjaxForm(NULL, [], 'field_paragraphs_demo_0_edit'); + + $edit = [ + 'title[0][value]' => 'Title in french', + 'field_paragraphs_demo[0][subform][field_paragraphs_demo][0][subform][field_text_demo][0][value]' => 'Text in french', + ]; + $this->drupalPostForm( + NULL, + $edit, + t('Save (this translation)') + ); + + // Check the english translation. + $this->drupalGet('node/' . $node->id()); + $this->assertText('Title in english'); + $this->assertText('Text in english'); + $this->assertNoText('Title in french'); + $this->assertNoText('Text in french'); + + // Check the french translation. + $this->drupalGet('fr/node/' . $node->id()); + $this->assertText('Title in french'); + $this->assertText('Text in french'); + $this->assertNoText('Title in english'); + $this->assertNoText('Text in english'); + + // 2. Update the paragraphs. + // Try to edit the paragraphs, to see if the correct translation gets + // updated. Start with the english. + $this->drupalGet('node/' . $node->id() . '/edit'); + $this->drupalPostAjaxForm(NULL, [], 'field_paragraphs_demo_0_edit'); + $edit = [ + 'field_paragraphs_demo[0][subform][field_paragraphs_demo][0][subform][field_text_demo][0][value]' => 'The updated english text', + ]; + $this->drupalPostForm(NULL, $edit, t('Save (this translation)')); + // Check if only the english node had its paragraph text updated, and that + // there has been no mixing-up of the paragraph entities. + $this->drupalGet('node/' . $node->id()); + $this->assertText('The updated english text'); + $this->assertNoText('Text in english'); + $this->assertNoText('Text in french'); + + $this->drupalGet('fr/node/' . $node->id()); + $this->assertText('Text in french'); + $this->assertNoText('Text in english'); + $this->assertNoText('The updated english text'); + + // 3. Add different number of paragraphs on one translation. + // Add one more paragraph on the english node. + $this->drupalGet('node/' . $node->id() . '/edit'); + $this->drupalPostAjaxForm(NULL, [], 'field_paragraphs_demo_0_edit'); + $this->drupalPostAjaxForm(NULL, NULL, 'field_paragraphs_demo_0_subform_field_paragraphs_demo_text_add_more'); + $edit = [ + 'field_paragraphs_demo[0][subform][field_paragraphs_demo][1][subform][field_text_demo][0][value]' => 'New english text', + ]; + $this->drupalPostForm( + NULL, + $edit, + t('Save (this translation)') + ); + + // Confirm that the english node has the new paragraphs, and the french + // node is intact. + $this->drupalGet('node/' . $node->id()); + $this->assertText('The updated english text'); + $this->assertText('New english text'); + $this->assertNoText('Text in french'); + + $this->drupalGet('fr/node/' . $node->id()); + $this->assertText('Text in french'); + $this->assertNoText('The updated english text'); + $this->assertNoText('New english text'); + } + + /** + * Test paragraph multilingual field deletes. + */ + public function testParagraphsMultilingualFieldDeleteTranslation() { + // Edit widget to classic. + $this->drupalGet('/admin/structure/types/manage/paragraphed_content_demo/form-display'); + $this->drupalPostForm(NULL, array('fields[field_paragraphs_demo][type]' => 'entity_reference_paragraphs'), t('Save')); + + // 1. Translate node and create different paragraphs. Delete the translation + // and check if the original is intact. + // Add an English node. + $this->drupalGet('node/add/paragraphed_content_demo'); + $this->drupalPostForm(NULL, NULL, t('Add Nested Paragraph'), array(), array(), ''); + $this->drupalPostAjaxForm(NULL, NULL, 'field_paragraphs_demo_0_subform_field_paragraphs_demo_text_add_more'); + $edit = [ + 'title[0][value]' => 'Title in english', + 'field_paragraphs_demo[0][subform][field_paragraphs_demo][0][subform][field_text_demo][0][value]' => 'Text in english', + ]; + $this->drupalPostForm(NULL, $edit, t('Save')); + + // Translate the node to French. + $node = $this->drupalGetNodeByTitle('Title in english'); + $this->drupalGet('node/' . $node->id() . '/translations/add/en/fr'); + $this->drupalPostAjaxForm(NULL, [], 'field_paragraphs_demo_0_edit'); + + $edit = [ + 'title[0][value]' => 'Title in french', + 'field_paragraphs_demo[0][subform][field_paragraphs_demo][0][subform][field_text_demo][0][value]' => 'Text in french', + ]; + $this->drupalPostForm( + NULL, + $edit, + t('Save (this translation)') + ); + + // Check the english translation. + $this->drupalGet('node/' . $node->id()); + $this->assertText('Title in english'); + $this->assertText('Text in english'); + $this->assertNoText('Title in french'); + $this->assertNoText('Text in french'); + + // Check the french translation. + $this->drupalGet('fr/node/' . $node->id()); + $this->assertText('Title in french'); + $this->assertText('Text in french'); + $this->assertNoText('Title in english'); + $this->assertNoText('Text in english'); + + // Now delete the french translation. + $this->drupalGet('fr/node/' . $node->id() . '/delete'); + $this->drupalPostForm(NULL, NULL, t('Delete French translation')); + + // Check the english translation. + $this->drupalGet('node/' . $node->id()); + $this->assertText('Title in english'); + $this->assertText('Text in english'); + $this->assertNoText('Title in french'); + $this->assertNoText('Text in french'); + } + +} diff --git a/src/Tests/Classic/ParagraphsConfigTest.php b/src/Tests/Classic/ParagraphsConfigTest.php index 6494543..b0e35fe 100644 --- a/src/Tests/Classic/ParagraphsConfigTest.php +++ b/src/Tests/Classic/ParagraphsConfigTest.php @@ -77,14 +77,13 @@ class ParagraphsConfigTest extends ParagraphsTestBase { // Check warning message is displayed. $this->drupalGet('admin/config/regional/content-language'); - $this->assertText('(* unsupported) Paragraphs fields do not support translation.'); + $this->assertText('(* not recommended) The recommended multilingual configuration is to not enable translation on Paragraph fields.'); $this->addParagraphedContentType('paragraphed_test', 'paragraphs_field', 'entity_reference_paragraphs'); // Check error message is not displayed. $this->drupalGet('admin/config/regional/content-language'); - $this->assertText('(* unsupported) Paragraphs fields do not support translation.'); - $this->assertNoRaw('