Skip to content

Instantly share code, notes, and snippets.

@vinmassaro
Created October 13, 2014 21:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vinmassaro/049109c69d59aa2d33c0 to your computer and use it in GitHub Desktop.
Save vinmassaro/049109c69d59aa2d33c0 to your computer and use it in GitHub Desktop.
<?php
/**
* Fix importer mappings for file and image fields to use :uri convention.
*/
function feeds_update_7210(&$sandbox) {
// If this is the first pass through this update function then set some variables.
if (!isset($sandbox['progress'])) {
$sandbox['importers'] = feeds_importer_load_all(TRUE);
$sandbox['progress'] = 0;
$sandbox['max'] = count($sandbox['importers']);
}
$importer_index = $sandbox['progress'];
$importer = array_pop($sandbox['importers']);
$processor = $importer->processor;
$config = $processor->getConfig();
foreach ($config['mappings'] as $id => $mapping) {
// Act on mappings that do not contain a colon.
if (!strpos($mapping['target'], ':')) {
// Get field data. Check if $info is empty to weed out non-field mappings like temporary targets.
$info = field_info_field($mapping['target']);
if (!empty($info)) {
// Act on file or image fields.
if (in_array($info['type'], array('file', 'image'))) {
// Add ':uri' to fix the mapping.
$config['mappings'][$id]['target'] = $mapping['target'] . ':uri';
}
}
}
}
// Add the updated config and save the importer.
$processor->addConfig(array('mappings' => $config['mappings']));
$importer->save();
$sandbox['progress']++;
// Set the value for finished. If progress == max then finished will be 1, signifying we are done.
$sandbox['#finished'] = ($sandbox['progress'] / $sandbox['max']);
return t('@importers importers processed.', array('@importers' => $sandbox['max']));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment