You are here

function globallink_entity_save_field_collections_recursively in GlobalLink Connect for Drupal 7.5

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

Saves entity field collections recursively.

Parameters

string $entity_type: The entity type.

string $host_entity: The host entity.

string $fc_entity_id: The field collection entity ID.

array $translated_fc_arr: The translated field collection array.

string $target_lang: The entities' target language.

1 call to globallink_entity_save_field_collections_recursively()
globallink_entity_save_field_collections in globallink_entity/globallink_entity.inc
Saves entity field collections.

File

globallink_entity/globallink_entity.inc, line 1219

Code

function globallink_entity_save_field_collections_recursively($entity_type, $host_entity, $fc_entity_id, $translated_fc_arr, $target_lang) {
  $field_collection_item_entity = entity_load_unchanged('field_collection_item', $fc_entity_id);
  if (!$field_collection_item_entity) {
    return;
  }
  $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);
  $new_field_collection_arr = array(
    'field_name' => $field_collection_name,
  );
  foreach ($arr as $key) {

    // Check if this key exists; If true then read this.
    $fc_field_def = field_read_field($key);
    if (!empty($fc_field_def) && isset($fc_field_def['type'])) {
      switch ($fc_field_def['type']) {
        case 'list_boolean':
        case 'file':
        case 'taxonomy_term_reference':
          continue 2;
          break;
        case 'field_collection':
          if (isset($field_collection_item_array[$key]) && isset($field_collection_item_array[$key][$target_lang])) {
            $field_data_arr = $field_collection_item_array[$key][$target_lang];
            $new_field_collection_arr[$key][$target_lang] = $field_data_arr;
          }
          break;
        default:
          if (!isset($field_collection_item_array[$key])) {
            break;
          }
          if (!isset($field_collection_item_array[$key][LANGUAGE_NONE])) {
            break;
          }
          $field_data_arr = $field_collection_item_array[$key][LANGUAGE_NONE];
          foreach ($field_data_arr as $delta => $field_data) {
            if (isset($translated_fc_arr[$field_collection_name][$fc_entity_id][$key][$target_lang][$delta])) {
              $gl_obj = $translated_fc_arr[$field_collection_name][$fc_entity_id][$key][$target_lang][$delta];
              if (is_object($gl_obj)) {
                $translated_content = $gl_obj->translatedContent;
              }
              else {
                $translated_content = $gl_obj;
              }
              $new_field_collection_arr[$key][$target_lang][$delta] = array(
                'value' => $translated_content,
              );
              if (isset($field_data['format'])) {
                $new_field_collection_arr[$key][$target_lang][$delta]['format'] = $field_data['format'];
              }
            }
          }
      }
    }
  }
  if ($entity_type != 'node') {
    $host_entity->tpt_skip = TRUE;
  }
  $new_entity = entity_create('field_collection_item', $new_field_collection_arr);

  // Create new field collection item.
  $new_entity
    ->setHostEntity($entity_type, $host_entity);

  // Attach it to the node.
  $new_entity
    ->save(TRUE);

  // Save field-collection item
  field_attach_presave($entity_type, $host_entity);
  field_attach_update($entity_type, $host_entity);

  // Now set the child FC if any
  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'] == 'field_collection') {
        $items = field_get_items('field_collection_item', $field_collection_item_entity, $key);
        if ($items) {
          foreach ($items as $entity_id_arr) {
            if (isset($entity_id_arr['value'])) {
              $fc_entity_id = $entity_id_arr['value'];
              globallink_entity_save_field_collections_recursively('field_collection_item', $new_entity, $fc_entity_id, $translated_fc_arr, $target_lang);
            }
          }
        }
      }
    }
  }
}