You are here

function globallink_entity_traverse_fields_and_field_collections in GlobalLink Connect for Drupal 7.7

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

Traverses entity fields and field collections.

Parameters

string $entity_type: The entity type.

string $content_type: The content type.

string $parent_fc: The parent field collection.

string $bundle: The bundle.

string $entity_id: The entity ID.

array $items: The array of items.

object $field: The entity field.

object $field_def: The field definition object.

object $dom: Object representation of the DOM.

object $root: The DOM root.

object $source_node: The source node.

array $target_arr: The target array.

bool $is_hook_enabled: Determines whether or not the hook is enabled. Defaults to 0.

2 calls to globallink_entity_traverse_fields_and_field_collections()
globallink_entity_generate_xml_document in globallink_entity/globallink_entity.inc
Generates XML document for entity.
globallink_entity_reference_generate_xml_document in globallink_custom_entity/globallink_custom_entity.inc

File

globallink_entity/globallink_entity.inc, line 1015

Code

function globallink_entity_traverse_fields_and_field_collections($entity_type, $content_type, $parent_fc, $bundle, $entity_id, $items, $field, $field_def, $dom, $root, $source_node, $target_arr, $is_hook_enabled = 0) {
  if (!$items) {
    return;
  }
  switch ($field_def['type']) {
    case 'list_boolean':
    case 'file':
    case 'taxonomy_term_reference':
      return;
  }
  if ($field_def['type'] != 'field_collection') {
    if ($is_hook_enabled == 0) {
      if (!globallink_is_field_configured_for_translation($entity_type, $bundle, $field, $content_type)) {
        return;
      }

      // Regular Text Field, get the content directly from items array
      foreach ($items as $delta => $item) {
        $field_info_all = field_info_field($field);
        $sum_field = '';
        if ($field_info_all['type'] == 'text_with_summary' && globallink_is_field_configured_for_translation($entity_type, $bundle, $field . '@summary', $content_type)) {
          $sum_field = $field . '@summary';
        }
        if (isset($item['summary']) && is_string($item['summary']) && !empty($sum_field)) {
          if (globallink_is_field_configured_for_translation($entity_type, $bundle, $field, $content_type)) {
            $f_label = $field . ' Summary';
            $f_value = $item['summary'];
            $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' => $sum_field,
              'label' => $f_label,
              'delta' => $delta,
              'format' => $f_format,
            ));
          }
        }
        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,
          ));
        }
        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,
          ));
        }
        elseif ($field_def['type'] == 'image') {
          $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,
            ));
          }
          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,
            ));
          }
        }
        elseif ($field_def['type'] == 'entityreference' && isset($item['target_id'])) {

          //call function to generate xml for the referenced entity here.
          $reference_type = $field_info_all['settings']['target_type'];
          $reference_id = $item['target_id'];
          if (!isset($_SESSION['eck'][$reference_type][$reference_id]) || $_SESSION['eck'][$reference_type][$reference_id] != $reference_id) {
            $_SESSION['eck'][$reference_type][$reference_id] = $reference_id;
            $entity_ref[$reference_type][] = $reference_id;
          }
          else {
            watchdog(GLOBALLINK_MODULE, 'Skipping entity Id - %id as it is in active submission', array(
              '%id' => $reference_id,
            ), WATCHDOG_DEBUG);
          }
        }
      }
    }
    else {
      if (!globallink_is_field_translatable($source_node, $field, $target_arr)) {
        return;
      }

      // Regular Text Field, get the content directly from items array
      foreach ($items as $delta => $item) {
        $field_info_all = field_info_field($field);
        $sum_field = '';
        if ($field_info_all['type'] == 'text_with_summary' && globallink_is_field_configured_for_translation($entity_type, $bundle, $field . '@summary', $content_type)) {
          $sum_field = $field . '@summary';
        }
        if (isset($item['summary']) && is_string($item['summary']) && !empty($sum_field)) {
          if (globallink_is_field_configured_for_translation($entity_type, $bundle, $field, $content_type)) {
            $f_label = $field . ' Summary';
            $f_value = $item['summary'];
            $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' => $sum_field,
              'label' => $f_label,
              'delta' => $delta,
              'format' => $f_format,
            ));
          }
        }
        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,
          ));
        }
        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,
          ));
        }
        elseif ($field_def['type'] == 'entityreference' && isset($item['target_id'])) {
          $reference_type = $field_info_all['settings']['target_type'];
          $reference_id = array(
            $item['target_id'],
          );
          if (!isset($_SESSION['eck'][$reference_type][$reference_id]) || $_SESSION['eck'][$reference_type][$reference_id] != $reference_id) {
            $_SESSION['eck'][$reference_type][$reference_id] = $reference_id;
            $entity_ref[$reference_type][] = $reference_id;
          }
          else {
            watchdog(GLOBALLINK_MODULE, 'Skipping entity Id - %id as it is in active submission', array(
              '%id' => $reference_id,
            ), WATCHDOG_DEBUG);
          }
        }
      }
    }
  }
  else {
    if (!module_exists('field_collection')) {
      return;
    }

    // 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'])) {
          continue;
        }
        switch ($fc_field_def['type']) {
          case 'list_boolean':
          case 'file':
          case 'taxonomy_term_reference':
            continue 2;
        }
        $fc_item = field_get_items('field_collection_item', $field_collection_item_entity, $key);
        if ($fc_item) {
          globallink_entity_traverse_fields_and_field_collections('field_collection_item', $content_type, $parent_fc, $field_collection_name, $fc_entity_id, $fc_item, $key, $fc_field_def, $dom, $root, $source_node, $target_arr, $is_hook_enabled);
        }
      }
    }
  }
  return $entity_ref;
}