You are here

function lingotek_entity_xml_body in Lingotek Translation 7.7

Same name and namespace in other branches
  1. 7.5 lingotek.util.inc \lingotek_entity_xml_body()
  2. 7.6 lingotek.util.inc \lingotek_entity_xml_body()

Return the xml representation of the source content for an entity.

Parameters

$entity_type: A string containing a Drupal entity type.

object $entity: A Drupal entity.

Return value

string The XML representation of the entity in Lingotek format.

2 calls to lingotek_entity_xml_body()
LingotekEntity::documentLingotekXML in lib/Drupal/lingotek/LingotekEntity.php
Gets the contents of this item formatted as XML that can be sent to Lingotek.
lingotek_entity_changed in ./lingotek.util.inc

File

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

Code

function lingotek_entity_xml_body($entity_type, $entity) {
  $translatable = array();
  $translate = variable_get('lingotek_enabled_fields', array());
  list($id, $vid, $bundle) = lingotek_entity_extract_ids($entity_type, $entity);
  $fields_desired = isset($translate[$entity_type][$bundle]) ? $translate[$entity_type][$bundle] : array();
  if (empty($fields_desired)) {
    $fields_desired = lingotek_get_translatable_fields_by_content_type($entity_type, $bundle);
  }
  foreach ($fields_desired as $value) {
    $field = field_info_field($value);

    // Enable menu links title and description to masquerade as fields
    if (isset($field) || $entity_type == 'menu_link') {
      array_push($translatable, $value);
    }
  }

  // Workaround for the bean module's source-language problem.
  if ($entity_type == 'bean') {
    $entity->language = lingotek_get_bean_source($entity->bid);
  }

  // Workaround for the group module's source-language problem.
  if ($entity_type == 'group') {
    $entity->language = lingotek_get_group_source($entity->gid);
  }
  if ($entity_type == 'paragraphs_item') {
    $entity->language = lingotek_get_paragraphs_item_source($entity->item_id);
  }
  if ($entity_type == 'file') {
    $entity->language = lingotek_get_file_source($entity->fid);
  }
  list($content_obj, $_) = lingotek_xml_fields($entity_type, $entity, $translatable, $entity->language);
  if ($entity_type == 'node') {

    // URL Alias related to the page:
    if (!empty($entity->lingotek['url_alias_translation'])) {
      $conditions = array(
        'source' => 'node/' . $entity->nid,
      );
      if ($entity->language != LANGUAGE_NONE) {
        $conditions['language'] = $entity->language;
      }
      $path = path_load($conditions);
      if ($path !== FALSE) {
        $url = $path['alias'];
        $url_alias_obj = $content_obj
          ->addChild('url_alias');
        $url_alias_obj
          ->addCData($url);
      }
    }

    // Set $node_based if the entity type is a node, inherit otherwise.
    if (in_array('title', $fields_desired) && variable_get('lingotek_translate_original_node_titles', FALSE) && lingotek_uses_node_translation($entity)) {
      $title_obj = $content_obj
        ->addChild('title');
      $title_obj
        ->addCData($entity->title);
    }
  }
  try {
    $hook_params = array(
      'entity_type' => $entity_type,
      'entity' => $entity,
      'xml' => $content_obj,
      'langcode' => $entity->language,
    );

    // Allow other modules to manipulate the uploaded content.
    drupal_alter('lingotek_entity_upload', $hook_params);
  } catch (Exception $e) {
    LingotekLog::error("Failed to parse or modify XML before uploading. Error: @error. Text: !xml.", array(
      '!xml' => $content_obj,
      '@error' => $e
        ->getMessage(),
    ));
  }
  return $content_obj
    ->asXML();
}