Fix for Code

  1. Index: migrate_example/beer.migrate_example.inc
  2. ===================================================================
  3. --- migrate_example/beer.migrate_example.inc    (revision 135)
  4. +++ migrate_example/beer.migrate_example.inc    (working copy)
  5. @@ -%ld,%ld +%ld,%ld @@
  6.                                    ));
  7.      $this->destination = new MigrateDestinationEntity('node', 'migrate_example_beer');
  8.  
  9. -    // Example of a single value image.
  10. -    $this->addFieldMapping(array('callback' => 'prepare_file', 'arguments' => array('source_field_name' => 'image', 'source_path' => drupal_get_path('module', 'migrate_example'))), 'field-migrate-example-image');
  11. -
  12.      $this->addFieldMapping('name', 'title');
  13.      $this->addFieldMapping('aid', 'uid');
  14.      $this->addFieldMapping(NULL, 'sticky', 1);
  15.  
  16.      // BEWARE. Replace underscores with dashes in field names. @see entity_metadata_field_property_set().
  17.  
  18. +    // Example of a single value image.
  19. +    // Waiting on entity.module support: http://drupal.org/node/708268
  20. +    // $this->addFieldMappingCallback('field-migrate-example-image', 'prepare_file', array('source_field_name' => 'image', 'source_path' => drupal_get_path('module', 'migrate_example'))));
  21. +
  22.      // Example of delimited field handling. Also example of unformatted multi-value text field.
  23. -    $this->addFieldMapping(array('callback' => 'prepare_explode', 'arguments' => array('source_field_name' => 'countries', 'separator' => '|')), 'field-migrate-example-country');
  24. +    $this->addFieldMappingCallback('field-migrate-example-country', 'prepare_explode', array('source_field_name' => 'countries', 'separator' => '|'));
  25.  
  26.      // Example of a reference field. In this case, a term_reference.
  27.      // $this->addFieldMapping(array('callback' => 'prepare_keyed_object', 'arguments' => array('source_field_name' => 'countries', 'key' => 'tid')), 'taxonomy-tags');
  28.  
  29.      // Example of a single value textfield with input format.
  30. -    $this->addFieldMapping(array('callback' => 'prepare_textfield', 'arguments' => array('source_field_name' => 'manufacturer', 'format' => 2)), 'body');
  31. +    $this->addFieldMappingCallback('body', 'prepare_textfield', array('source_field_name' => 'manufacturer', 'format' => 2));
  32.  
  33.    }
  34.  }
  35. @@ -%ld,%ld +%ld,%ld @@
  36.      $this->addFieldMapping('name', 'name');
  37.      $this->addFieldMapping('manufacturer_mail', 'mail');
  38.      // Add the rid=3 for all users. TODO: not working. Asked fago at http://drupal.org/node/705370.
  39. -    $this->addFieldMapping(array('callback' => 'prepare_fixed', 'arguments' => array(2, 3)), 'roles');
  40. +    $this->addFieldMappingCallback('roles', 'prepare_fixed', array(2, 3));
  41.    }
  42.  }
  43.  
  44. @@ -%ld,%ld +%ld,%ld @@
  45.      $this->addFieldMapping('description', 'description');
  46.      // This tells us the name of the map table. Perhaps there is a better way.
  47.      $this->ensureTables();
  48. -    $this->addFieldMapping(array('callback' => 'prepare_parent_term', 'arguments' => array('map_table_name' => $this->map_table_name)), 'parent');
  49. +    $this->addFieldMappingCallback('parent', 'prepare_parent_term', array('map_table_name' => $this->map_table_name));
  50.    }
  51.  }
  52.  
  53.  class MigrateDestinationEntityBeer extends MigrateDestinationEntity {
  54. -  // A fieldMapping callback for term creation. Get tid of the parent_name.
  55. +  // A fieldMappingCallback for term creation. Get tid of the parent_name.
  56.    function prepare_parent_term($row, $arguments) {
  57.      if (!empty($row->parent_name)) {
  58.        return db_query("SELECT destid FROM :map_table WHERE sourceid = :sourceid", array(':map_table' => $arguments['map_table_name'], ':sourceid' => $row->parent_name))->fetchField();
  59. Index: includes/migration.inc
  60. ===================================================================
  61. --- includes/migration.inc      (revision 135)
  62. +++ includes/migration.inc      (working copy)
  63. @@ -%ld,%ld +%ld,%ld @@
  64.    public $name, $description, $source, $destination;
  65.    public $map_table_name, $message_table_name;
  66.  
  67. -  protected $fieldMappings = array(), $defaultValues = array();
  68. +  protected $fieldMappings = array(), $fieldMappingsCallback = array(), $defaultValues = array();
  69.  
  70.    protected $currSourceKeys = array();
  71.    protected $sourceKeyMap, $destinationKeyMap;
  72. @@ -%ld,%ld +%ld,%ld @@
  73.    ////////////////////////////////////////////////////////////////////
  74.    // Processing
  75.  
  76. -  // Specify field mapping and their prepare callbacks if needed.
  77. +  // Specify simple field mapping.
  78.    public function addFieldMapping($sourceField, $destinationField, $defaultValue = NULL) {
  79.      $this->fieldMappings[$destinationField] = $sourceField;
  80.      $this->defaultValues[$destinationField] = $defaultValue;
  81.    }
  82.  
  83. +  // Specify field mapping and its prepare callback.
  84. +  public function addFieldMappingCallback($destinationField, $callback, $arguments, $defaultValue = NULL) {
  85. +    $this->fieldMappingsCallback[$destinationField] = array($callback, $arguments);
  86. +    $this->defaultValues[$destinationField] = $defaultValue;
  87. +  }
  88. +
  89.    public function rollback() {
  90.      $this->ensureTables();
  91.      // TODO: Support multi-field keys
  92. @@ -%ld,%ld +%ld,%ld @@
  93.      $this->ensureTables();
  94.      while ($data_row = $this->source->fetch($this)) {
  95.        // TODO: Check memory
  96. +      // Simple mapping.
  97.        foreach ($this->fieldMappings as $destination => $source) {
  98. -        if (is_array($source)) {
  99. -          // Invoke a FieldMapping callback.
  100. -          $values[$destination] = $this->destination->$source['callback']($data_row, @$source['arguments'], $destination);
  101. -        }
  102. -        elseif (isset($data_row->$source) && $data_row->$source) {
  103. -          // Simple mapping.
  104. +        if (isset($data_row->$source) && $data_row->$source) {
  105.            $values[$destination] = $data_row->$source;
  106.          }
  107.          elseif (isset($this->defaultValues[$destination])) {
  108. @@ -%ld,%ld +%ld,%ld @@
  109.          }
  110.        }
  111.  
  112. +      // These mapping run through a prepare function.
  113. +      foreach ($this->fieldMappingsCallback as $destination => $pair) {
  114. +        // $pair is an array like: array($callback, $arguments);
  115. +        $values[$destination] = $this->destination->$pair[0]($data_row, $pair[1], $destination);
  116. +      }
  117. +
  118.        // Track the current source key
  119.  
  120.        // If there's a destination ID, the intent is to update an existing object,

Submit Fix

Any tags you'd like to associate with your code, delimitered by commas (example: Views, CCK, Module, etc).
Select the syntax highlighting mode to use.