function globallink_beans_save_field_collections_recursively in GlobalLink Connect for Drupal 7.7
Same name and namespace in other branches
- 7.6 globallink_beans/globallink_beans.inc \globallink_beans_save_field_collections_recursively()
Saves field collections recursively.
Parameters
string $entity_type: The type of entity.
string $host_entity: The host entity.
string $fc_entity_id: The id of the field collection.
array $translated_fc_arr: The array of translated field collections.
1 call to globallink_beans_save_field_collections_recursively()
- globallink_beans_save_field_collections in globallink_beans/
globallink_beans.inc
File
- globallink_beans/
globallink_beans.inc, line 565
Code
function globallink_beans_save_field_collections_recursively($entity_type, $host_entity, $fc_entity_id, $translated_fc_arr, $target_locale) {
$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 (!$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;
case 'field_collection':
$lang = key($field_collection_item_array[$key]);
if (isset($field_collection_item_array[$key]) && isset($field_collection_item_array[$key][$lang])) {
$field_data_arr = $field_collection_item_array[$key][$lang];
$new_field_collection_arr[$key][$target_locale] = $field_data_arr;
}
break;
default:
$lang = key($field_collection_item_array[$key]);
if (!isset($field_collection_item_array[$key]) || !isset($field_collection_item_array[$key][$lang])) {
continue 2;
}
$field_data_arr = $field_collection_item_array[$key][$lang];
foreach ($field_data_arr as $delta => $field_data) {
if (isset($translated_fc_arr[$field_collection_name][$fc_entity_id][$key][LANGUAGE_NONE][$delta])) {
$gl_obj = $translated_fc_arr[$field_collection_name][$fc_entity_id][$key][LANGUAGE_NONE][$delta];
if (is_object($gl_obj)) {
$translated_content = $gl_obj->translatedContent;
}
else {
$translated_content = $gl_obj;
}
if (isset($field_data['format'])) {
$format = $field_data['format'];
$new_field_collection_arr[$key][$target_locale][$delta] = array(
'value' => $translated_content,
'format' => $format,
);
}
else {
$new_field_collection_arr[$key][$target_locale][$delta] = array(
'value' => $translated_content,
);
}
}
else {
$new_field_collection_arr[$key][$target_locale][$delta] = $field_data;
}
}
}
}
if ($entity_type != 'node') {
$host_entity->tpt_skip = TRUE;
}
$field_collection_item = entity_create('field_collection_item', $new_field_collection_arr);
// Create new field collection item.
$field_collection_item
->setHostEntity($entity_type, $host_entity, $target_locale);
// Attach it to the bean.
$field_collection_item
->save(TRUE);
// Save field-collection item.
$host_entity->{$new_field_collection_arr['field_name']}[$target_locale][0] = array(
'value' => $field_collection_item->item_id,
'revision_id' => $field_collection_item->revision_id,
);
$host_entity
->save();
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_beans_save_field_collections_recursively('field_collection_item', $field_collection_item, $fc_entity_id, $translated_fc_arr, $target_locale);
}
}
}
}
}
}
}