diff --git a/field_collection.module b/field_collection.module index b5cfc07..3fd4198 100644 --- a/field_collection.module +++ b/field_collection.module @@ -1509,3 +1509,114 @@ function field_collection_admin_menu_map() { return $map; } } + +/** + * Implements hook_feeds_processor_targets_alter() + */ +function field_collection_feeds_processor_targets_alter(&$targets, $entity_type, $bundle_name) { + // Thou shalt not attempt Sorcery! + if ($entity_type == 'field_collection_item') { + return; + } + foreach (field_info_instances($entity_type, $bundle_name) as $name => $instance) { + $info = field_info_field($name); + if ($info['type'] == 'field_collection') { + + static $loaded = FALSE; + if (!$loaded) { + $path = drupal_get_path('module', 'feeds') . '/mappers'; + $files = drupal_system_listing('/.*\.inc$/', $path, 'name', 0); + foreach ($files as $file) { + if (strstr($file->uri, '/mappers/')) { + require_once(DRUPAL_ROOT . '/' . $file->uri); + } + } + } + $loaded = TRUE; + $fc_fields = field_info_instances('field_collection_item', $info['field_name']); + + $sub_type = 'field_collection_item'; + + $new_targets = array(); + $new_targets = module_invoke_all('feeds_processor_targets', $sub_type, $info['field_name']); + drupal_alter('feeds_processor_targets', $new_targets, $sub_type, $info['field_name']); + + foreach ($new_targets as $sub_name => $target) { + $new_name = t($info['field_name']) . ':' . t($sub_name); + $targets[$new_name] = $target; + if (isset($target['name'])) { + $targets[$new_name]['name'] = $instance['label'] . ':' . $target['name']; + } + + // We override callback for now and retrieve original later. + $targets[$new_name]['callback'] = 'field_collection_feeds_set_target'; + } + } + } +} + +/** + * Process Field Collection items + */ +function field_collection_feeds_set_target($source, $entity, $target, $value) { + static $sub_targets = array(); + + $args = explode(':', $target); + $target = array_shift($args); + $sub_target = implode(':', $args); + + $sub_type = 'field_collection_item'; + $new_targets = module_invoke_all('feeds_processor_targets', $sub_type, $target); + drupal_alter('feeds_processor_targets', $new_targets, $sub_type, $target); + + // Now we retrieve old callbacks and keep then on a static cache + if (!isset($sub_targets[$target])) { + $sub_targets[$target] = array(); + drupal_alter('feeds_processor_targets', $sub_targets[$target], $sub_type, $target); + } + $_sub_targets = $new_targets; + + $value = is_array($value) ? $value : array($value); + $info = field_info_field($target); + + // Iterate over all values. + $delta = 0; + $field = isset($entity->$target) ? $entity->$target : array(); + foreach ($value as $v) { + if (empty($v)) { + // Avoid creation of empty field collections + continue; + } + if (isset($field['und'][$delta]['entity'])) { + $field_collection_item = $field['und'][$delta]['entity']; + } + elseif (isset($field['und'][$delta]['value'])) { + $field_collection_item = field_collection_item_load($field['und'][$delta]['value']); + } + if (empty($field_collection_item)) { + $field_collection_item = entity_create('field_collection_item', array('field_name' => $target)); + $field_collection_item->setHostEntity($entity->feeds_item->entity_type, $entity); + } + + // @todo: not sure if we still need to pre-process "special" field types to get subfields (e.g. addressfield, urls, phone numbers, etc.) +// if (isset($_sub_targets[$sub_target]['callback']) && function_exists($_sub_targets[$sub_target]['callback'])) { +// $callback = $_sub_targets[$sub_target]['callback']; +// $callback($source, $field_collection_item, $sub_target, (array)$v); +// } + + $cwrapper = entity_metadata_wrapper('field_collection_item', $field_collection_item); + $cwrapper->{$sub_target}->set($v); + $field_collection_item->save(true); // TRUE to skip host entity save - don't need to save the node here + + $field['und'][$delta]['entity'] = $field_collection_item; + + unset($field_collection_item); + + if ($info['cardinality'] == 1) { + break; + } + $delta++; + } + + $entity->{$target} = $field; +} \ No newline at end of file