1

i want to switch the destination_dir in a custom file migration class derived from DrupalFile6Migration. The DrupalFile6Migration class is provided by the migrate_d2d module.

For this purpose i think it's necessary to use callbacks for field mappings like it's described here: https://www.drupal.org/node/1133448

I use successful a callback with the field mapping for destination_file in the file migration:

class FileMigration extends DrupalFile6Migration {
  public function __construct(array $arguments) {
    parent::__construct($arguments);
    $this->addFieldMapping('destination_file', 'filepath')
      ->callbacks(array($this, 'myPath'));
  ...
  protected function myPath($uri) {
    // do stuff
  }
...

But when i try do do this with the destination_dir i can't get success. XDebug shows, that myDestination() is never called:

  this->addFieldMapping('destination_dir')
    ->callbacks('myDestination');
  ...
  protected function myDestination() {
    if(...) {
       return 'private://';
    }
    else {
      return 'public://';
    }
  }

But i can change the destination_dir with defaultValue, e.g.:

  this->addFieldMapping('destination_dir')
    ->defaultValue('private://');

Any suggestions, knowings if callbacks() in combination with destination_dir is perhaps not possible?

1

Maybe you can use file_class like this

$this->addFieldMapping('field_yourimage_field', 'fidsarray');
$this->addFieldMapping('field_yourimage_field:file_class')
->defaultValue('MigrateFileFid');

The you have to implement prepare row to get the managed file fids you want to import like this

public function prepareRow($current_row) {
 if (parent::prepareRow($current_row) === FALSE) {
  return FALSE;
 }
 //sample code to get the file ids. it may be different in your case
 $fidsarray= array(); 
 $fidsarray = explode(",", $current_row->columnwith_file_ids_comma separated);  
 $current_row->fidsarray=$fidsarray;
}

if you dont have managed files and therefore fids available other options can be used too. See https://www.drupal.org/node/1540106

  • I can not understand, how this will solve a switch between the destination_dir? – surfi Jan 22 '15 at 12:47
  • Can you explain it a little bit further? – surfi Jan 22 '15 at 20:55
  • The solution I proposed is suitable if you have somehow 'managed' the files you want and then you just add the fids. The good thing with this way is that when you manage the files with custom code you have complete control about the directory that they will be stored. If you need more info I'll provide an update. – Nikos Tsagkas Jan 22 '15 at 21:20
1

OMG, it is possible with addFieldMapping(param1, param2), witha destination and source. The callback function can just override the source value and when there is no source value the callback is useless/never called.

So i change my code like this and everything is fine:

this->addFieldMapping('destination_dir', 'filepath', FALSE)
  ->callbacks(array($this, 'myDestination'));
...
protected function myDestination($uri) {
  if (preg_match('/^myfiles\/\w{2}\/thumbs\/\S+$')) {
    return 'public://';
  }
  else {
    return 'private://';
  }
}

Third parameter FALSE avoids warn_on_override :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.