You are here

function globallink_generate_xml_document in GlobalLink Connect for Drupal 7.5

Same name and namespace in other branches
  1. 7.7 globallink_node.inc \globallink_generate_xml_document()
  2. 7.6 globallink_node.inc \globallink_generate_xml_document()

Builds XML document with translation data.

Parameters

object $node: The node data.

string $target_arr: Array of locales node is being translated into.

int $tnid: The node id for existing translations.

int $tvid: The node vid for existing translations.

string $name: The xml document name.

bool $for_display: Indicate for display purposes only.

Return value

string The translation XML data.

1 call to globallink_generate_xml_document()
globallink_get_xml in ./globallink_node.inc
Retrieves translation XML data.

File

./globallink_node.inc, line 1676

Code

function globallink_generate_xml_document($node, $target_arr, $tnid = NULL, $tvid = NULL, $for_display = FALSE) {
  $is_hook_enabled = variable_get('globallink_implementation_type', 0);
  try {
    $dom = new DOMDocument('1.0', 'UTF-8');
    $dom->formatOutput = TRUE;
    $root = $dom
      ->createElement('content');
    $nid = $dom
      ->createAttribute('nid');
    if ($tnid != NULL) {
      $nid->value = $tnid;
    }
    else {
      $nid->value = $node->nid;
    }
    $root
      ->appendChild($nid);
    $vid = $dom
      ->createAttribute('vid');
    if ($tvid != NULL) {
      $vid->value = $tvid;
    }
    else {
      $vid->value = $node->vid;
    }
    $root
      ->appendChild($vid);
    $url = $dom
      ->createAttribute('pageUrl');
    if (isset($node->path) && isset($node->path['source'])) {
      $url->value = url($node->path['source'], array(
        'absolute' => TRUE,
      ));
    }
    else {
      $url->value = url('node/' . $node->nid, array(
        'absolute' => TRUE,
      ));
    }
    $root
      ->appendChild($url);
    $dom
      ->appendChild($root);
    if ($for_display) {
      globallink_insert_child_element($dom, $root, 'title', $node->title);
    }
    elseif (globallink_is_field_configured_for_translation('node', $node->type, 'title', $node->type) && $is_hook_enabled == 0) {
      globallink_insert_child_element($dom, $root, 'title', $node->title);
    }
    elseif ($is_hook_enabled == 1) {
      if (globallink_is_field_translatable($node, 'title', $target_arr)) {
        globallink_insert_child_element($dom, $root, 'title', $node->title);
      }
    }
    $path = path_load('node/' . $node->nid);
    if (!empty($path)) {
      globallink_insert_child_element($dom, $root, 'path', $path['alias']);
    }
    $field_arr = field_info_instances('node', $node->type);
    $keys = array_keys($field_arr);
    foreach ($keys as $field) {
      $field_def = field_read_field($field);
      $langcode = field_language('node', $node, $field, NULL);
      $items = field_get_items('node', $node, $field, $langcode);
      if ($items) {
        $parent_fc = '';
        if ($field_def['type'] == 'field_collection') {
          $parent_fc = $field;
          if (isset($items[0]['revision_id'])) {
            $field_collection_item_entity = entity_load('field_collection_item', array(
              $items[0]['value'],
            ), array(
              'revision_id' => $items[0]['revision_id'],
            ));
          }
        }
        globallink_traverse_fields_and_field_collections('node', $node->type, $parent_fc, $node->type, $node->nid, $items, $field, $dom, $root, $node, $target_arr, $is_hook_enabled, $langcode);
      }
    }
    if (module_exists('metatag')) {
      if (isset($node->metatags)) {
        if ($for_display) {
          if (!empty($node->metatags[$node->language])) {
            $metatags = $node->metatags[$node->language];

            // Langage of the node to access the metatags
            foreach ($metatags as $name => $value) {
              if (isset($value['value'])) {
                globallink_insert_child_element($dom, $root, 'metatag', $value['value'], array(
                  'entity_type' => 'node',
                  'name' => $name,
                  'label' => 'Metatag - ' . ucwords($name),
                ));
              }
            }
          }
        }
        elseif (globallink_is_field_configured_for_translation('node', $node->type, 'metatags', $node->type)) {
          if (!empty($node->metatags[$node->language])) {
            $metatags = $node->metatags[$node->language];

            // Langage of the node to access the metatags
            foreach ($metatags as $name => $value) {
              if (isset($value['value'])) {
                globallink_insert_child_element($dom, $root, 'metatag', $value['value'], array(
                  'entity_type' => 'node',
                  'name' => $name,
                  'label' => 'Metatag - ' . ucwords($name),
                ));
              }
            }
          }
        }
      }
    }
  } catch (Exception $e) {
    watchdog('GlobalLink', 'Exception - %function - File[%file], Line[%line], Code[%code], Message[%message]', array(
      '%function' => __FUNCTION__,
      '%file' => $e
        ->getFile(),
      '%line' => $e
        ->getLine(),
      '%code' => $e
        ->getCode(),
      '%message' => $e
        ->getMessage(),
    ), WATCHDOG_ERROR);
    throw $e;
  }
  $root_element = $dom
    ->getElementsByTagName('content')
    ->item(0);
  if (!$root_element
    ->hasChildNodes()) {
    return FALSE;
  }
  return $dom
    ->saveXML();
}