function _translation_entity_update_field_translatability

Stores entity translation settings.

@todo Remove this migration entirely once the Field API is converted to the Entity Field API.

Parameters

array $settings: An associative array of settings keyed by entity type and bundle. At bundle level the following keys are available:

  • translatable: The bundle translatability status, which is a bool.
  • settings: An array of language configuration settings as defined by language_save_default_configuration().
  • fields: An associative array with field names as keys and a boolean as value, indicating field translatability.
1 call to _translation_entity_update_field_translatability()
translation_entity_form_language_content_settings_submit in drupal/core/modules/translation_entity/translation_entity.admin.inc
Form submission handler for translation_entity_admin_settings_form().

File

drupal/core/modules/translation_entity/translation_entity.admin.inc, line 318
The entity translation administration forms.

Code

function _translation_entity_update_field_translatability($settings) {
  $fields = array();
  foreach ($settings as $entity_type => $entity_settings) {
    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.
          $fields[$field_name] = $translatable || !empty($fields[$field_name]);
        }
      }

      // @todo Store non-configurable field settings to be able to alter their
      //   definition afterwards.
    }
  }
  $operations = array();
  foreach ($fields as $field_name => $translatable) {
    $field = field_info_field($field_name);
    if ($field['translatable'] != $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 before
      // we convert data to the entity language. Conversely we need to switch
      // data back to Language::LANGCODE_NOT_SPECIFIED before making a field
      // untranslatable lest we lose information.
      $field_operations = array(
        array(
          'translation_entity_translatable_switch',
          array(
            $translatable,
            $field_name,
          ),
        ),
      );
      if (field_has_data($field)) {
        $field_operations[] = array(
          'translation_entity_translatable_batch',
          array(
            $translatable,
            $field_name,
          ),
        );
        $field_operations = $translatable ? $field_operations : array_reverse($field_operations);
      }
      $operations = array_merge($operations, $field_operations);
    }
  }

  // As last operation store the submitted settings.
  $operations[] = array(
    'translation_entity_save_settings',
    array(
      $settings,
    ),
  );
  $batch = array(
    'title' => t('Updating translatability for the selected fields'),
    'operations' => $operations,
    'finished' => 'translation_entity_translatable_batch_done',
    'file' => drupal_get_path('module', 'translation_entity') . '/translation_entity.admin.inc',
  );
  batch_set($batch);
}