You are here

function globallink_entity_traverse_fields_and_field_collections in GlobalLink Connect for Drupal 7.5

Same name and namespace in other branches
  1. 7.7 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.

1 call to globallink_entity_traverse_fields_and_field_collections()
globallink_entity_generate_xml_document in globallink_entity/globallink_entity.inc
Generates XML document for entity.

File

globallink_entity/globallink_entity.inc, line 1520

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) {
        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,
            ));
          }
        }
      }
    }
    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) {
        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,
          ));
        }
      }
    }
  }
  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);
        }
      }
    }
  }
}