You are here

function entity_translation_translatable_form_submit in Entity Translation 7

Submit handler for the field settings 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_NONE] 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.

File

./entity_translation.admin.inc, line 561
The entity translation user interface.

Code

function entity_translation_translatable_form_submit($form, $form_state) {

  // This is the current state that we want to reverse.
  $translatable = $form_state['values']['translatable'];
  $field_name = $form_state['field']['field_name'];
  $copy_all_languages = !empty($form_state['values']['copy_all_languages']);
  $field = field_info_field($field_name);
  if ($field['translatable'] !== $translatable) {

    // Field translatability has changed since form creation, abort.
    $t_args = array(
      '%field_name' => $field_name,
      '!translatable' => $translatable ? t('untranslatable') : t('translatable'),
    );
    drupal_set_message(t('The field %field_name is already !translatable. No change was performed.', $t_args), 'warning');
    return;
  }

  // If a field is untranslatable, it can have no data except under
  // LANGUAGE_NONE. 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_NONE before making a field untranslatable lest we lose
  // information.
  $operations = array(
    array(
      'entity_translation_translatable_batch',
      array(
        !$translatable,
        $field_name,
        $copy_all_languages,
      ),
    ),
    array(
      'entity_translation_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' => 'entity_translation_translatable_batch_done',
    'file' => drupal_get_path('module', 'entity_translation') . '/entity_translation.admin.inc',
  );
  batch_set($batch);
}