function translation_entity_entity_insert

Implements hook_entity_insert().

1 call to translation_entity_entity_insert()
translation_entity_entity_update in drupal/core/modules/translation_entity/translation_entity.module
Implements hook_entity_update().

File

drupal/core/modules/translation_entity/translation_entity.module, line 717
Allows entities to be translated into different languages.

Code

function translation_entity_entity_insert(EntityInterface $entity) {

  // Only do something if translation support for the given entity is enabled.
  if (!$entity
    ->isTranslatable()) {
    return;
  }
  $fields = array(
    'entity_type',
    'entity_id',
    'langcode',
    'source',
    'outdated',
    'uid',
    'status',
    'created',
    'changed',
  );
  $query = db_insert('translation_entity')
    ->fields($fields);
  foreach ($entity
    ->getTranslationLanguages() as $langcode => $language) {
    $translation = isset($entity->translation[$langcode]) ? $entity->translation[$langcode] : array();
    $translation += array(
      'source' => '',
      'uid' => $GLOBALS['user']->uid,
      'outdated' => FALSE,
      'status' => TRUE,
      'created' => REQUEST_TIME,
      'changed' => REQUEST_TIME,
    );
    $translation['entity_type'] = $entity
      ->entityType();
    $translation['entity_id'] = $entity
      ->id();
    $translation['langcode'] = $langcode;

    // Reorder values to match the schema.
    $values = array();
    foreach ($fields as $field_name) {
      $value = is_bool($translation[$field_name]) ? intval($translation[$field_name]) : $translation[$field_name];
      $values[$field_name] = $value;
    }
    $query
      ->values($values);
  }
  $query
    ->execute();
}