diff --git a/src/Plugin/Field/FieldWidget/InlineParagraphsWidget.php b/src/Plugin/Field/FieldWidget/InlineParagraphsWidget.php index fd7b5ab..507f53a 100644 --- a/src/Plugin/Field/FieldWidget/InlineParagraphsWidget.php +++ b/src/Plugin/Field/FieldWidget/InlineParagraphsWidget.php @@ -239,11 +239,55 @@ class InlineParagraphsWidget extends WidgetBase { $source = $form_state->get(['content_translation', 'source']); $source_langcode = $source ? $source->getId() : $entity_langcode; $paragraphs_entity = $paragraphs_entity->getTranslation($source_langcode); - // Initialise the translation with source language values. - $paragraphs_entity->addTranslation($langcode, $paragraphs_entity->toArray()); - $translation = $paragraphs_entity->getTranslation($langcode); - $manager = \Drupal::service('content_translation.manager'); - $manager->getTranslationMetadata($translation)->setSource($paragraphs_entity->language()->getId()); + + /** + * Need to support two language methodologies: + * + * Localized Paragraphs. + * If the parent field is marked as translatable, assume paragraphs + * to be localized (host entity expects different paragraphs for + * different languages) + * + * Translated Paragraphs. (original method) + * If the parent field is not translatable, assume the paragraph + * entity itself (rather the fields within it) are marked as + * translatable. (host entity expects same paragraphs in different + * languages). + */ + + // Parent field is translatable, localize the paragraph. + if ($items->getFieldDefinition()->isTranslatable()) { + // Get the paragraph item as an array of values. + $paragraph_array = $paragraphs_entity->toArray(); + // Get entity type if has not been previously fetched. + if (!isset($entity_type)) { + $entity_type = $entity_manager->getDefinition($target_type); + $bundle_key = $entity_type->getKey('bundle'); + } + // Create a new paragraph entity for this language. + $new_paragraph = array( + $bundle_key => $paragraphs_entity->bundle(), + 'langcode' => $langcode + ); + + // Loop through all fields in the paragraph and add to new entity. + foreach ($paragraphs_entity->getFieldDefinitions() as $key => $value) { + // Check that the value is a field config and not empty. + if (get_class($value) == 'Drupal\field\Entity\FieldConfig' && !empty($paragraph_array[$key])) { + $new_paragraph[$key] = $paragraph_array[$key]; + } + } + // Set the current entity to the new paragraph entity. + $paragraphs_entity = $entity_manager->getStorage($target_type)->create($new_paragraph); + } + // Parent field is not translatable, translate the paragraph itself. + else { + // Initialise the translation with source language values. + $paragraphs_entity->addTranslation($langcode, $paragraphs_entity->toArray()); + $translation = $paragraphs_entity->getTranslation($langcode); + $manager = \Drupal::service('content_translation.manager'); + $manager->getTranslationMetadata($translation)->setSource($paragraphs_entity->language()->getId()); + } } // Switch the paragraph to the translation. $paragraphs_entity = $paragraphs_entity->getTranslation($langcode); @@ -786,7 +830,8 @@ class InlineParagraphsWidget extends WidgetBase { $elements['add_more'] = array( '#type' => 'container', '#theme_wrappers' => array('paragraphs_dropbutton_wrapper'), - '#access' => !$this->isTranslating, + // If not translating, or if the parent field is translatable. + '#access' => (!$this->isTranslating || $items->getFieldDefinition()->isTranslatable()) ); if (count($access_options)) {