Skip to content
Snippets Groups Projects
Commit bed1bddf authored by git's avatar git Committed by Miro Dietiker
Browse files

Issue #2463575 by yongt9412, Lukas von Blarer, ytsurk, tassilogroeper,...

Issue #2463575 by yongt9412, Lukas von Blarer, ytsurk, tassilogroeper, miro_dietiker, Berdir, bojanz: Wrong "all languages" in multilingual paragraphs
parent 2909a690
No related branches found
Tags 8.x-1.0-rc5
No related merge requests found
......@@ -18,6 +18,7 @@ use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Field\WidgetBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Render\Element;
use Drupal\paragraphs;
use Symfony\Component\Validator\ConstraintViolationInterface;
......@@ -542,6 +543,17 @@ class InlineParagraphsWidget extends WidgetBase {
if ($item_mode == 'edit') {
$display->buildForm($paragraphs_entity, $element['subform'], $form_state);
foreach (Element::children($element['subform']) as $field) {
if ($paragraphs_entity->hasField($field)) {
$translatable = $paragraphs_entity->{$field}->getFieldDefinition()->isTranslatable();
if ($translatable) {
$element['subform'][$field]['widget']['#after_build'][] = [
static::class,
'removeTranslatabilityClue'
];
}
}
}
}
elseif ($item_mode == 'preview') {
$element['subform'] = array();
......@@ -1179,4 +1191,40 @@ class InlineParagraphsWidget extends WidgetBase {
$this->isTranslating = TRUE;
}
}
/**
* After-build callback for removing the translatability clue from the widget.
*
* If the fields on the paragraph type are translatable,
* ContentTranslationHandler::addTranslatabilityClue()adds an
* "(all languages)" suffix to the widget title. That suffix is incorrect and
* is being removed by this method using a #after_build on the field widget.
*
* @param array $element
* @param \Drupal\Core\Form\FormStateInterface $form_state
*
* @return array
*/
public static function removeTranslatabilityClue(array $element, FormStateInterface $form_state) {
// Widgets could have multiple elements with their own titles, so remove the
// suffix if it exists, do not recurse lower than this to avoid going into
// nested paragraphs or similar nested field types.
$suffix = ' <span class="translation-entity-all-languages">(' . t('all languages') . ')</span>';
if (isset($element['#title']) && strpos($element['#title'], $suffix)) {
$element['#title'] = str_replace($suffix, '', $element['#title']);
}
// Loop over all widget deltas.
foreach (Element::children($element) as $delta) {
if (isset($element[$delta]['#title']) && strpos($element[$delta]['#title'], $suffix)) {
$element[$delta]['#title'] = str_replace($suffix, '', $element[$delta]['#title']);
}
// Loop over all form elements within the current delta.
foreach (Element::children($element[$delta]) as $field) {
if (isset($element[$delta][$field]['#title']) && strpos($element[$delta][$field]['#title'], $suffix)) {
$element[$delta][$field]['#title'] = str_replace($suffix, '', $element[$delta][$field]['#title']);
}
}
}
return $element;
}
}
......@@ -30,6 +30,7 @@ class ParagraphsTranslationTest extends WebTestBase {
'paragraphs_demo',
'content_translation',
'block',
'link',
);
/**
......@@ -324,6 +325,57 @@ class ParagraphsTranslationTest extends WebTestBase {
// Check that the english translation of the paragraphs is displayed.
$this->assertFieldByName('field_paragraphs_demo[0][subform][field_text_demo][0][value]', 'english_translation_1');
$this->assertFieldByName('field_paragraphs_demo[1][subform][field_text_demo][0][value]', 'english_translation_2');
// Add a non translatable field to Text Paragraph type.
$edit = [
'new_storage_type' => 'text_long',
'label' => 'untranslatable_field',
'field_name' => 'untranslatable_field',
];
$this->drupalPostForm('admin/structure/paragraphs_type/text/fields/add-field', $edit, t('Save and continue'));
$this->drupalPostForm(NULL, [], t('Save field settings'));
$this->drupalPostForm(NULL, [], t('Save settings'));
// Add a non translatable reference field.
$edit = [
'new_storage_type' => 'field_ui:entity_reference:node',
'label' => 'untranslatable_ref_field',
'field_name' => 'untranslatable_ref_field',
];
$this->drupalPostForm('admin/structure/paragraphs_type/text/fields/add-field', $edit, t('Save and continue'));
$this->drupalPostForm(NULL, [], t('Save field settings'));
$this->drupalPostForm(NULL, ['settings[handler_settings][target_bundles][paragraphed_content_demo]' => TRUE], t('Save settings'));
// Add a non translatable link field.
$edit = [
'new_storage_type' => 'link',
'label' => 'untranslatable_link_field',
'field_name' => 'untranslatable_link_field',
];
$this->drupalPostForm('admin/structure/paragraphs_type/text/fields/add-field', $edit, t('Save and continue'));
$this->drupalPostForm(NULL, [], t('Save field settings'));
$this->drupalPostForm(NULL, [], t('Save settings'));
// Attempt to add a translation.
$this->drupalGet('node/' . $node->id() . '/translations/add/de/fr');
$this->assertText('untranslatable_field (all languages)');
$this->assertText('untranslatable_ref_field (all languages)');
$this->assertText('untranslatable_link_field (all languages)');
$this->assertNoText('Text (all languages)');
// Enable translations for the reference and link field.
$edit = [
'translatable' => TRUE,
];
$this->drupalPostForm('admin/structure/paragraphs_type/text/fields/paragraph.text.field_untranslatable_ref_field', $edit, t('Save settings'));
$this->drupalPostForm('admin/structure/paragraphs_type/text/fields/paragraph.text.field_untranslatable_link_field', $edit, t('Save settings'));
// Attempt to add a translation.
$this->drupalGet('node/' . $node->id() . '/translations/add/de/fr');
$this->assertText('untranslatable_field (all languages)');
$this->assertNoText('untranslatable_link_field (all languages)');
$this->assertNoText('untranslatable_ref_field (all languages)');
$this->assertNoText('Text (all languages)');
}
/**
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment