diff --git a/src/Plugin/Field/FieldWidget/InlineParagraphsWidget.php b/src/Plugin/Field/FieldWidget/InlineParagraphsWidget.php index 1025c35..25efb48 100644 --- a/src/Plugin/Field/FieldWidget/InlineParagraphsWidget.php +++ b/src/Plugin/Field/FieldWidget/InlineParagraphsWidget.php @@ -4,7 +4,7 @@ namespace Drupal\paragraphs\Plugin\Field\FieldWidget; use Drupal\Component\Utility\NestedArray; use Drupal\Component\Utility\Html; -use Drupal\Core\Entity\Entity; +use Drupal\Core\Entity\ContentEntityInterface; use Drupal\Core\Entity\Entity\EntityFormDisplay; use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\RevisionableInterface; @@ -234,47 +234,19 @@ class InlineParagraphsWidget extends WidgetBase { } } - /** - * Localised Paragraphs. - * - * If the parent field is marked as translatable, assume paragraphs - * to be localized (host entity expects different paragraphs for - * different languages) - */ - else 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 => $field) { - // Check that the value is a field config and not empty. - if ($field instanceof 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); + // Localised Paragraphs. + // If the parent field is marked as translatable, assume paragraphs + // to be localized (host entity expects different paragraphs for + // different languages) + elseif ($items->getFieldDefinition()->isTranslatable()) { + $paragraphs_entity = $this->cloneReferencedEntity($paragraphs_entity, $langcode); } - /** - * Translated Paragraphs - * - * 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). - */ + // Translated Paragraphs + // 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). else { // Add translation if missing for the target language. if (!$paragraphs_entity->hasTranslation($langcode)) { @@ -776,7 +748,7 @@ class InlineParagraphsWidget extends WidgetBase { foreach ($bundles as $machine_name => $bundle) { if ($dragdrop_settings || (!count($this->getSelectionHandlerSetting('target_bundles')) - || in_array($machine_name, $this->getSelectionHandlerSetting('target_bundles')))) { + || in_array($machine_name, $this->getSelectionHandlerSetting('target_bundles')))) { $options[$machine_name] = $bundle['label']; if ($access_control_handler->createAccess($machine_name)) { @@ -873,7 +845,7 @@ class InlineParagraphsWidget extends WidgetBase { ); if ($drop_button) { $elements['add_more']['add_more_button_' . $machine_name]['#prefix'] = '