Skip to content

Instantly share code, notes, and snippets.

@pcambra
Last active November 20, 2015 11:00
Show Gist options
  • Select an option

  • Save pcambra/9de9997ba8780e1f2a2e to your computer and use it in GitHub Desktop.

Select an option

Save pcambra/9de9997ba8780e1f2a2e to your computer and use it in GitHub Desktop.
File field feeds - field collection https://www.drupal.org/node/1063434
/**
* Implements hook_feeds_processor_targets_alter().
*
* Overrides the file.inc mapping to correctly set the allowed extensions for
* files inside field collections.
*/
function {my_module}_feeds_processor_targets_alter(array &$targets, $entity_type, $bundle) {
if ($bundle == 'field_files_collection') {
foreach ($targets as $target_id => &$target) {
if ((isset($target['real_target'])) && $target['real_target'] == 'field_file') {
if ($target_id == 'field_file:uri') {
$target['callback'] = '{my_module}_file_feeds_set_target';
}
}
}
}
}
/**
* Callback for mapping file fields inside field_collections.
*
* @see file_feeds_set_target()
*/
function {my_module}_file_feeds_set_target(FeedsSource $source, $entity, $target, array $values) {
// Add default of uri for backwards compatibility.
list($field_name, $sub_field) = explode(':', $target . ':uri');
$info = field_info_field($field_name);
// We're acting for uris only.
if ($sub_field != 'uri') {
return;
}
foreach ($values as $k => $v) {
if (!($v instanceof FeedsEnclosure)) {
if (is_string($v)) {
$values[$k] = new FeedsEnclosure($v, file_get_mimetype($v));
}
else {
// Set the value for FALSE rather than remove it to keep our deltas
// correct.
$values[$k] = FALSE;
}
}
}
$entity_type = $entity->entityType();
$bundle = $entity->bundle();
$instance_info = field_info_instance($entity_type, $field_name, $bundle);
// Determine file destination.
// @todo This needs review and debugging.
$data = array();
if (!empty($entity->uid)) {
$data[$entity_type] = $entity;
}
$destination = file_field_widget_uri($info, $instance_info, $data);
// Populate entity.
$field = isset($entity->$field_name) ? $entity->$field_name : array(LANGUAGE_NONE => array());
$delta = 0;
foreach ($values as $v) {
if ($info['cardinality'] == $delta) {
break;
}
if (!isset($field[LANGUAGE_NONE][$delta])) {
$field[LANGUAGE_NONE][$delta] = array();
}
if ($v) {
try {
$v->setAllowedExtensions($instance_info['settings']['file_extensions']);
$field[LANGUAGE_NONE][$delta] += (array) $v->getFile($destination);
// @todo: Figure out how to properly populate this field.
$field[LANGUAGE_NONE][$delta]['display'] = 1;
}
catch (Exception $e) {
watchdog('feeds', check_plain($e->getMessage()));
}
}
$delta++;
}
$entity->$field_name = $field;
}
/**
* Implements hook_module_implements_alter().
*/
function {my_module}_module_implements_alter(&$implementations, $hook) {
switch ($hook) {
case 'feeds_processor_targets_alter':
// Move some of our hook implementations to the end of the list.
$group = $implementations['{my_module}'];
unset($implementations['{my_module}']);
$implementations['{my_module}'] = $group;
$hola = 1;
break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment