You are here

function lingotek_xml_fields in Lingotek Translation 7.7

Same name and namespace in other branches
  1. 7.4 lingotek.util.inc \lingotek_xml_fields()
  2. 7.5 lingotek.util.inc \lingotek_xml_fields()
  3. 7.6 lingotek.util.inc \lingotek_xml_fields()
1 call to lingotek_xml_fields()
lingotek_entity_xml_body in ./lingotek.util.inc
Return the xml representation of the source content for an entity.

File

./lingotek.util.inc, line 379
Utility functions.

Code

function lingotek_xml_fields($entity_type, $entity, $translatable_fields, $language, $is_empty = TRUE) {
  list($id, $vid, $bundle) = lingotek_entity_extract_ids($entity_type, $entity);
  $content_obj = new LingotekXMLElement('<?xml version="1.0" encoding="UTF-8"?' . '><contents />');
  $translatable_field_types = lingotek_get_translatable_field_types();
  $enabled_fields = variable_get('lingotek_enabled_fields');
  $all_field_info = field_info_fields();
  $has_invalid_xml = FALSE;
  $empty_entity = $is_empty;
  foreach ($translatable_fields as $field_name) {
    $field_type = isset($all_field_info[$field_name]['type']) ? $all_field_info[$field_name]['type'] : NULL;
    $field_module = isset($all_field_info[$field_name]['module']) ? $all_field_info[$field_name]['module'] : NULL;
    if ($entity_type == 'menu_link') {
      $field_language = $entity->language;
      $field_content = lingotek_get_menu_link_field_content($field_name, $entity);
      $field_columns = lingotek_get_menu_link_field_columns();
    }
    else {
      $field_columns = $all_field_info[$field_name]['columns'];
      $field_content =& $entity->{$field_name};
      $field_language = is_array($field_content) && array_key_exists($language, $field_content) ? $language : LANGUAGE_NONE;
    }

    // Deal with not being initialized right, such as pre-existing titles.
    if (!isset($field_content[$field_language])) {
      continue;
    }

    // Create fields from all target keys.
    foreach ($field_content[$field_language] as $delta) {

      // Initialize a tag the field for each delta of the field in source language.
      $field_obj = $content_obj
        ->addChild($field_name);
      foreach ($field_columns as $column_name => $column_attributes) {
        if (empty($delta[$column_name]) && $field_type !== 'multifield' && $field_module !== 'multifield') {
          continue;
        }
        if (!lingotek_translatable_field_column($entity_type, $bundle, $field_name, $column_name, $field_type, $field_module)) {
          continue;
        }

        // Handle nested field-collection entities
        // TODO: make a better way of detecting if this is a field-collection column!
        if ($column_name == 'value' && isset($delta['revision_id']) && module_exists('field_collection')) {
          $sub_entity = lingotek_entity_load_single('field_collection_item', $delta['value']);

          // if the field collection is disabled for Lingotek translation, skip it.
          if ($sub_entity->lingotek['profile'] == LingotekSync::PROFILE_DISABLED) {
            continue;
          }
          $sub_fields = field_info_instances('field_collection_item', $field_name);
          $translatable_sub_fields = array();
          foreach ($sub_fields as $sub_field => $f) {
            $field_type_is_translatable = in_array($all_field_info[$f['field_name']]['type'], $translatable_field_types);
            $field_collection_is_enabled_for_translation = isset($enabled_fields['field_collection_item'][$field_name]);
            $sub_field_is_enabled_for_translation = $field_collection_is_enabled_for_translation && in_array($sub_field, $enabled_fields['field_collection_item'][$field_name]);
            $sub_field_is_enabled_field_collection = $field_collection_is_enabled_for_translation && isset($enabled_fields['field_collection_item'][$sub_field]);
            if ($field_type_is_translatable && $field_collection_is_enabled_for_translation && ($sub_field_is_enabled_for_translation || $sub_field_is_enabled_field_collection)) {
              $translatable_sub_fields[] = $sub_field;
            }
          }
          list($subcontent_obj, $empty_entity) = lingotek_xml_fields('field_collection_item', $sub_entity, $translatable_sub_fields, $field_language, $empty_entity);
          $field_obj
            ->addXML($subcontent_obj);
          continue;
        }
        if ($field_type === 'link_field' && $field_module === 'link' && $column_name === 'attributes') {
          $current_field = $delta[$column_name]['title'];
        }
        elseif ($field_type === 'multifield' && $field_module === 'multifield') {
          $current_field = lingotek_get_multifield_field($delta, $column_name, $field_language);
          if ($current_field === NULL) {
            continue;
          }
        }
        else {
          $current_field = $delta[$column_name];
        }
        $delta_column_content = lingotek_filter_placeholders($current_field);
        if (remove_invalid_xml_characters($delta_column_content)) {
          $has_invalid_xml = TRUE;
        }

        // Handle element suffixes (all columns should be suffixed now)
        $column_obj = $field_obj
          ->addChild($column_name);
        $element = $column_obj
          ->addChild('element');
        $element
          ->addCData($delta_column_content);
        if (trim(strip_tags($delta_column_content)) !== '') {
          $empty_entity = FALSE;
        }
      }
    }
  }

  // If the document has invalid characters, flag it
  if ($has_invalid_xml) {
    lingotek_keystore($entity_type, $id, 'invalid_xml', LingotekSync::INVALID_XML_PRESENT);
    lingotek_keystore($entity_type, $id, 'last_sync_error', 'Entity contains invalid XML characters');
    LingotekSync::setUploadStatus($entity_type, $id, LingotekSync::STATUS_ERROR);
    LingotekLog::error(t('Invalid XML characters in entity !entity_type (!id): @xml'), t(array(
      '@xml' => $content_obj
        ->asXML(),
      '!entity_type' => $entity_type,
      '!id' => $id,
    )));
  }
  else {
    lingotek_keystore_delete($entity_type, $id, 'invalid_xml');
    lingotek_keystore($entity_type, $id, 'last_sync_error', '');

    //Flag if entity is empty
    flag_entity_as_empty($empty_entity, $entity_type, $id, $content_obj);
  }
  return [
    $content_obj,
    $empty_entity,
  ];
}