You are here

function lingotek_field_language_data_cleanup_update_entity in Lingotek Translation 7.5

Same name and namespace in other branches
  1. 7.7 lingotek.batch.inc \lingotek_field_language_data_cleanup_update_entity()
  2. 7.6 lingotek.batch.inc \lingotek_field_language_data_cleanup_update_entity()

Ensures correct language-specific field data for the specified item.

Logic: Look at each translatable_field (Any field marked for lingotek translation management) for the given node. If the field has data in the language 'und' area, and is empty in the language area that this node is, copy the data over. So if this node is marked as English, but there is no data in the English language spots, but there IS in the 'und' spots, move the data to the English locations.

Parameters

int $nid: The node ID of the item to be updated.

Return value

bool TRUE if specified node's field data was updated. FALSE if no changes made.

1 call to lingotek_field_language_data_cleanup_update_entity()
lingotek_field_language_data_cleanup_batch_worker in ./lingotek.batch.inc
Batch API processor for field data language updates.

File

./lingotek.batch.inc, line 471
Central location for batch create functions, before control is handed off to individual batch command files.

Code

function lingotek_field_language_data_cleanup_update_entity($entity_type, $entity_id, &$context) {
  $edited = FALSE;
  $entity = lingotek_entity_load_single($entity_type, $entity_id);
  if (!$entity) {
    LingotekLog::error('Attempted to update field data for a non-existent @entity_type: @entity_id', array(
      '@entity_type' => $entity_type,
      '@entity_id' => $entity_id,
    ));
    return $edited;
  }
  $info = entity_get_info($entity_type);
  $label_field = $info['entity keys']['label'];

  // Use 'language' as a fallback in case the entity doesn't give lang field.
  $language_field = isset($info['entity keys']['language']) ? $info['entity keys']['language'] : 'language';
  $context['message'] = t('Preparing translatable content for @entity_type: @entity_title', array(
    '@entity_type' => $entity_type,
    '@entity_title' => $entity->{$label_field},
  ));
  if ($entity->{$language_field} != 'und') {
    $translatable_fields = lingotek_translatable_fields();
    foreach ($translatable_fields as $field_name) {
      if (!empty($entity->{$field_name}['und']) && empty($entity->{$field_name}[$entity->{$language_field}])) {
        $entity->{$field_name}[$entity->{$language_field}] = $entity->{$field_name}['und'];
        $edited = TRUE;
      }
    }
  }
  if ($edited) {
    $entity->lingotek_upload_override = FALSE;
    entity_save($entity_type, $entity);
  }
  return $edited;
}