Commit c86767d7 authored by webchick's avatar webchick

Issue #2148795 by plach, Gábor Hojtsy: Configurable field translatability is not properly switched.

parent 39601199
......@@ -333,24 +333,32 @@ function content_translation_form_language_content_settings_submit(array $form,
* value, indicating field translatability.
*/
function _content_translation_update_field_translatability($settings) {
$fields = array();
// Update translatability for configurable fields.
// @todo Remove this once configurable fields rely on entity field info to
// track translatability. See https://drupal.org/node/2018685.
foreach ($settings as $entity_type => $entity_settings) {
$fields = array();
foreach ($entity_settings as $bundle => $bundle_settings) {
// Collapse field settings since here we have per instance settings, but
// translatability has per-field scope. We assume that all the field
// instances have the same value.
if (!empty($bundle_settings['fields'])) {
foreach ($bundle_settings['fields'] as $field_name => $translatable) {
// If a field is enabled for translation for at least one instance we
// need to mark it as translatable.
// If translatability changes for at least one field instance we need
// to switch field translatability.
$field = FieldService::fieldInfo()->getField($entity_type, $field_name);
if ($field && $field->isFieldTranslatable() !== $translatable) {
$field->translatable = $translatable;
$field->save();
$fields[$field_name] = $translatable;
}
}
}
}
// Store updated fields.
foreach ($fields as $field_name => $translatable) {
$field = FieldService::fieldInfo()->getField($entity_type, $field_name);
$field->translatable = $translatable;
$field->save();
}
}
content_translation_save_settings($settings);
......
......@@ -812,6 +812,24 @@ function content_translation_form_field_ui_field_edit_form_alter(array &$form, a
'#default_value' => $form['#field']->isFieldTranslatable(),
'#weight' => 20,
);
$form['#submit'][] = 'content_translation_form_field_ui_field_edit_form_submit';
}
/**
* Form submission handler for 'field_ui_field_edit_form'.
*/
function content_translation_form_field_ui_field_edit_form_submit($form, array &$form_state) {
$instance = $form_state['instance'];
$value = content_translation_get_config($instance->entity_type, $instance->bundle, 'fields');
if (!isset($value)) {
$value = array();
}
$value[$instance->getField()->getFieldName()] = $form_state['values']['field']['translatable'];
// Store the same value for all bundles as translatability is tracked per
// field.
foreach (entity_get_bundles($instance->entity_type) as $bundle => $info) {
content_translation_set_config($instance->entity_type, $bundle, 'fields', $value);
}
}
/**
......
......@@ -8,6 +8,7 @@
namespace Drupal\content_translation\Tests;
use Drupal\Core\Language\Language;
use Drupal\field\Field as FieldService;
use Drupal\simpletest\WebTestBase;
/**
......@@ -39,7 +40,7 @@ function setUp() {
$this->drupalCreateContentType(array('type' => 'page'));
$this->container->get('comment.manager')->addDefaultField('node', 'article', 'comment_article');
$admin_user = $this->drupalCreateUser(array('administer languages', 'administer content translation', 'administer content types', 'administer comment fields'));
$admin_user = $this->drupalCreateUser(array('administer languages', 'administer content translation', 'administer content types', 'administer node fields', 'administer comment fields'));
$this->drupalLogin($admin_user);
}
......@@ -125,6 +126,26 @@ function testSettingsUI() {
);
$this->assertSettings('node', NULL, TRUE, $edit);
foreach (array(TRUE, FALSE) as $translatable) {
// Test that configurable field translatability is correctly switched.
$edit = array('settings[node][article][fields][body]' => $translatable);
$this->assertSettings('node', 'article', TRUE, $edit);
$field = FieldService::fieldInfo()->getField('node', 'body');
$definitions = \Drupal::entityManager()->getFieldDefinitions('node', 'article');
$this->assertEqual($definitions['body']->isFieldTranslatable(), $translatable, 'Field translatability correctly switched.');
$this->assertEqual($field->isFieldTranslatable(), $definitions['body']->isFieldTranslatable(), 'Configurable field translatability correctly switched.');
// Test that also the Field UI form behaves correctly.
$translatable = !$translatable;
$edit = array('field[translatable]' => $translatable);
$this->drupalPostForm('admin/structure/types/manage/article/fields/node.article.body/field', $edit, t('Save field settings'));
field_info_cache_clear();
entity_info_cache_clear();
$field = FieldService::fieldInfo()->getField('node', 'body');
$definitions = \Drupal::entityManager()->getFieldDefinitions('node', 'article');
$this->assertEqual($definitions['body']->isFieldTranslatable(), $translatable, 'Field translatability correctly switched.');
$this->assertEqual($field->isFieldTranslatable(), $definitions['body']->isFieldTranslatable(), 'Configurable field translatability correctly switched.');
}
}
/**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment