You are here

protected function FieldCollectionEntityTranslationTestCase::createTranslation in Field collection 7

Creates a translation programmatically using Entity Translation.

Parameters

mixed $node: Node of the basic page to create translation for.

string $langcode: The language code of the translation.

1 call to FieldCollectionEntityTranslationTestCase::createTranslation()
FieldCollectionEntityTranslationTestCase::testEntityTranslation in ./field_collection.test
Ensures the right behaviour in all Entity Translation use cases.

File

./field_collection.test, line 1023
Tests for field_collections.

Class

FieldCollectionEntityTranslationTestCase
Test using field collection with content that gets translated with Entity Translation.

Code

protected function createTranslation($node, $langcode) {
  $source_langcode = $node->language;

  // Get the Entity Translation Handler.
  $handler = entity_translation_get_handler('node', $node, TRUE);

  // Variable to hold the fields values.
  $values = array();

  // Translation settings.
  $translation = array(
    'translate' => 0,
    'status' => 1,
    'language' => $langcode,
    'source' => $source_langcode,
    'uid' => $node->uid,
  );

  // Copy field values.
  foreach (field_info_instances('node', $node->type) as $instance) {
    $field_name = $instance['field_name'];
    $field = field_info_field($field_name);
    $field_value = array();

    // Copy the value of the translated field if it's translatable.
    if ($field['translatable'] && isset($node->{$field_name}[$node->language])) {
      $field_value = $node->{$field_name}[$source_langcode];
      $values[$field_name][$langcode] = $field_value;
      $node->{$field_name}[$langcode] = $field_value;
    }
  }
  $handler
    ->setTranslation($translation, $values);
  $handler
    ->saveTranslations();
  field_attach_update('node', $node);

  // Reload an return the node.
  $node = node_load($node->nid, NULL, TRUE);
  return $node;
}