You are here

function lingotek_xml_node_body in Lingotek Translation 7.3

Same name and namespace in other branches
  1. 7.2 lingotek.util.inc \lingotek_xml_node_body()
  2. 7.4 lingotek.util.inc \lingotek_xml_node_body()

Return the xml representation of the source content for a node.

Parameters

object $node: A Drupal node.

Return value

string The XML representation of the node in Lingotek format.

3 calls to lingotek_xml_node_body()
LingotekApi::addContentDocument in lib/Drupal/lingotek/LingotekApi.php
Add a document to the Lingotek platform.
LingotekApi::updateContentDocument in lib/Drupal/lingotek/LingotekApi.php
Updates the content of an existing Lingotek document with the current object contents.
LingotekNode::documentLingotekXML in lib/Drupal/lingotek/LingotekNode.php
Gets the contents of this item formatted as XML that can be sent to Lingotek.

File

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

Code

function lingotek_xml_node_body($node) {
  $translatable = array();
  $translate = variable_get('lingotek_translate_fields', array());
  $fields_desired = array();
  if ($translate && ($t_array = $translate[$node->type])) {
    $fields_desired = array_combine($t_array, $t_array);
  }
  foreach ($node as $key => $value) {
    $field = field_info_field($key);
    if (isset($field) && array_key_exists('lingotek_translatable', $field) && $field['lingotek_translatable'] == 1 && isset($fields_desired[$key]) && $fields_desired[$key]) {
      array_push($translatable, $key);
    }
  }
  $content = '';
  foreach ($translatable as $field) {
    $language = $node->language;
    if (!array_key_exists($language, $node->{$field})) {
      $language = LANGUAGE_NONE;
    }
    $text =& $node->{$field};

    // Deal with not being initialized right, such as pre-existing titles.
    if (!array_key_exists($language, $node->{$field}) || !array_key_exists(0, $text[$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(
        'url' => '',
        'title' => '',
      );
    }

    // 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 ($text[$language] as $delta) {
        if (!empty($delta[$target_key]) || isset($array_key) && !empty($delta[$array_key])) {
          $element_name = $field;
          if (!is_array($element_suffix) && !empty($element_suffix)) {
            $element_name .= '__' . $element_suffix;
          }
          $current_field = '<' . $element_name . '>';

          /* if (!array_key_exists('value', $value)) {
             dpm($value);
               //TODO add TAGs to be translatable
                 $terms = $node->$field;
                 foreach ($terms[$language] as $term) {
                 // Do something.
                 }
               continue;
             }*/
          if (is_array($element_suffix)) {
            foreach ($element_suffix as $t_key => $t_val) {
              $current_field .= '<element><![CDATA[' . $delta[$t_key] . ']]></element>' . "\n";
            }
          }
          else {
            $current_field .= '<element><![CDATA[' . $delta[$target_key] . ']]></element>' . "\n";
          }
          $current_field .= '</' . $element_name . '>';
          $content .= $current_field;
        }
      }
    }
  }

  /* deprecated with config translation
    //Menus related to the page:
    // Do we still want this? Config translation translates these items
    $menu = menu_link_get_preferred('node/' . $node->nid);
    $txt = $menu['link_title'];
    if ($txt != "") {
      $content = $content . "<menu_title><![CDATA[$txt]]></menu_title>\n";
    }*/

  //URL Alias related to the page:
  $url_alias_translation = lingotek_variable_get(lingotek_lingonode($node->nid, 'url_alias_translation'), 'lingotek_url_alias_translation', 1);
  if ($url_alias_translation == 1) {
    $conditions = array(
      'source' => 'node/' . $node->nid,
    );
    if ($node->language != LANGUAGE_NONE) {
      $conditions['language'] = $node->language;
    }
    $path = path_load($conditions);
    if ($path !== FALSE) {
      $url = $path['alias'];
      $content = $content . "<url_alias><![CDATA[{$url}]]></url_alias>\n";
    }
  }
  return "<?xml version=\"1.0\" encoding=\"UTF-8\"?><contents>{$content}</contents>";
}