Skip to content

Instantly share code, notes, and snippets.

@HelenaEksler
Last active April 14, 2016 09:30
Show Gist options
  • Save HelenaEksler/bf8c207c4abe5c11a6d4 to your computer and use it in GitHub Desktop.
Save HelenaEksler/bf8c207c4abe5c11a6d4 to your computer and use it in GitHub Desktop.
Function to add translation object to the migrated entity.
<?php
public function addLanguagesToEntityTranslations($entity_type, $entity) {
// Get the basic information about the entity.
$entity_info = entity_get_info($entity_type);
// Get the existing site languages.
$languages = array_keys(language_list());
// Add to the entity translation object. Set the original language to
// the entity's default language.
$entity->translations = (object) array(
'original' => $entity->language,
);
$data = array();
foreach($languages as $language) {
// For each language set the data array.
$data[$language] = array(
'entity_type' => $entity_type,
'entity_id' => $entity->{$entity_info['entity keys']['id']},
'language' => $language,
'source' => $language == $entity->language ? '' : $entity->language,
'uid' => '0',
'status' => '1',
'translate' => '0',
);
}
// Add the data array to the entity translation object.
$entity->translations->data = $data;
// Update the entity.
entity_save($entity_type, $entity);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment