diff --git a/src/Plugin/Field/FieldWidget/InlineParagraphsWidget.php b/src/Plugin/Field/FieldWidget/InlineParagraphsWidget.php index cfdb189..9365de5 100644 --- a/src/Plugin/Field/FieldWidget/InlineParagraphsWidget.php +++ b/src/Plugin/Field/FieldWidget/InlineParagraphsWidget.php @@ -14,6 +14,7 @@ use Drupal\Core\Field\WidgetBase; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Field\FieldItemListInterface; use Drupal\Core\Render\Element; +use Drupal\field\Entity\FieldConfig; use Drupal\paragraphs; use Symfony\Component\Validator\ConstraintViolationInterface; @@ -232,6 +233,48 @@ 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); + } + + /** + * 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)) { @@ -332,7 +375,7 @@ class InlineParagraphsWidget extends WidgetBase { } // Hide the button when translating. - $button_access = $paragraphs_entity->access('delete') && !$this->isTranslating; + $button_access = $paragraphs_entity->access('delete') && (!$this->isTranslating || $items->getFieldDefinition()->isTranslatable()); $links['remove_button'] = array( '#type' => 'submit', '#value' => $this->t('Remove'), @@ -794,7 +837,7 @@ class InlineParagraphsWidget extends WidgetBase { $elements['add_more'] = array( '#type' => 'container', '#theme_wrappers' => array('paragraphs_dropbutton_wrapper'), - '#access' => !$this->isTranslating, + '#access' => (!$this->isTranslating || $items->getFieldDefinition()->isTranslatable()), ); if (count($access_options)) {