diff --git a/core/modules/content_translation/content_translation.admin.inc b/core/modules/content_translation/content_translation.admin.inc index 9904ba0..5cccfda 100644 --- a/core/modules/content_translation/content_translation.admin.inc +++ b/core/modules/content_translation/content_translation.admin.inc @@ -350,7 +350,7 @@ function _content_translation_update_field_translatability($settings) { array('content_translation_translatable_switch', array($translatable, $entity_type, $field_name)), ); if ($field->hasData()) { - $field_operations[] = array('content_translation_translatable_batch', array($translatable, $field_name)); + $field_operations[] = array('content_translation_translatable_batch', array($translatable, $entity_type, $field_name)); $field_operations = $translatable ? $field_operations : array_reverse($field_operations); } $operations = array_merge($operations, $field_operations); @@ -397,22 +397,15 @@ function content_translation_translatable_switch($translatable, $entity_type, $f * @param bool $translatable * Indicator of whether the field should be made translatable (TRUE) or * untranslatble (FALSE). + * @param string $entity_type + * Field entity type. * @param string $field_name * Field machine name. */ -function content_translation_translatable_batch($translatable, $field_name, &$context) { +function content_translation_translatable_batch($translatable, $entity_type, $field_name, &$context) { // Determine the entity types to act on. - $entity_types = array(); - foreach (field_info_instances() as $entity_type => $info) { - foreach ($info as $bundle => $instances) { - foreach ($instances as $instance_field_name => $instance) { - if ($instance_field_name == $field_name) { - $entity_types[] = $entity_type; - break 2; - } - } - } - } + // @todo Clean-up implementation in https://drupal.org/node/2092573 + $entity_types = array($entity_type); if (empty($context['sandbox'])) { $context['sandbox']['progress'] = 0; @@ -476,31 +469,33 @@ function content_translation_translatable_batch($translatable, $field_name, &$co // and only after we can remove the old ones. Otherwise we might have data // loss, since the removal of the old translations might occur before the // new ones are stored. - if ($translatable && isset($entity->{$field_name}[Language::LANGCODE_NOT_SPECIFIED])) { + if ($translatable && !$entity->getTranslation(Language::LANGCODE_NOT_SPECIFIED)->get($field_name)->isEmpty()) { // If the field is being switched to translatable and has data for // Language::LANGCODE_NOT_SPECIFIED then we need to move the data to the right // language. - $entity->{$field_name}[$langcode] = $entity->{$field_name}[Language::LANGCODE_NOT_SPECIFIED]; + $entity->getTranslation($langcode) + ->set($field_name, $entity->getTranslation(Language::LANGCODE_NOT_SPECIFIED)->get($field_name)->getValue()); // Store the original value. - _content_translation_update_field($entity_type, $entity, $field_name); - $entity->{$field_name}[Language::LANGCODE_NOT_SPECIFIED] = array(); + _content_translation_update_field($entity, $field_name); // Remove the language neutral value. - _content_translation_update_field($entity_type, $entity, $field_name); + $entity->getTranslation(Language::LANGCODE_NOT_SPECIFIED)->set($field_name, array()); + _content_translation_update_field($entity, $field_name); } - elseif (!$translatable && isset($entity->{$field_name}[$langcode])) { + elseif (!$translatable && !$entity->getTranslation($langcode)->get($field_name)->isEmpty()) { // The field has been marked untranslatable and has data in the entity // language: we need to move it to Language::LANGCODE_NOT_SPECIFIED and drop the // other translations. - $entity->{$field_name}[Language::LANGCODE_NOT_SPECIFIED] = $entity->{$field_name}[$langcode]; + $entity->getTranslation(Language::LANGCODE_NOT_SPECIFIED) + ->set($field_name, $entity->getTranslation($langcode)->get($field_name)->getValue()); // Store the original value. - _content_translation_update_field($entity_type, $entity, $field_name); - // Remove translations. - foreach ($entity->{$field_name} as $langcode => $items) { - if ($langcode != Language::LANGCODE_NOT_SPECIFIED) { - $entity->{$field_name}[$langcode] = array(); + _content_translation_update_field($entity, $field_name); + // Remove all translations except Language::LANGCODE_NOT_SPECIFIED. + foreach ($entity->getTranslationLanguages() as $language) { + if ($language->id != Language::LANGCODE_NOT_SPECIFIED) { + $entity->getTranslation($language->id)->set($field_name, array()); } } - _content_translation_update_field($entity_type, $entity, $field_name); + _content_translation_update_field($entity, $field_name); } else { // No need to save unchanged entities. @@ -514,8 +509,13 @@ function content_translation_translatable_batch($translatable, $field_name, &$co /** * Stores the given field translations. + * + * @param \Drupal\Core\Entity\EntityInterface $entity + * The entity to update. + * @param string $field_name + * The field to update. */ -function _content_translation_update_field($entity_type, EntityInterface $entity, $field_name) { +function _content_translation_update_field(EntityInterface $entity, $field_name) { $empty = 0; $translations = $entity->getTranslationLanguages(); diff --git a/core/modules/content_translation/lib/Drupal/content_translation/Form/TranslatableForm.php b/core/modules/content_translation/lib/Drupal/content_translation/Form/TranslatableForm.php index 05d5dbe..b67d430 100644 --- a/core/modules/content_translation/lib/Drupal/content_translation/Form/TranslatableForm.php +++ b/core/modules/content_translation/lib/Drupal/content_translation/Form/TranslatableForm.php @@ -69,7 +69,7 @@ public function getFormID() { * {@inheritdoc} */ public function getQuestion() { - if ($this->field->isTranslatable()) { + if ($this->field['translatable']) { $question = $this->t('Are you sure you want to disable translation for the %name field?', array('%name' => $this->fieldName)); } else { @@ -85,7 +85,7 @@ public function getDescription() { $description = $this->t('By submitting this form these changes will apply to the %name field everywhere it is used.', array('%name' => $this->fieldName) ); - if ($this->field->isTranslatable()) { + if ($this->field['translatable']) { $description .= "
" . $this->t("All the existing translations of this field will be deleted.
This action cannot be undone."); } return $description; @@ -111,13 +111,6 @@ public function buildForm(array $form, array &$form_state, $entity_type = NULL, $this->fieldName = $field_name; $this->field = $this->fieldInfo->getField($entity_type, $field_name); - // Store the 'translatable' status on the client side to prevent outdated - // form submits from toggling translatability. - $form['translatable'] = array( - '#type' => 'value', - '#value' => $this->field->isTranslatable(), - ); - return parent::buildForm($form, $form_state); } @@ -141,18 +134,7 @@ public function buildForm(array $form, array &$form_state, $entity_type = NULL, */ public function submitForm(array &$form, array &$form_state) { // This is the current state that we want to reverse. - $translatable = $form_state['values']['translatable']; - debug($translatable, 'form'); - debug($this->field->isTranslatable()); - if ($this->field->isTranslatable() !== $translatable) { - // Field translatability has changed since form creation, abort. - $t_args = array('%field_name'); - $msg = $translatable ? - $this->t('The field %field_name is already translatable. No change was performed.', $t_args): - $this->t('The field %field_name is already untranslatable. No change was performed.', $t_args); - drupal_set_message($msg, 'warning'); - return; - } + $translatable = !$this->field->translatable; // If a field is untranslatable, it can have no data except under // Language::LANGCODE_NOT_SPECIFIED. Thus we need a field to be translatable @@ -163,13 +145,14 @@ public function submitForm(array &$form, array &$form_state) { array( 'content_translation_translatable_batch', array( !$translatable, + $this->field->entity_type, $this->fieldName, ), ), array( 'content_translation_translatable_switch', array( !$translatable, - $this->field['entity_type'], + $this->field->entity_type, $this->fieldName, ), ), diff --git a/core/modules/content_translation/lib/Drupal/content_translation/Tests/ContentTranslationSettingsTest.php b/core/modules/content_translation/lib/Drupal/content_translation/Tests/ContentTranslationSettingsTest.php index ecab4c4..e0d4095 100644 --- a/core/modules/content_translation/lib/Drupal/content_translation/Tests/ContentTranslationSettingsTest.php +++ b/core/modules/content_translation/lib/Drupal/content_translation/Tests/ContentTranslationSettingsTest.php @@ -113,6 +113,7 @@ function testSettingsUI() { $this->drupalPostForm('admin/structure/types/manage/article', $edit, t('Save content type')); $this->drupalGet('admin/structure/types/manage/article'); $this->assertFieldChecked('edit-language-configuration-content-translation'); + //Make sure node is translatable. } /**