You are here

import.install in Import 8

Same filename and directory in other branches
  1. 6 import.install

Set up the import base migrations.

File

import.install
View source
<?php

/**
 * @file
 * Set up the import base migrations.
 */
use Drupal\migrate\Entity\MigrationInterface;
use Drupal\migrate\Entity\Migration;

/**
 * Implements hook_install().
 */
function import_install() {

  // Set up base path.
  $path = dirname(__FILE__) . '/data/';

  // Create imports using the node base migration.
  $bundles = [
    'article',
    'page',
  ];

  // Load the base migration for nodes.
  $base_migration = Migration::load('import_node_base');
  foreach ($bundles as $type) {

    // Duplicate the node base creating a new migration.
    $migration = $base_migration
      ->createDuplicate();
    $migration
      ->set('label', 'Import node:' . $type);
    $migration
      ->set('id', 'import_node_' . $type);

    // Point source path to local CSV file.
    $source = $migration
      ->get('source');
    $source['path'] = $path . 'import.node.' . $type . '.csv';

    // Set content type to current bundle.
    $process = $migration
      ->get('process');
    $process['type']['default_value'] = $type;

    // Add processing for Article field_tags.
    if ($type == 'article') {
      $source['plugin'] = 'article_node';
      $source['csvColumns'][2] = 'Image';
      $process['field_image'] = 'Image';
      $source['csvColumns'][3] = 'Tags';
      $process['field_tags'] = [
        'plugin' => 'migration',
        'migration' => 'import_term_tags',
        'source' => 'Tags',
      ];
      $dependencies = [
        'required' => [
          'import_term_tags',
        ],
      ];
      $migration
        ->set('migration_dependencies', $dependencies);
    }

    // Set body fields to full_html.
    $process['body/format'] = [
      'plugin' => 'default_value',
      'default_value' => 'full_html',
    ];

    // Set new source and process modifications.
    $migration
      ->set('source', $source);
    $migration
      ->set('process', $process);

    // Save the new migration.
    $migration
      ->save();
  }

  // Add a 'Tags' term import using the taxonomy_term base migration.
  $type = 'tags';
  $base_migration = Migration::load('import_term_base');
  $migration = $base_migration
    ->createDuplicate();
  $migration
    ->set('label', 'Import term:' . $type);
  $migration
    ->set('id', 'import_term_' . $type);

  // Point source path to local CSV file.
  $source = $migration
    ->get('source');
  $source['path'] = $path . 'import.term.' . $type . '.csv';
  $migration
    ->set('source', $source);

  // Set correct taxonomy term vocabulary id.
  $process = $migration
    ->get('process');
  $process['vid']['default_value'] = $type;
  $migration
    ->set('process', $process);

  // Save the new migration.
  $migration
    ->save();

  // Add a 'Basic' block import using the block base migration.
  $type = 'basic';
  $base_migration = Migration::load('import_block_base');
  $migration = $base_migration
    ->createDuplicate();
  $migration
    ->set('label', 'Import block:' . $type);
  $migration
    ->set('id', 'import_block_' . $type);

  // Point source path to local CSV file.
  $source = $migration
    ->get('source');
  $source['path'] = $path . 'import.block.' . $type . '.csv';

  // Add body/value field.
  $process = $migration
    ->get('process');
  $source['csvColumns'][2] = 'Body';
  $process['body/value'] = 'Body';
  $process['body/format'] = [
    'plugin' => 'default_value',
    'default_value' => 'full_html',
  ];

  // Set new source and process modifications.
  $migration
    ->set('process', $process);
  $migration
    ->set('source', $source);

  // Save the new migration.
  $migration
    ->save();

  // Add an Image file migration using the base file migration
  $type = 'image';
  $base_migration = Migration::load('import_file_base');
  $migration = $base_migration
    ->createDuplicate();
  $migration
    ->set('label', 'Import file:' . $type);
  $migration
    ->set('id', 'import_file_' . $type);

  // Point source path to local CSV file.
  $source = $migration
    ->get('source');
  $source['path'] = $path . 'import.file.' . $type . '.csv';
  $source['plugin'] = 'image_file';
  $migration
    ->set('source', $source);

  // Save the new migration.
  $migration
    ->save();

  // Add a 'User' user import using the user base migration.
  $type = 'user';
  $base_migration = Migration::load('import_user_base');
  $migration = $base_migration
    ->createDuplicate();
  $migration
    ->set('label', 'Import user:' . $type);
  $migration
    ->set('id', 'import_user_' . $type);

  // Point source path to local CSV file.
  $source = $migration
    ->get('source');
  $source['path'] = $path . 'import.user.' . $type . '.csv';
  $migration
    ->set('source', $source);

  // Set user picture.
  $process = $migration
    ->get('process');
  $process['user_picture'] = [
    'plugin' => 'migration',
    'migration' => 'import_file_image',
    'source' => 'Picture',
  ];

  // Set a default access/login date to ~1-4 weeks ago.
  $time = strtotime("-" . rand(1, 4) . "week");
  $process['access'] = [
    'plugin' => 'default_value',
    'default_value' => $time,
  ];
  $process['login'] = [
    'plugin' => 'default_value',
    'default_value' => $time,
  ];
  $migration
    ->set('process', $process);

  // Set up proper dependencies for image files.
  $dependencies = [
    'import_file_image',
  ];
  $migration
    ->set('migration_dependencies', [
    'required' => $dependencies,
  ]);

  // Save the new migration.
  $migration
    ->save();

  // Add a 'Default comment' comment import using the comment base migration.
  $type = 'comment';
  $base_migration = Migration::load('import_comment_base');
  $migration = $base_migration
    ->createDuplicate();
  $migration
    ->set('label', 'Import comment:' . $type);
  $migration
    ->set('id', 'import_comment_' . $type);

  // Point source path to local CSV file.
  $source = $migration
    ->get('source');
  $source['path'] = $path . 'import.comment.' . $type . '.csv';
  $source['plugin'] = 'node_comment';
  $migration
    ->set('source', $source);
  $process = $migration
    ->get('process');
  $process['entity_id'] = [
    'plugin' => 'migration',
    'migration' => 'import_node_article',
    'source' => 'Content',
  ];
  $migration
    ->set('process', $process);

  // Set up proper dependencies for comments.
  $dependencies = [
    'import_node_article',
  ];
  $migration
    ->set('migration_dependencies', [
    'required' => $dependencies,
  ]);
  $migration
    ->save();
}

Functions

Namesort descending Description
import_install Implements hook_install().