You are here

function globallink_beans_get_xml in GlobalLink Connect for Drupal 7.6

Same name and namespace in other branches
  1. 7.7 globallink_beans/globallink_beans.inc \globallink_beans_get_xml()

Gets XML data from specific bean.

Parameters

string $bid: The bean ID.

string $target_arr: The array of target locales.

Return value

array Associative array of bean XML data.

2 calls to globallink_beans_get_xml()
globallink_beans_get_translations_for_row_id in globallink_beans/globallink_beans_receive.inc
Gets beans translations by row ID.
globallink_beans_send_for_translations in globallink_beans/globallink_beans.inc
Sends beans for translation.

File

globallink_beans/globallink_beans.inc, line 167

Code

function globallink_beans_get_xml($bid, $target_arr) {
  $dom = new DOMDocument('1.0', 'UTF-8');
  $dom->formatOutput = TRUE;
  $root = $dom
    ->createElement('content');
  $dom
    ->appendChild($root);
  $id = $dom
    ->createAttribute('bid');
  $id->value = $bid;
  $root
    ->appendChild($id);
  $beans = entity_load('bean', array(
    $bid,
  ));
  $key = key($beans);
  $bean = $beans[$key];
  if (module_exists('metatag')) {
    $lang = entity_language('bean', $bean);
    if (isset($bean->metatags)) {
      if (globallink_is_field_configured_for_translation('bean', 'bean:' . $bean->type, 'metatags', 'bean:' . $bean->type)) {
        if (!empty($bean->metatags[$lang])) {
          $metatags = $bean->metatags[$lang];
          foreach ($metatags as $name => $value) {
            if (isset($value['value'])) {
              globallink_insert_child_element($dom, $root, 'metatag', $value['value'], array(
                'entity_type' => 'bean',
                'name' => $name,
                'label' => 'Metatag - ' . ucwords($name),
              ));
            }
          }
        }
      }
    }
  }
  $fields = globallink_bean_get_fields($bean->type);
  $configured_fields = globallink_get_configured_fields_for_bean($bean->type);
  foreach ($fields as $field) {
    if (empty($bean->{$field})) {
      continue;
    }
    $language = field_language('bean', $bean, $field, NULL);
    $field_values = field_get_items('bean', $bean, $field, $language);
    $field_info = field_info_field($field);
    if ($field_values) {
      $parent_fc = '';
      if ($field_info['type'] == 'field_collection') {
        $parent_fc = $field;
        if (isset($field_values[0]['revision_id'])) {
          $field_collection_item_entity = entity_load('field_collection_item', array(
            $field_values[0]['value'],
          ), array(
            'revision_id' => $field_values[0]['revision_id'],
          ));
        }
        globallink_beans_traverse_fields_and_field_collections('bean', $bean->type, $parent_fc, $bean->type, $bean->bid, $field_values, $field, $dom, $root, $bean, $target_arr, $language);
        continue;
      }
    }
    if (array_key_exists($field, $configured_fields) && $configured_fields[$field]->translatable == 1) {
      if (is_array($field_values) && !empty($field_values)) {
        foreach ($field_values as $delta => $values) {
          if ($field_info['type'] == 'link_field') {
            globallink_insert_child_element($dom, $root, 'field', $values['title'], array(
              'field_name' => $field,
              'delta' => $delta,
              'langcode' => $language,
            ));
          }
          elseif ($field_info['type'] == 'image') {
            if (isset($values['alt'])) {
              globallink_insert_child_element($dom, $root, 'field_image', $values['alt'], array(
                'field_name' => $field,
                'type' => 'alt',
                'delta' => $delta,
                'langcode' => $language,
              ));
            }
            if (isset($values['title'])) {
              globallink_insert_child_element($dom, $root, 'field_image', $values['title'], array(
                'field_name' => $field,
                'type' => 'title',
                'delta' => $delta,
                'langcode' => $language,
              ));
            }
          }
          elseif (!empty($values['value'])) {
            globallink_insert_child_element($dom, $root, 'field', $values['value'], array(
              'field_name' => $field,
              'delta' => $delta,
              'langcode' => $language,
            ));
          }
        }
      }
      elseif (!empty($field_values) && is_string($field_values)) {
        globallink_insert_child_element($dom, $root, 'field', $field_values, array(
          'field_name' => $field,
        ));
      }
    }
  }
  $xml = $dom
    ->saveXML();
  return $xml;
}