Skip to content
Snippets Groups Projects

Issue #3514391 by solimanharkas: Fix: Prevent InvalidArgumentException when...

1 file
+ 39
10
Compare changes
  • Side-by-side
  • Inline
@@ -2,6 +2,7 @@
@@ -2,6 +2,7 @@
namespace Drupal\layout_builder_at\Plugin\Field\FieldWidget;
namespace Drupal\layout_builder_at\Plugin\Field\FieldWidget;
 
use Drupal\block_content\BlockContentInterface;
use Drupal\Component\Utility\Html;
use Drupal\Component\Utility\Html;
use Drupal\Component\Utility\NestedArray;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\ContentEntityInterface;
@@ -12,7 +13,6 @@ use Drupal\Core\Field\FieldItemListInterface;
@@ -12,7 +13,6 @@ use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\WidgetBase;
use Drupal\Core\Field\WidgetBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\block_content\BlockContentInterface;
use Drupal\layout_builder\Plugin\SectionStorage\OverridesSectionStorage;
use Drupal\layout_builder\Plugin\SectionStorage\OverridesSectionStorage;
use Drupal\layout_builder\SectionComponent;
use Drupal\layout_builder\SectionComponent;
@@ -258,23 +258,52 @@ class LayoutBuilderCopyWidget extends WidgetBase {
@@ -258,23 +258,52 @@ class LayoutBuilderCopyWidget extends WidgetBase {
if ($replicated_block->hasTranslation($entity->language()->getId())) {
if ($replicated_block->hasTranslation($entity->language()->getId())) {
// Extract values of translation to keep.
// Extract values of translation to keep.
$values_to_keep = $replicated_block->getTranslation($language_to_keep)->toArray();
$values_to_keep = $replicated_block->getTranslation($language_to_keep)->toArray();
// The removed translation needs to be saved or we can not
// Only attempt to remove the translation if it's not the default
// change the language to that.
// translation.
$replicated_block->removeTranslation($language_to_keep);
if ($replicated_block->language()->getId() !== $language_to_keep) {
$replicated_block->save();
try {
 
// The removed translation needs to be saved or we can not
 
// change the language to that.
 
$replicated_block->removeTranslation($language_to_keep);
 
$replicated_block->save();
 
}
 
catch (\InvalidArgumentException $e) {
 
// If we can't remove the translation, just continue with the
 
// process. This can happen when trying to remove the default
 
// language translation.
 
\Drupal::logger('layout_builder_at')->notice(
 
'Could not remove translation @lang: @message',
 
[
 
'@lang' => $language_to_keep,
 
'@message' => $e->getMessage(),
 
]
 
);
 
}
 
}
}
}
$replicated_block->set('langcode', $language_to_keep);
$replicated_block->set('langcode', $language_to_keep);
$replicated_block->save();
// Copy translatable field values.
// Copy translatable field values.
foreach ($values_to_keep as $field_name => $field_values) {
foreach ($values_to_keep as $field_name => $field_values) {
if ($replicated_block->getFieldDefinition($field_name)->isTranslatable() && !in_array($field_name, ['default_langcode'])) {
if ($replicated_block->getFieldDefinition($field_name)->isTranslatable()
 
&& !in_array($field_name, ['default_langcode'])) {
$replicated_block->set($field_name, $field_values);
$replicated_block->set($field_name, $field_values);
}
}
}
}
// Remove other translations.
// Remove other translations that are not the language we're keeping
 
// and not the default language.
foreach ($replicated_block->getTranslationLanguages() as $translation_language) {
foreach ($replicated_block->getTranslationLanguages() as $translation_language) {
if ($translation_language->getId() !== $language_to_keep) {
$translation_id = $translation_language->getId();
$replicated_block->removeTranslation($translation_language->getId());
if ($translation_id !== $language_to_keep && $translation_id !== $replicated_block->language()->getId()) {
 
try {
 
$replicated_block->removeTranslation($translation_id);
 
}
 
catch (\InvalidArgumentException $e) {
 
// If we can't remove the translation, just skip it.
 
\Drupal::logger('layout_builder_at')->notice('Could not remove translation @lang: @message', [
 
'@lang' => $translation_id,
 
'@message' => $e->getMessage(),
 
]);
 
}
}
}
}
}
$replicated_block->save();
$replicated_block->save();
Loading