You are here

function lingotek_xml_fields in Lingotek Translation 7.5

Same name and namespace in other branches
  1. 7.7 lingotek.util.inc \lingotek_xml_fields()
  2. 7.4 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 a node.

File

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

Code

function lingotek_xml_fields($entity, $translatable, $language) {
  $content = '';
  foreach ($translatable as $field) {
    $content_container =& $entity->{$field};
    $field_language = is_array($content_container) && array_key_exists($language, $content_container) ? $language : LANGUAGE_NONE;

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

    // We may split compound Drupal fields into several Lingotek fields.
    $target_keys = array(
      'value' => '',
      // Most text fields
      'summary' => 'summary',
    );
    if (module_exists('link')) {
      $target_keys['link'] = array(
        'title' => '',
        'url' => '',
      );
    }

    // Create fields from all target keys.
    foreach ($target_keys as $target_key => $element_suffix) {
      $array_key = NULL;
      if (is_array($element_suffix)) {
        foreach ($element_suffix as $tarkey => $val) {
          $array_key = $tarkey;
          continue;
        }
      }
      foreach ($content_container[$field_language] as $delta) {
        if (!empty($delta[$target_key]) || isset($array_key) && !empty($delta[$array_key])) {
          $element_name = $field;
          if ($target_key == 'value' && isset($delta['revision_id']) && module_exists('field_collection')) {
            $current_field = '<' . $element_name . '>';
            $enabled_fields = variable_get('lingotek_enabled_fields');
            $sub_entity = lingotek_entity_load_single('field_collection_item', $delta['value']);
            $sub_fields = field_info_instances('field_collection_item', $field);
            $translatable_field_types = lingotek_get_translatable_field_types();
            $translatable_sub_fields = array();
            foreach ($sub_fields as $sub_field => $f) {
              if (in_array($f['widget']['type'], $translatable_field_types) && isset($enabled_fields['field_collection_item'][$field]) && in_array($sub_field, $enabled_fields['field_collection_item'][$field])) {
                $translatable_sub_fields[] = $sub_field;
              }
            }
            $current_field .= lingotek_xml_fields($sub_entity, $translatable_sub_fields, $field_language);
            $current_field .= '</' . $element_name . '>';
            $content .= $current_field;
            continue;
          }
          if (!is_array($element_suffix) && !empty($element_suffix)) {
            $element_name .= '__' . $element_suffix;
          }
          $current_field = '<' . $element_name . '>';
          if (is_array($element_suffix)) {
            foreach ($element_suffix as $t_key => $t_val) {
              if (isset($delta[$t_key])) {
                $delta[$t_key] = lingotek_filter_placeholders($delta[$t_key]);
                $current_field .= '<element><![CDATA[' . $delta[$t_key] . ']]></element>' . "\n";
              }
            }
          }
          else {
            $delta[$target_key] = lingotek_filter_placeholders($delta[$target_key]);
            $current_field .= '<element><![CDATA[' . $delta[$target_key] . ']]></element>' . "\n";
          }
          $current_field .= '</' . $element_name . '>';
          $content .= $current_field;
        }
      }
    }
  }
  return $content;
}