You are here

function globallink_entity_generate_xml_document in GlobalLink Connect for Drupal 7.6

Same name and namespace in other branches
  1. 7.7 globallink_entity/globallink_entity.inc \globallink_entity_generate_xml_document()
  2. 7.5 globallink_entity/globallink_entity.inc \globallink_entity_generate_xml_document()

Generates XML document for entity.

Parameters

object $node: The entity node.

array $target_arr: The target array

string $tnid: The translated entity node ID. Defaults to NULL.

string $tvid: The translated entity VID. Defaults to NULL.

bool $for_display: Whether or not the XML will be displayed. Defaults to FALSE.

string $source_lang: The source language of the entity. Defaults to empty.

Return value

XML representation of the entity. FALSE on failure.

1 call to globallink_entity_generate_xml_document()
globallink_entity_get_xml in globallink_entity/globallink_entity.inc
Gets XML data from specific entity.

File

globallink_entity/globallink_entity.inc, line 1414

Code

function globallink_entity_generate_xml_document($node, $target_arr, $tnid = NULL, $tvid = NULL, $for_display = FALSE, $source_lang = '') {
  $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');
    $path = path_load(array(
      'source' => 'node/' . $node->nid,
      'language' => $source_lang,
    ));
    if (isset($path['alias'])) {
      $url->value = url($path['alias'], array(
        'absolute' => TRUE,
      ));
    }
    else {
      $url->value = url('node/' . $node->nid, array(
        'absolute' => TRUE,
      ));
    }
    if (!empty($path)) {
      globallink_insert_child_element($dom, $root, 'path', $path['alias']);
    }
    $root
      ->appendChild($url);
    $dom
      ->appendChild($root);
    $field_arr = field_info_instances('node', $node->type);
    $keys = array_keys($field_arr);
    foreach ($keys as $field) {
      $field_def = field_read_field($field);
      $items = field_get_items('node', $node, $field, $source_lang);
      if ($items) {
        $parent_fc = '';
        if ($field_def['type'] == 'field_collection') {
          $parent_fc = $field;
          if (isset($items[0]['revision_id'])) {
            entity_load('field_collection_item', array(
              $items[0]['value'],
            ), array(
              'revision_id' => $items[0]['revision_id'],
            ));
          }
        }
        globallink_entity_traverse_fields_and_field_collections('node', $node->type, $parent_fc, $node->type, $node->nid, $items, $field, $field_def, $dom, $root, $node, $target_arr, $is_hook_enabled);
      }
    }
    if (module_exists('metatag')) {
      if (isset($node->metatags)) {
        $ignore_metatags = [
          'geo.position',
          'icbm',
          'rating',
          'referrer',
          'revisit-after',
          'content-language',
        ];
        if ($for_display) {
          if (!empty($node->metatags[$node->language])) {
            $metatags = $node->metatags[$node->language];
            foreach ($metatags as $name => $value) {
              if (isset($value['value']) && !is_array($value['value'])) {
                if (in_array($name, $ignore_metatags)) {
                  continue;
                }
                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];
            foreach ($metatags as $name => $value) {
              if (isset($value['value']) && !is_array($value['value'])) {
                if (in_array($name, $ignore_metatags)) {
                  continue;
                }
                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();
}