diff --git a/src/Plugin/Field/FieldWidget/InlineParagraphsWidget.php b/src/Plugin/Field/FieldWidget/InlineParagraphsWidget.php index ce8d108..7127bb1 100644 --- a/src/Plugin/Field/FieldWidget/InlineParagraphsWidget.php +++ b/src/Plugin/Field/FieldWidget/InlineParagraphsWidget.php @@ -14,6 +14,7 @@ use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Field\FieldItemListInterface; use Drupal\Core\Render\Element; use Drupal\paragraphs\ParagraphInterface; +use Drupal\paragraphs\Traits\FieldWidgetTrait; use Symfony\Component\Validator\ConstraintViolationInterface; use Drupal\paragraphs\Plugin\EntityReferenceSelection\ParagraphSelection; @@ -34,6 +35,8 @@ use Drupal\paragraphs\Plugin\EntityReferenceSelection\ParagraphSelection; */ class InlineParagraphsWidget extends WidgetBase { + use FieldWidgetTrait; + /** * Indicates whether the current widget instance is in translation. * @@ -1349,6 +1352,9 @@ class InlineParagraphsWidget extends WidgetBase { return; } $this->isTranslating = FALSE; + + $this->initFormLangcodes($form_state, $host); + if (!$host->isTranslatable()) { return; } diff --git a/src/Plugin/Field/FieldWidget/ParagraphsWidget.php b/src/Plugin/Field/FieldWidget/ParagraphsWidget.php index edb24b9..e2bbca6 100644 --- a/src/Plugin/Field/FieldWidget/ParagraphsWidget.php +++ b/src/Plugin/Field/FieldWidget/ParagraphsWidget.php @@ -18,6 +18,7 @@ use Drupal\Core\Render\Element; use Drupal\Core\TypedData\TranslationStatusInterface; use Drupal\paragraphs\ParagraphInterface; use Drupal\paragraphs\Plugin\EntityReferenceSelection\ParagraphSelection; +use Drupal\paragraphs\Traits\FieldWidgetTrait; use Symfony\Component\Validator\ConstraintViolationInterface; use Symfony\Component\Validator\ConstraintViolationListInterface; @@ -35,6 +36,8 @@ use Symfony\Component\Validator\ConstraintViolationListInterface; */ class ParagraphsWidget extends WidgetBase { + use FieldWidgetTrait; + /** * Action position is in the add paragraphs place. */ @@ -2309,6 +2312,9 @@ class ParagraphsWidget extends WidgetBase { return; } $this->isTranslating = FALSE; + + $this->initFormLangcodes($form_state, $host); + if (!$host->isTranslatable()) { return; } diff --git a/src/Traits/FieldWidgetTrait.php b/src/Traits/FieldWidgetTrait.php new file mode 100644 index 0000000..306e9fd --- /dev/null +++ b/src/Traits/FieldWidgetTrait.php @@ -0,0 +1,43 @@ +has('entity_default_langcode')) { + $form_state->set('entity_default_langcode', $host->getUntranslated()->language()->getId()); + } + // This value might have been explicitly populated to work with a particular + // entity translation. If not we fall back to the most proper language based + // on contextual information. + if (!$form_state->has('langcode')) { + // Imply a 'view' operation to ensure users edit entities in the same + // language they are displayed. This allows to keep contextual editing + // working also for multilingual entities. + $form_state->set('langcode', $entity_repository->getTranslationFromContext($host)->language()->getId()); + } + } + +}