You are here

public function LingotekComment::updateLocalContentByTarget in Lingotek Translation 7.3

Same name and namespace in other branches
  1. 7.4 lib/Drupal/lingotek/LingotekComment.php \LingotekComment::updateLocalContentByTarget()

Updates the local content of $target_code with data from a Lingotek Document

Parameters

string $lingotek_locale: The code for the language that needs to be updated.

Return value

bool TRUE if the content updates succeeded, FALSE otherwise.

Overrides LingotekTranslatableEntity::updateLocalContentByTarget

1 call to LingotekComment::updateLocalContentByTarget()
LingotekComment::updateLocalContent in lib/Drupal/lingotek/LingotekComment.php
Updates the local content with data from a Lingotek Document.

File

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

Class

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

Code

public function updateLocalContentByTarget($lingotek_locale) {
  $metadata = $this
    ->metadata();
  $document_id = $metadata['document_id'];
  if (empty($document_id)) {
    LingotekLog::error('Unable to refresh local contents for comment @cid. Could not find Lingotek Document ID.', array(
      '@cid' => $this->comment->cid,
    ));
    return FALSE;
  }
  $api = LingotekApi::instance();
  $document_xml = $api
    ->downloadDocument($document_id, $lingotek_locale);
  $target_language = Lingotek::convertLingotek2Drupal($lingotek_locale);
  foreach ($document_xml as $drupal_field_name => $content) {

    // Figure out which subkey of the field data we're targeting.
    // "value" for standard text fields, or some other key for
    // compound text fields (text with summary, for example).
    $target_key = 'value';
    $subfield_parts = explode('__', $drupal_field_name);
    if (count($subfield_parts) == 2) {
      $drupal_field_name = $subfield_parts[0];
      $target_key = $subfield_parts[1];
    }
    $field = field_info_field($drupal_field_name);
    if (!empty($field['lingotek_translatable'])) {
      $comment_field =& $this->comment->{$drupal_field_name};
      $index = 0;
      foreach ($content as $text) {
        $comment_field[$target_language][$index][$target_key] = decode_entities($text);

        // Copy filter format from source language field.
        if (!empty($comment_field[$this->comment->language][0]['format'])) {
          $comment_field[$target_language][$index]['format'] = $comment_field[$this->comment->language][0]['format'];
        }
        $index++;
      }
    }
  }
  $comment_node = LingotekNode::loadById($this->comment->nid);
  $comment_fields = array_keys(field_info_instances('comment', 'comment_node_' . $comment_node->type));
  foreach ($comment_fields as $field) {

    // Copy any untranslated fields from the default language into this target.
    if (isset($this->comment->{$field}[$this->comment->language]) && !isset($this->comment->{$field}[$target_language])) {
      $this->comment->{$field}[$target_language] = $this->comment->{$field}[$this->comment->language];
    }

    // Ensure that all fields get their LANGUAGE_NONE field data populated with the
    // comment's default language data, to support toggling off of comment translation
    // at some point in the future.
    if (!empty($this->comment->{$field}[$this->comment->language])) {
      $this->comment->{$field}[LANGUAGE_NONE] = $this->comment->{$field}[$this->comment->language];
    }
  }

  // This avoids an infitinite loop when hooks resulting from comment_save() are invoked.
  self::$content_update_in_progress = TRUE;
  comment_save($this->comment);
  self::$content_update_in_progress = FALSE;
  $this->comment = comment_load($this->comment->cid);
  return TRUE;
}