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

Issue #2760341 by yongt9412, miro_dietiker: Let the user be aware that...

Issue #2760341 by yongt9412, miro_dietiker: Let the user be aware that Paragraphs fields are not supported for translation
parent 97f29b3f
No related branches found
No related tags found
No related merge requests found
......@@ -104,6 +104,87 @@ function paragraphs_form_field_storage_config_edit_form_alter(&$form, \Drupal\Co
}
}
/**
* Implements hook_form_FORM_ID_alter().
*
* Indicate unsupported multilingual paragraphs field configuration.
*/
function paragraphs_form_field_config_edit_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
$field = $form_state->getFormObject()->getEntity();
if (!\Drupal::hasService('content_translation.manager')) {
return;
}
$bundle_is_translatable = \Drupal::service('content_translation.manager')
->isEnabled($field->getTargetEntityTypeId(), $field->getTargetBundle());
if (!$bundle_is_translatable
|| !$field->getType() == 'entity_reference_revisions'
|| !$field->getSetting('target_type') == 'paragraph') {
return;
}
// This is a translatable ERR field pointing to a paragraph.
$message_display = 'warning';
$message_text = t('Paragraphs fields do not support translation. See the <a href=":documentation">online documentation</a>.', [
':documentation' => Url::fromUri('https://www.drupal.org/node/2735121')
->toString()
]);
if ($form['translatable']['#default_value'] == TRUE) {
$message_display = 'error';
}
$form['paragraphs_message'] = array(
'#type' => 'container',
'#markup' => $message_text,
'#attributes' => array(
'class' => array('messages messages--' . $message_display),
),
'#weight' => 0,
);
}
/**
* Implements hook_form_FORM_ID_alter().
*
* Indicate unsupported multilingual paragraphs field configuration.
*
* Add a warning that paragraph fields can not be translated.
* Switch to error if a paragraph field is marked as translatable.
*/
function paragraphs_form_language_content_settings_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
$message_display = 'warning';
$message_text = t('(* unsupported) Paragraphs fields do not support translation. See the <a href=":documentation">online documentation</a>.', [
':documentation' => Url::fromUri('https://www.drupal.org/node/2735121')
->toString()]);
$map = \Drupal::service('entity_field.manager')->getFieldMapByFieldType('entity_reference_revisions');
foreach ($map as $entity_type_id => $info) {
$field_storage_definitions = \Drupal::service('entity_field.manager')->getFieldStorageDefinitions($entity_type_id);
foreach ($field_storage_definitions as $name => $data) {
if ($data->getSetting('target_type') && $data->getSetting('target_type') == 'paragraph') {
foreach($data->getBundles() as $bundle) {
$form['settings'][$entity_type_id][$bundle]['fields'][$name]['#label'] .= ' (* unsupported)';
if ($form['settings'][$entity_type_id][$bundle]['fields'][$name]['#default_value']) {
$message_display = 'error';
}
}
}
}
}
$form['settings']['paragraphs_message'] = array(
'#type' => 'container',
'#markup' => $message_text,
'#attributes' => array(
'class' => array('messages messages--' . $message_display),
),
'#weight' => 0,
);
return $form;
}
/**
* Prepares variables for paragraph templates.
*
......
......@@ -60,4 +60,56 @@ class ParagraphsConfigTest extends ParagraphsTestBase {
$this->drupalPostForm(NULL, [], t('Save and keep published (this translation)'));
$this->assertText('paragraphed_test paragraphed_title has been updated.');
}
/**
* Tests content translation form translatability constraints messages.
*/
public function testContentTranslationForm() {
$this->loginAsAdmin([
'administer languages',
'administer content translation',
'create content translations',
'translate any entity',
]);
// Check warning message is displayed.
$this->drupalGet('admin/config/regional/content-language');
$this->assertText('(* unsupported) Paragraphs fields do not support translation.');
$this->addParagraphedContentType('paragraphed_test', 'paragraphs_field');
// Check error message is displayed.
$this->drupalGet('admin/config/regional/content-language');
$this->assertText('(* unsupported) Paragraphs fields do not support translation.');
$this->assertRaw('<div class="messages messages--error');
// Add a second language.
ConfigurableLanguage::create(['id' => 'de'])->save();
// Enable translation for paragraphed content type.
$edit = [
'entity_types[node]' => TRUE,
'settings[node][paragraphed_test][translatable]' => TRUE,
'settings[node][paragraphed_test][fields][paragraphs_field]' => FALSE,
];
$this->drupalPostForm('admin/config/regional/content-language', $edit, t('Save configuration'));
// Check content type field management warning.
$this->drupalGet('admin/structure/types/manage/paragraphed_test/fields/node.paragraphed_test.paragraphs_field');
$this->assertText('Paragraphs fields do not support translation.');
// Make the paragraphs field translatable.
$edit = [
'entity_types[node]' => TRUE,
'settings[node][paragraphed_test][translatable]' => TRUE,
'settings[node][paragraphed_test][fields][paragraphs_field]' => TRUE,
];
$this->drupalPostForm('admin/config/regional/content-language', $edit, t('Save configuration'));
// Check content type field management error.
$this->drupalGet('admin/structure/types/manage/paragraphed_test/fields/node.paragraphed_test.paragraphs_field');
$this->assertText('Paragraphs fields do not support translation.');
$this->assertRaw('<div class="messages messages--error');
}
}
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