You are here

public function LingotekComment::documentLingotekXML in Lingotek Translation 7.4

Same name and namespace in other branches
  1. 7.2 lib/Drupal/lingotek/LingotekComment.php \LingotekComment::documentLingotekXML()
  2. 7.3 lib/Drupal/lingotek/LingotekComment.php \LingotekComment::documentLingotekXML()

Gets the contents of this item formatted as XML that can be sent to Lingotek.

Return value

string The XML document representing the entity's translatable content.

Overrides LingotekTranslatableEntity::documentLingotekXML

File

lib/Drupal/lingotek/LingotekComment.php, line 206
Defines LingotekComment.

Class

LingotekComment
A class wrapper for Lingotek-specific behavior on Comments.

Code

public function documentLingotekXML() {
  $translatable = array();
  foreach ($this->comment as $key => $value) {
    $field = field_info_field($key);
    if (isset($field)) {
      array_push($translatable, $key);
    }
  }
  $content = '';
  foreach ($translatable as $field) {
    $language = $this->comment->language;
    if (!array_key_exists($language, $this->comment->{$field})) {
      $language = LANGUAGE_NONE;
    }
    $text = $this->comment->{$field};

    // Deal with not being initialized right, such as pre-existing titles.
    if (!array_key_exists($language, $this->comment->{$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',
    );

    // Create fields from all target keys.
    foreach ($target_keys as $target_key => $element_suffix) {
      if (!empty($text[$language][0][$target_key])) {
        $element_name = $field;
        if (!empty($element_suffix)) {
          $element_name .= '__' . $element_suffix;
        }
        $current_field = '<' . $element_name . '>';
        foreach ($text[$language] as $key => $value) {

          // TODO: This isn't a very robust check for text fields.
          // Switch to using field metadata looking for known text field types?
          if (!array_key_exists('value', $value)) {
            continue;
          }
          $current_field .= '<element><![CDATA[' . $value[$target_key] . ']]></element>' . "\n";
        }
        $current_field .= '</' . $element_name . '>';
        $content .= $current_field . "\n";
      }
    }
  }
  return "<?xml version=\"1.0\" encoding=\"UTF-8\"?><contents>{$content}</contents>";
}