You are here

public function LingotekComment::updateLocalContent in Lingotek Translation 7.2

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

Updates the local content with data from a Lingotek Document.

Return value

bool TRUE if the content updates succeeded, FALSE otherwise.

Overrides LingotekTranslatableEntity::updateLocalContent

1 call to LingotekComment::updateLocalContent()
LingotekComment::contentUpdated in lib/Drupal/lingotek/LingotekComment.php
Event handler for updates to the comment's data.

File

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

Class

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

Code

public function updateLocalContent() {
  $success = TRUE;
  $metadata = $this
    ->metadata();
  if (!empty($metadata['document_id'])) {
    $document_id = $metadata['document_id'];
    $api = LingotekApi::instance();
    $document = $api
      ->getDocument($document_id);
    foreach ($document->translationTargets as $target) {
      $document_xml = $api
        ->downloadDocument($metadata['document_id'], $target->language);
      $target_language = Lingotek::convertLingotek2Drupal($target->language);
      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(lingotek_xml_decode($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);
  }
  else {
    watchdog('lingotek', 'Unable to refresh local contents for comment @cid. Could not find Lingotek Document ID.', array(
      '@cid' => $this->comment->cid,
    ), WATCHDOG_ERROR);
    $success = FALSE;
  }
  return $success;
}