diff --git a/field_collection.module b/field_collection.module index b5cfc07..8c1b996 100644 --- a/field_collection.module +++ b/field_collection.module @@ -1509,3 +1509,129 @@ 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, $main_mapping) { + 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(); + try { + 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); + } + + $sub_mapping = array(); + $config = $source->importer()->getConfig(); + if (!empty($config['processor']['config']['mappings'])) { + foreach ($config['processor']['config']['mappings'] as $mapping) { + if ($mapping['target'] == $target . ':' . $sub_target/* && $main_mapping['language'] == $mapping['language']*/) { + $sub_mapping = $mapping; + $sub_mapping['target'] = $sub_target; + break; + } + } + } + + 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), $sub_mapping); + } + + $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++; + } + } catch (Exception $e) { + drupal_set_message($e->getMessage(), 'error'); + watchdog_exception('field_collection', $e, $e->getMessage()); + throw $e; + } + + $entity->{$target} = $field; +} \ No newline at end of file