You are here

function globallink_traverse_fields_and_field_collections in GlobalLink Connect for Drupal 7.5

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

Recursively adds fields and field collections to translation XML document.

Parameters

string $entity_type: The type of entity.

string $content_type: The content type.

string $parent_fc: The parent field collection.

string $bundle: The field collection bundle.

string $entity_id: The id of the entity.

array $items: Array of content.

string $field: The name of the field.

array $dom: Array representation of the DOM.

array $root: The root of the DOM.

object $source_node: The source node.

array $target_arr: The target array.

bool $is_hook_enabled: Whether or not the hook is enabled. Defaults to false.

string $langcode: The language code. Defaults to LANGUAGE_NONE.

1 call to globallink_traverse_fields_and_field_collections()
globallink_generate_xml_document in ./globallink_node.inc
Builds XML document with translation data.

File

./globallink_node.inc, line 1843

Code

function globallink_traverse_fields_and_field_collections($entity_type, $content_type, $parent_fc, $bundle, $entity_id, $items, $field, $dom, $root, $source_node, $target_arr, $is_hook_enabled = 0, $langcode = LANGUAGE_NONE) {
  if (!$items) {
    return;
  }
  $field_def = field_read_field($field);
  $max_length = '0';
  if (isset($field_def['settings']) && isset($field_def['settings']['max_length'])) {
    $max_length = $field_def['settings']['max_length'];
  }
  switch ($field_def['type']) {
    case 'list_boolean':
    case 'file':
    case 'taxonomy_term_reference':
    case 'field_collection':
      if (!module_exists('field_collection')) {
        break;
      }

      // Field Collection field, read the entity id from item and load
      // Entity object and then do recursion for nested field collections.
      foreach ($items as $entity_id_arr) {
        if (!isset($entity_id_arr['value'])) {
          continue;
        }
        $fc_entity_id = $entity_id_arr['value'];
        $field_collection_item_entity_arr = array();
        if (isset($entity_id_arr['revision_id'])) {
          $field_collection_item_entity_arr = entity_load('field_collection_item', array(
            $fc_entity_id,
          ), array(
            'revision_id' => $entity_id_arr['revision_id'],
          ));
        }
        else {
          $field_collection_item_entity_arr = entity_load('field_collection_item', array(
            $fc_entity_id,
          ));
        }
        if (!$field_collection_item_entity_arr || !is_array($field_collection_item_entity_arr) || sizeof($field_collection_item_entity_arr) < 1) {
          continue;
        }
        $field_collection_item_entity = $field_collection_item_entity_arr[$fc_entity_id];
        $field_collection_name = $field_collection_item_entity->field_name;
        $field_collection_item_array = get_object_vars($field_collection_item_entity);
        $arr = array_keys($field_collection_item_array);
        foreach ($arr as $key) {

          // Check if this key exists; If true then read this.
          $fc_field_def = field_read_field($key);
          if ($fc_field_def && !empty($fc_field_def) && isset($fc_field_def['type'])) {
            if ($fc_field_def['type'] != 'list_boolean' && $fc_field_def['type'] != 'file' && $fc_field_def['type'] != 'taxonomy_term_reference') {
              $fc_item = field_get_items('field_collection_item', $field_collection_item_entity, $key);
              if ($fc_item) {
                globallink_traverse_fields_and_field_collections('field_collection_item', $content_type, $parent_fc, $field_collection_name, $fc_entity_id, $fc_item, $key, $dom, $root, $source_node, $target_arr, $is_hook_enabled);
              }
            }
          }
        }
      }
      break;
    default:
      if ($is_hook_enabled == 0) {
        if (!globallink_is_field_configured_for_translation($entity_type, $bundle, $field, $content_type)) {
          break;
        }

        // Regular Text Field, get the content directly from items array
        foreach ($items as $delta => $item) {
          if (isset($item['value']) && is_string($item['value'])) {
            $f_label = field_info_instance($entity_type, $field, $bundle);
            $f_value = $item['value'];
            $f_format = isset($item['format']) && !is_null($item['format']) ? $item['format'] : '';
            globallink_insert_child_element($dom, $root, 'field', $f_value, array(
              'entity_type' => $entity_type,
              'content_type' => $content_type,
              'parent_fc' => $parent_fc,
              'bundle' => $bundle,
              'entity_id' => $entity_id,
              'field_name' => $field,
              'label' => $f_label['label'],
              'delta' => $delta,
              'format' => $f_format,
              'langcode' => $langcode,
              'max_length' => $max_length,
            ));
          }
          elseif ($field_def['type'] == 'link_field' && isset($item['title']) && is_string($item['title'])) {
            $f_label = field_info_instance($entity_type, $field, $bundle);
            $f_value = $item['title'];
            $f_format = isset($item['format']) && !is_null($item['format']) ? $item['format'] : '';
            globallink_insert_child_element($dom, $root, 'field', $f_value, array(
              'entity_type' => $entity_type,
              'content_type' => $content_type,
              'parent_fc' => $parent_fc,
              'bundle' => $bundle,
              'entity_id' => $entity_id,
              'field_name' => $field,
              'label' => $f_label['label'],
              'delta' => $delta,
              'format' => $f_format,
              'langcode' => $langcode,
              'max_length' => $max_length,
            ));
          }
          elseif ($field_def['type'] == 'image' && !module_exists('file_entity')) {
            $f_label = field_info_instance($entity_type, $field, $bundle);
            if (isset($item['alt'])) {
              $f_value = $item['alt'];
              globallink_insert_child_element($dom, $root, 'field_image', $f_value, array(
                'field_name' => $field,
                'type' => 'alt',
                'delta' => $delta,
                'max_length' => $max_length,
              ));
            }
            if (isset($item['title'])) {
              $f_value = $item['title'];
              globallink_insert_child_element($dom, $root, 'field_image', $f_value, array(
                'field_name' => $field,
                'type' => 'title',
                'delta' => $delta,
                'max_length' => $max_length,
              ));
            }
          }
          elseif ($field_def['type'] == 'image' && module_exists('file_entity')) {
            $parent_info = field_info_instance($entity_type, $field, $bundle);
            $file_entity_info = field_info_field($field);
            $file_fields = field_info_instances('file', $file_entity_info['type']);
            foreach ($file_fields as $file_field) {
              $f_value = field_get_items('file', (object) $item, $file_field['field_name']);
              if (empty($f_value)) {
                continue;
              }
              globallink_insert_child_element($dom, $root, "field_image", $f_value[0]['value'], array(
                'field_name' => $field,
                'subfield' => $file_field['field_name'],
                'delta' => $delta,
                'label' => $parent_info['label'],
                'langcode' => $langcode,
                'subfield_label' => $file_field['label'],
              ));
            }
          }
        }
      }
      else {
        if (!globallink_is_field_translatable($source_node, $field, $target_arr)) {
          break;
        }

        // Regular Text Field, get the content directly from items array
        foreach ($items as $delta => $item) {
          if (isset($item['value']) && is_string($item['value'])) {
            $f_label = field_info_instance($entity_type, $field, $bundle);
            $f_value = $item['value'];
            $f_format = isset($item['format']) && !is_null($item['format']) ? $item['format'] : '';
            globallink_insert_child_element($dom, $root, 'field', $f_value, array(
              'entity_type' => $entity_type,
              'content_type' => $content_type,
              'parent_fc' => $parent_fc,
              'bundle' => $bundle,
              'entity_id' => $entity_id,
              'field_name' => $field,
              'label' => $f_label['label'],
              'delta' => $delta,
              'format' => $f_format,
              'langcode' => $langcode,
              'max_length' => $max_length,
            ));
          }
          elseif ($field_def['type'] == 'link_field' && isset($item['title']) && is_string($item['title'])) {
            $f_label = field_info_instance($entity_type, $field, $bundle);
            $f_value = $item['title'];
            $f_format = isset($item['format']) && !is_null($item['format']) ? $item['format'] : '';
            globallink_insert_child_element($dom, $root, 'field', $f_value, array(
              'entity_type' => $entity_type,
              'content_type' => $content_type,
              'parent_fc' => $parent_fc,
              'bundle' => $bundle,
              'entity_id' => $entity_id,
              'field_name' => $field,
              'label' => $f_label['label'],
              'delta' => $delta,
              'format' => $f_format,
              'langcode' => $langcode,
              'max_length' => $max_length,
            ));
          }
        }
      }
  }
}