Form submission handler for translation_entity_translatable_form().
This submit handler maintains consistency between the translatability of an entity and the language under which the field data is stored. When a field is marked as translatable, all the data in $entity->{field_name}[LANGUAGE_NOT_SPECIFIED] is moved to $entity->{field_name}[$entity_language]. When a field is marked as untranslatable the opposite process occurs. Note that marking a field as untranslatable will cause all of its translations to be permanently removed, with the exception of the one corresponding to the entity language.
function translation_entity_translatable_form_submit(array $form, array $form_state) {
// This is the current state that we want to reverse.
$translatable = $form_state['values']['translatable'];
$field_name = $form_state['field']['field_name'];
$field = field_info_field($field_name);
if ($field['translatable'] !== $translatable) {
// Field translatability has changed since form creation, abort.
$t_args = array(
'%field_name',
);
$msg = $translatable ? t('The field %field_name is already translatable. No change was performed.', $t_args) : t('The field %field_name is already untranslatable. No change was performed.', $t_args);
drupal_set_message($msg, 'warning');
return;
}
// If a field is untranslatable, it can have no data except under
// LANGUAGE_NOT_SPECIFIED. Thus we need a field to be translatable before we convert
// data to the entity language. Conversely we need to switch data back to
// LANGUAGE_NOT_SPECIFIED before making a field untranslatable lest we lose
// information.
$operations = array(
array(
'translation_entity_translatable_batch',
array(
!$translatable,
$field_name,
),
),
array(
'translation_entity_translatable_switch',
array(
!$translatable,
$field_name,
),
),
);
$operations = $translatable ? $operations : array_reverse($operations);
$t_args = array(
'%field' => $field_name,
);
$title = !$translatable ? t('Enabling translation for the %field field', $t_args) : t('Disabling translation for the %field field', $t_args);
$batch = array(
'title' => $title,
'operations' => $operations,
'finished' => 'translation_entity_translatable_batch_done',
'file' => drupal_get_path('module', 'translation_entity') . '/translation_entity.admin.inc',
);
batch_set($batch);
}