weight.migrate.inc 1.16 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
<?php

/**
 * @file
 * Migrate support for weight.
 */

/**
 * Implements hook_migrate_api().
 */
function weight_migrate_api() {
  return array(
    'api' => 2,
    'destination handlers' => array(
      'WeightNodeHandler',
    ),
  );
}

class WeightNodeHandler extends MigrateDestinationHandler {
  public function __construct() {
    $this->registerTypes(array('node'));
  }

  public function fields($entity_type, $bundle) {
    $settings = _weight_get_settings($bundle);
    if (!empty($settings) && $settings['enabled']) {
      return array(
        'weight' => t('The node weight ranging between @min and @max.', array(
          '@max' => $settings['range'],
          '@min' => (int)$settings['range']*-1,
        )),
      );
    }
  }

  public function prepare($entity, stdClass $row) {
    $settings = _weight_get_settings($entity->type);
    if (!empty($settings) && $settings['enabled']) {
      // Use the mapped entity value to populate the real value
      if (isset($entity->weight)) {
        $entity->weight_weight = $entity->weight;
        unset($entity->weight);
      }
      else {
        $entity->weight_weight = $settings['default'];
      }
    }
  }
}