You are here

public function FieldCollectionItemEntity::copyTranslations in Field collection 7

Copies text to all languages the collection item has a translation for.

Parameters

$source_language: Language code to copy the text from.

1 call to FieldCollectionItemEntity::copyTranslations()
FieldCollectionItemEntity::save in ./field_collection.entity.inc
Save the field collection item.

File

./field_collection.entity.inc, line 522

Class

FieldCollectionItemEntity
Class for field_collection_item entities.

Code

public function copyTranslations($source_language = NULL) {

  // Get a handler for Entity Translation if there is one.
  $host_et_handler = NULL;
  if (module_exists('entity_translation')) {
    $host_et_handler = entity_translation_get_handler($this
      ->hostEntityType(), $this
      ->hostEntity());
  }
  if (is_null($host_et_handler)) {
    return;
  }
  $host_languages = array_keys($host_et_handler
    ->getTranslations()->data);
  if (empty($host_languages)) {
    $host_languages = array(
      entity_language($this
        ->hostEntityType(), $this
        ->hostEntity()),
    );
  }
  $source_language = isset($source_language) ? $source_language : $host_et_handler
    ->getLanguage();
  $target_languages = array_diff($host_languages, array(
    $source_language,
  ));
  $fields_instances = array_keys(field_info_instances('field_collection_item', $this->field_name));
  $fields = field_info_fields();
  foreach ($fields_instances as $translatable_field) {
    if ($fields[$translatable_field]['translatable'] == 1) {
      foreach ($target_languages as $langcode) {

        // Source (translatable_field) is set, therefore continue
        // processing.
        if (isset($this->{$translatable_field}[$source_language]) && !isset($this->{$translatable_field}[$langcode])) {

          // Destination (translatable_field) is not set, therefore safe to
          // copy the translation.
          $this->{$translatable_field}[$langcode] = $this->{$translatable_field}[$source_language];
        }
      }
      if ($source_language == LANGUAGE_NONE && count($this->{$translatable_field}) > 1) {
        $this->{$translatable_field}[$source_language] = NULL;
      }
    }
  }
}