diff --git a/src/Plugin/Field/FieldWidget/InlineParagraphsWidget.php b/src/Plugin/Field/FieldWidget/InlineParagraphsWidget.php index cbf66fc..abff8a6 100644 --- a/src/Plugin/Field/FieldWidget/InlineParagraphsWidget.php +++ b/src/Plugin/Field/FieldWidget/InlineParagraphsWidget.php @@ -22,9 +22,6 @@ use Symfony\Component\Validator\ConstraintViolationInterface; /** * Plugin implementation of the 'entity_reference paragraphs' widget. * - * We hide add / remove buttons when translating to avoid accidental loss of - * data because these actions effect all languages. - * * @FieldWidget( * id = "entity_reference_paragraphs", * label = @Translation("Paragraphs"), @@ -208,17 +205,22 @@ class InlineParagraphsWidget extends WidgetBase { } if ($paragraphs_entity) { - // If target translation is not yet available, populate it with data from the original paragraph. + // If there is no paragraph entity in this language create one and duplicate the fields. $target_langcode = $this->getCurrentLangcode($form_state, $items); - if ($paragraphs_entity->language()->getId() != $target_langcode && !$paragraphs_entity->hasTranslation($target_langcode)) { - $paragraphs_entity->addTranslation($target_langcode, $paragraphs_entity->toArray()); - } - - // Initiate the paragraph with the correct translation. - if ($paragraphs_entity->hasTranslation($this->getCurrentLangcode($form_state, $items))) { - $paragraphs_entity = $paragraphs_entity->getTranslation($this->getCurrentLangcode($form_state, $items)); - } else { - $paragraphs_entity = $paragraphs_entity->addTranslation($this->getCurrentLangcode($form_state, $items)); + if ($paragraphs_entity->language()->getId() != $target_langcode) { + $values = $paragraphs_entity->toArray(); + $entity_type = $entity_manager->getDefinition($target_type); + $bundle_key = $entity_type->getKey('bundle'); + $new_values = array( + $bundle_key => $paragraphs_entity->bundle(), + 'langcode' => $target_langcode, + ); + foreach ($paragraphs_entity->getFieldDefinitions() as $key => $value) { + if (get_class($value) == 'Drupal\field\Entity\FieldConfig') { + $new_values[$key] = $values[$key][0]; + } + } + $paragraphs_entity = $entity_manager->getStorage($target_type)->create($new_values); } $element_parents = $parents; @@ -295,8 +297,6 @@ class InlineParagraphsWidget extends WidgetBase { ); } - // Hide the button when translating. - $button_access = $paragraphs_entity->access('delete') && $paragraphs_entity->language()->getId() == $paragraphs_entity->getUntranslated()->language()->getId(); $links['remove_button'] = array( '#type' => 'submit', '#value' => t('Remove'), @@ -310,7 +310,6 @@ class InlineParagraphsWidget extends WidgetBase { 'wrapper' => $widget_state['ajax_wrapper_id'], 'effect' => 'fade', ), - '#access' => $button_access, '#prefix' => '
  • ', '#suffix' => '
  • ', ); @@ -731,12 +730,9 @@ class InlineParagraphsWidget extends WidgetBase { // Add 'add more' button, if not working with a programmed form. if (($real_item_count < $cardinality || $cardinality == FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED) && !$form_state->isProgrammed()) { - // Hide the button when translating. - $add_more_access = $this->getCurrentLangcode($form_state, $items) == $items->getEntity()->getUntranslated()->language()->getId(); $elements['add_more'] = array( '#type' => 'container', '#theme_wrappers' => array('paragraphs_dropbutton_wrapper'), - '#access' => $add_more_access, ); if (count($access_options)) {