You are here

public function BeerNodeMigration::__construct in Migrate 7.2

Same name and namespace in other branches
  1. 6.2 migrate_example/beer.inc \BeerNodeMigration::__construct()

General initialization of a Migration object.

Overrides BasicExampleMigration::__construct

File

migrate_example/beer.inc, line 340
A basic example of using the Migrate module to import taxonomy, users, nodes, and comments.

Class

BeerNodeMigration
The BeerNodeMigration uses the migrate_example_beer_node table as source and creates Drupal nodes of type 'Beer' as destination.

Code

public function __construct($arguments) {
  parent::__construct($arguments);
  $this->description = t('Beers of the world');

  // We have a more complicated query. The Migration class fundamentally
  // depends on taking a single source row and turning it into a single
  // Drupal object, so how do we deal with zero or more terms attached to
  // each node? One way (valid for MySQL only) is to pull them into a single
  // comma-separated list.
  $query = db_select('migrate_example_beer_node', 'b')
    ->fields('b', array(
    'bid',
    'name',
    'body',
    'excerpt',
    'aid',
    'countries',
    'image',
    'image_alt',
    'image_title',
    'image_description',
  ));
  $query
    ->leftJoin('migrate_example_beer_topic_node', 'tb', 'b.bid = tb.bid');

  // Gives a single comma-separated list of related terms
  $query
    ->groupBy('tb.bid');
  $query
    ->addExpression('GROUP_CONCAT(tb.style)', 'terms');

  // By default, MigrateSourceSQL derives a count query from the main query -
  // but we can override it if we know a simpler way
  $count_query = db_select('migrate_example_beer_node', 'b');
  $count_query
    ->addExpression('COUNT(bid)', 'cnt');

  // Passing the cache_counts option means the source count (shown in
  // drush migrate-status) will be cached - this can be very handy when
  // dealing with a slow source database.
  $this->source = new MigrateSourceSQL($query, array(), $count_query, array(
    'cache_counts' => TRUE,
  ));

  // Set up our destination - nodes of type migrate_example_beer
  $this->destination = new MigrateDestinationNode('migrate_example_beer');
  $this->map = new MigrateSQLMap($this->machineName, array(
    'bid' => array(
      'type' => 'int',
      'not null' => TRUE,
      'description' => 'Beer ID.',
      'alias' => 'b',
    ),
  ), MigrateDestinationNode::getKeySchema());

  // Mapped fields
  $this
    ->addFieldMapping('title', 'name')
    ->description(t('Mapping beer name in source to node title'));
  $this
    ->addFieldMapping('sticky')
    ->description(t('Should we default this to 0 or 1?'))
    ->issueGroup(t('Client questions'))
    ->issueNumber(765736)
    ->issuePriority(MigrateFieldMapping::ISSUE_PRIORITY_LOW);

  // References to related objects (such as the author of the content) are
  // most likely going to be identifiers from the source data, not Drupal
  // identifiers (such as uids). You can use the mapping from the relevant
  // migration to translate from the old ID to the Drupal identifier.
  // Note that we also provide a default value of 1 - if the lookup fails to
  // find a corresponding uid for the aid, the owner will be the administrative
  // account.
  $this
    ->addFieldMapping('uid', 'aid')
    ->sourceMigration('BeerUser')
    ->defaultValue(1);

  // This is a multi-value text field - in the source data the values are
  // separated by |, so we tell migrate to split it by that character.
  $this
    ->addFieldMapping('field_migrate_example_country', 'countries')
    ->separator('|');

  // These are related terms, which by default will be looked up by name.
  $this
    ->addFieldMapping('migrate_example_beer_styles', 'terms')
    ->separator(',');

  // Some fields may have subfields such as text formats or summaries. These
  // can be individually mapped as we see here.
  $this
    ->addFieldMapping('body', 'body');
  $this
    ->addFieldMapping('body:summary', 'excerpt');

  // File fields are more complex - the file needs to be copied, a Drupal
  // file entity (file_managed table row) created, and the field populated.
  // There are several different options involved. It's usually best to do
  // migrate the files themselves in their own migration (see wine.inc for an
  // example), but they can also be brought over through the field mapping.
  // We map the filename (relative to the source_dir below) to the field
  // itself.
  $this
    ->addFieldMapping('field_migrate_example_image', 'image');

  // The file_class determines how the 'image' value is interpreted, and what
  // other options are available. In this case, MigrateFileUri indicates that
  // the 'image' value is a URI.
  $this
    ->addFieldMapping('field_migrate_example_image:file_class')
    ->defaultValue('MigrateFileUri');

  // Here we specify the directory containing the source files.
  $this
    ->addFieldMapping('field_migrate_example_image:source_dir')
    ->defaultValue(drupal_get_path('module', 'migrate_example'));

  // And we map the alt and title values in the database to those on the image.
  $this
    ->addFieldMapping('field_migrate_example_image:alt', 'image_alt');
  $this
    ->addFieldMapping('field_migrate_example_image:title', 'image_title');

  // No description for images, only alt and title
  $this
    ->addUnmigratedSources(array(
    'image_description',
  ));

  // Unmapped destination fields
  // Some conventions we use here: with a long list of fields to ignore, we
  // arrange them alphabetically, one distinct field per line (although
  // subfields of the same field may be grouped on the same line), and indent
  // subfields to distinguish them from top-level fields.
  $this
    ->addUnmigratedDestinations(array(
    'body:format',
    'changed',
    'comment',
    'created',
    'field_migrate_example_image:destination_dir',
    'field_migrate_example_image:destination_file',
    'field_migrate_example_image:file_replace',
    'field_migrate_example_image:preserve_files',
    'field_migrate_example_image:urlencode',
    'is_new',
    'language',
    'log',
    'migrate_example_beer_styles:source_type',
    'migrate_example_beer_styles:create_term',
    'promote',
    'revision',
    'revision_uid',
    'status',
    'tnid',
  ));
  $destination_fields = $this->destination
    ->fields();
  if (isset($destination_fields['path'])) {
    $this
      ->addFieldMapping('path')
      ->issueGroup(t('DNM'));
    if (isset($destination_fields['pathauto'])) {
      $this
        ->addFieldMapping('pathauto')
        ->issueGroup(t('DNM'));
    }
  }
  if (module_exists('statistics')) {
    $this
      ->addUnmigratedDestinations(array(
      'totalcount',
      'daycount',
      'timestamp',
    ));
  }
}