protected function ParagraphsClassicAsymmetricWidget::createDuplicateWithSingleLanguage in Paragraphs asymmetric translation widgets 8
Clones a paragraph recursively.
Also, in case of a translatable paragraph, updates its original language and removes all other translations.
Parameters
\Drupal\paragraphs\ParagraphInterface $paragraph: The paragraph entity to clone.
string $langcode: Language code for all the clone entities created.
Return value
\Drupal\paragraphs\ParagraphInterface New paragraph object with the data from the original paragraph. Not saved. All sub-paragraphs are clones as well.
1 call to ParagraphsClassicAsymmetricWidget::createDuplicateWithSingleLanguage()
- ParagraphsClassicAsymmetricWidget::formElement in src/
Plugin/ Field/ FieldWidget/ ParagraphsClassicAsymmetricWidget.php - Uses a similar approach to populate a new translation.
File
- src/
Plugin/ Field/ FieldWidget/ ParagraphsClassicAsymmetricWidget.php, line 748
Class
- ParagraphsClassicAsymmetricWidget
- Plugin implementation of the 'paragraphs_classic_asymmetric' widget.
Namespace
Drupal\paragraphs_asymmetric_translation_widgets\Plugin\Field\FieldWidgetCode
protected function createDuplicateWithSingleLanguage(ParagraphInterface $paragraph, $langcode) {
$duplicate = $paragraph
->createDuplicate();
// Clone all sub-paragraphs recursively.
foreach ($duplicate
->getFields(FALSE) as $field) {
// @todo: should we support field collections as well?
if ($field
->getFieldDefinition()
->getType() == 'entity_reference_revisions' && $field
->getFieldDefinition()
->getTargetEntityTypeId() == 'paragraph') {
foreach ($field as $item) {
$item->entity = $this
->createDuplicateWithSingleLanguage($item->entity, $langcode);
}
}
}
// Change the original language and remove possible translations.
if ($duplicate
->isTranslatable()) {
$duplicate
->set('langcode', $langcode);
foreach ($duplicate
->getTranslationLanguages(FALSE) as $language) {
try {
$duplicate
->removeTranslation($language
->getId());
} catch (\InvalidArgumentException $e) {
// Should never happen.
}
}
}
return $duplicate;
}