You are here

function globallink_entity_get_translated_array in GlobalLink Connect for Drupal 7.5

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

Gets array of translated entities.

Parameters

object $xml: XML representation of entities.

string $target_lang: The entities' target language. Defaults to LANGUAGE_NONE.

Return value

array Associative array of translated entities.

2 calls to globallink_entity_get_translated_array()
globallink_entity_get_translations_for_row_id in globallink_entity/globallink_entity_receive.inc
Gets entity translations by row ID.
globallink_entity_update in globallink_entity/globallink_entity.inc
Updates entity.

File

globallink_entity/globallink_entity.inc, line 761

Code

function globallink_entity_get_translated_array($xml, $target_lang = LANGUAGE_NONE) {
  if (is_null($xml) || !is_string($xml) || $xml == '') {
    return array();
  }
  $dom = new DomDocument();
  $dom->preserveWhiteSpace = FALSE;
  $dom
    ->loadXML($xml);
  $arr = array();
  $contents = $dom
    ->getElementsByTagName('content');
  foreach ($contents as $content) {
    if (!is_null($content->attributes)) {
      foreach ($content->attributes as $attr_name => $attr_node) {
        switch ($attr_name) {
          case 'rid':
            $arr['rid'] = $attr_node->value;
            break;
          case 'nid':
            $arr['nid'] = $attr_node->value;
            break;
          case 'vid':
            $arr['vid'] = $attr_node->value;
            break;
        }
      }
    }
  }
  $titles = $dom
    ->getElementsByTagName('title');
  foreach ($titles as $title) {
    $arr['title'] = $title->nodeValue;
  }
  $paths = $dom
    ->getElementsByTagName('path');
  foreach ($paths as $path) {
    $arr['path'] = $path->nodeValue;
  }
  $field_image = $dom
    ->getElementsByTagName('field_image');
  foreach ($field_image as $attr) {
    $field_image_obj = new GLFieldImage();
    if (!is_null($attr->attributes)) {
      foreach ($attr->attributes as $attr_name => $attr_node) {
        switch ($attr_name) {
          case 'type':
            if ($attr_node->value == 'title') {
              $field_image_obj->title = $attr->nodeValue;
            }
            elseif ($attr_node->value == 'alt') {
              $field_image_obj->alt = $attr->nodeValue;
            }
            continue 2;
          case 'delta':
            $field_image_obj->delta = $attr_node->value;
            continue 2;
          case 'field_name':
            $field_image_obj->field_name = $attr_node->value;
            continue 2;
        }
      }
      if (is_null($field_image_obj->delta)) {
        $field_image_obj->delta = '0';
      }
      if (isset($field_image_obj->field_name)) {
        if (isset($field_image_obj->title)) {
          $arr[$field_image_obj->field_name][$target_lang][$field_image_obj->delta]->title = $field_image_obj->title;
        }
        if (isset($field_image_obj->alt)) {
          $arr[$field_image_obj->field_name][$target_lang][$field_image_obj->delta]->alt = $field_image_obj->alt;
        }
      }
      else {
        $arr[$field_image_obj->field_name][$target_lang][$field_image_obj->delta] = $field_image_obj;
      }
    }
  }
  $fields = $dom
    ->getElementsByTagName('field');
  foreach ($fields as $field) {
    $field_obj = new GLField();
    $field_obj->type = 'field';
    $field_obj->translatedContent = $field->nodeValue;
    if (!is_null($field->attributes)) {
      foreach ($field->attributes as $attr_name => $attr_node) {
        switch ($attr_name) {
          case 'entity_type':
            $field_obj->entityType = $attr_node->value;
            continue 2;
          case 'content_type':
            $field_obj->contentType = $attr_node->value;
            continue 2;
          case 'parent_fc':
            $field_obj->parentFCName = $attr_node->value;
            continue 2;
          case 'bundle':
            $field_obj->bundle = $attr_node->value;
            continue 2;
          case 'entity_id':
            $field_obj->entityId = $attr_node->value;
            continue 2;
          case 'field_name':
            $field_obj->fieldName = $attr_node->value;
            continue 2;
          case 'label':
            $field_obj->fieldLabel = $attr_node->value;
            continue 2;
          case 'delta':
            $field_obj->delta = $attr_node->value;
            continue 2;
          case 'format':
            $field_obj->format = $attr_node->value;
            continue 2;
        }
      }
      if (is_null($field_obj->entityId)) {
        $field_obj->entityId = '0';
      }
      if (is_null($field_obj->bundle)) {
        $field_obj->bundle = $field_obj->fieldName;
      }
      if (is_null($field_obj->delta)) {
        $field_obj->delta = '0';
      }
      if ($field_obj->entityType == 'node') {
        $arr[$field_obj->fieldName][$target_lang][$field_obj->delta] = $field_obj;
      }
      else {
        $arr['field_collection'][$field_obj->parentFCName][$field_obj->bundle][$field_obj->entityId][$field_obj->fieldName][$target_lang][$field_obj->delta] = $field_obj;
      }
    }
  }
  $metatags = $dom
    ->getElementsByTagName('metatag');
  foreach ($metatags as $metatag) {
    $metatag_obj = new GLField();
    $metatag_obj->type = 'metatag';
    $metatag_obj->translatedContent = $metatag->nodeValue;
    if (!is_null($metatag->attributes)) {
      foreach ($metatag->attributes as $attr_name => $attr_node) {
        switch ($attr_name) {
          case 'entity_type':
            $metatag_obj->entityType = $attr_node->value;
            continue 2;
          case 'content_type':
            $field_obj->contentType = $attr_node->value;
            continue 2;
          case 'bundle':
            $field_obj->bundle = $attr_node->value;
            continue 2;
          case 'entity_id':
            $metatag_obj->entityId = $attr_node->value;
            continue 2;
          case 'name':
            $metatag_obj->fieldName = $attr_node->value;
            continue 2;
          case 'label':
            $metatag_obj->fieldLabel = $attr_node->value;
            continue 2;
        }
      }
      if (is_null($metatag_obj->entityId)) {
        $metatag_obj->entityId = '0';
      }
      if (is_null($metatag_obj->bundle)) {
        $metatag_obj->bundle = $metatag_obj->fieldName;
      }
      $arr['metatag'][$metatag_obj->bundle][$metatag_obj->entityId] = $metatag_obj;
    }
  }
  return $arr;
}