public function MigrateFileFieldBaseHandler::prepare in Migrate 7.2
Implementation of MigrateFieldHandler::prepare().
Prepare file data for saving as a Field API file field.
Return value
array Field API array suitable for inserting in the destination object.
File
- plugins/
destinations/ fields.inc, line 675 - Support for processing entity fields
Class
- MigrateFileFieldBaseHandler
- The next generation of file field handler. This class focuses on the file field itself, and offloads understanding of obtaining the actual file and dealing with the file entity to an embedded MigrateFileInterface instance.
Code
public function prepare($entity, array $field_info, array $instance, array $values) {
if (isset($values['arguments'])) {
$arguments = $values['arguments'];
unset($values['arguments']);
}
else {
$arguments = array();
}
$default_language = $this
->getFieldLanguage($entity, $field_info, $arguments);
$migration = Migration::currentMigration();
// One can override the source class via CLI or drushrc.php (the
// option is named file_function for historical reasons).
if ($migration
->getOption('file_function')) {
$file_class = $migration
->getOption('file_function');
}
elseif (!empty($arguments['file_class'])) {
$file_class = $arguments['file_class'];
}
else {
$file_class = 'MigrateFileUri';
}
// If a destination directory
// (relative to the Drupal public files directory) is not
// explicitly provided, use the default for the field.
if (empty($arguments['destination_dir'])) {
$arguments['destination_dir'] = $this
->destinationDir($field_info, $instance);
}
$return = array();
// Note that what $value represents depends on the file class -
// MigrateFileUri expects a filespec/URI, MigrateFileFid expects a file ID,
// etc.
foreach ($values as $delta => $value) {
if ($value) {
// Handle potentially multiple arguments.
$instance_arguments = array();
foreach ($arguments as $key => $argument) {
// For a scalar argument, pass it directly.
if (!is_array($argument)) {
$instance_arguments[$key] = $argument;
}
else {
if (isset($argument[$delta])) {
$instance_arguments[$key] = $argument[$delta];
}
else {
$migration
->saveMessage(t('No data for subfield %key at row %delta for field %field', array(
'%key' => $key,
'%delta' => $delta,
'%field' => $field_info['field_name'],
)), Migration::MESSAGE_WARNING);
}
}
}
// If the parent entity doesn't have an explicit uid, give ownership
// to the anonymous account.
$owner = isset($entity->uid) ? $entity->uid : 0;
// Call the MigrateFileInterface implementation to do the real work.
$source = new $file_class($instance_arguments);
$file = $source
->processFile($value, $owner);
// Assuming we got back a valid file ID, build the proper field
// array out of it. We assume that if we did not get back a fid, the
// MigrateFile class has saved a message indicating why.
if ($file) {
$field_array = array(
'fid' => $file->fid,
);
$language = isset($instance_arguments['language']) ? $instance_arguments['language'] : $default_language;
if (is_array($language)) {
$language = $language[$delta];
}
$return[$language][] = $this
->buildFieldArray($field_array, $instance_arguments, $delta);
}
}
}
return $return;
}