Commit 4049eef2 authored by Reena Raghavan's avatar Reena Raghavan

CSV Import added

parent 73b3be1b
This diff is collapsed.
id: article_csv_import
label: Import articles
migration_tags:
- CSV
migration_group: my_custom_migrate
source:
plugin: csv
path: public://import-sources/article.csv
ids:
[nid]
delimiter: ','
enclosure: '"'
header_row_count: 2
keys:
- nid
process:
langcode: langcode
type: type
status: status
uid: uid
title: title
created: created
changed: changed
default_langcode: default_langcode
path: path
body: body
field_image: field_image
field_tags: field_tags
destination:
plugin: entity:node
id: files
label: Migrate Files
migration_group: my_custom_migrate
source_type: Database
source:
plugin: files
key: migrate
destination:
plugin: 'entity:file'
source_base_path: https://drupal9-old.lndo.site
source_path_property: uri
urlencode: true
destination_path_property: uri
process:
filename: filename
uri: uri
filemime: filemime
status: status
created: timestamp
changed: timestamp
uid: uid
alt: alt
migration_dependencies:
required: {}
dependencies:
enforced:
module: {}
\ No newline at end of file
id: portfolio_csv_import
label: Import Portfolio
migration_tags:
- CSV
migration_group: my_custom_migrate
source:
plugin: csv
path: public://import-sources/portfolio.csv
ids:
[nid]
delimiter: ','
enclosure: '"'
header_row_count: 2
keys:
- nid
process:
langcode: langcode
type: type
status: status
uid: uid
title: title
created: created
changed: changed
default_langcode: default_langcode
metatag: metatag
path: path
menu_link: menu_link
content_translation_source: content_translation_source
content_translation_outdated: content_translation_outdated
field_aply_type: field_aply_type
field_author_simple: field_author_simple
field_background_foto: field_background_foto
field_broadcast_article_ref: field_broadcast_article_ref
field_content: field_content
field_country: field_country
field_co_founders_simple: field_co_founders_simple
field_founding_amount: field_founding_amount
field_founding_status: field_founding_status
field_founding_year: field_founding_year
field_github_legend: field_github_legend
field_github_url: field_github_url
field_graduated: field_graduated
field_localization: field_localization
field_organization: field_organization
field_theme: field_theme
field_thumbnail: field_thumbnail
destination:
plugin: entity:node
id: users
label: Migrate Users
migration_group: my_custom_migrate
source_type: Database
source:
plugin: users
key: migrate
destination:
plugin: entity:user
process:
uid: uid
name: name
pass: pass
mail: mail
created: created
status: status
field_first_name: field_first_name
field_last_name: field_last_name
field_job_role: field_job_role
field_mailing_address: field_mailing_address
migration_dependencies:
required: {}
dependencies:
enforced:
module: {}
\ No newline at end of file
<?php
namespace Drupal\uvf_migrate\Plugin\migrate\process;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\MigrateSkipRowException;
use Drupal\migrate\ProcessPluginBase;
use Drupal\migrate\Row;
use GuzzleHttp\Client;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Checks to see if a file exists.
*
* @MigrateProcessPlugin(
* id = "file_exists"
* )
*/
class FileExists extends ProcessPluginBase implements
ContainerFactoryPluginInterface
{
/**
* The Guzzle HTTP Client service.
*
* @var \GuzzleHttp\Client
*/
protected $httpClient;
/**
* Constructs a download process plugin.
*
* @param array $configuration
* The plugin configuration.
* @param string $plugin_id
* The plugin ID.
* @param mixed $plugin_definition
* The plugin definition.
* @param \GuzzleHttp\Client $http_client
* The HTTP client.
*/
public function __construct(
array $configuration,
$plugin_id,
$plugin_definition,
Client $http_client
) {
$configuration += [
'guzzle_options' => [],
];
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->httpClient = $http_client;
}
/**
* {@inheritdoc}
*/
public static function create(
ContainerInterface $container,
array $configuration,
$plugin_id,
$plugin_definition
) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('http_client')
);
}
/**
* {@inheritdoc}
*/
public function transform(
$value,
MigrateExecutableInterface $migrate_executable,
Row $row,
$destination_property
) {
// Verify that the image exists.
try {
$this->httpClient->get(
$value,
$this->configuration['guzzle_options']
);
} catch (\Exception $e) {
// Guzzle throws an exception for anything but 200.
throw new MigrateSkipRowException(
'Skipping file: "' . $value . '".'
);
}
return $value;
}
}
<?php
namespace Drupal\uvf_migrate\Plugin\migrate\source;
use Drupal\migrate\Plugin\migrate\source\SqlBase;
use Drupal\migrate\Row;
/**
* Source plugin for Files (images, docs).
*
* @MigrateSource(
* id = "files"
* )
*/
class Files extends SqlBase
{
/**
* {@inheritdoc}
*/
public function query()
{
$file_types = [
'image/jpeg',
'image/png',
'image/gif',
'text/plain',
'application/pdf',
'application/msword',
'application/vnd.ms-excel',
'application/vnd.ms-powerpoint',
'application/vnd.download',
'application/octet-stream',
];
return $this->select('file_managed')
->fields('file_managed', array_keys($this->fields()))
// Ignore unpublished files.
->condition('status', '1', '=')
// Only interested in image files.
->condition('filemime', $file_types, 'IN');
}
/**
* {@inheritdoc}
*/
public function fields()
{
$fields = [
'fid' => $this->t('File ID'),
'uid' => $this->t('User ID'),
'filename' => $this->t('File name'),
'uri' => $this->t('File URI'),
'filemime' => $this->t('File MIME type'),
'created' => $this->t('File created date UNIX timestamp'),
];
return $fields;
}
/**
* {@inheritdoc}
*/
public function getIds()
{
return [
'fid' => [
'type' => 'integer',
],
];
}
/**
* {@inheritdoc}
*/
public function prepareRow(Row $row)
{
// Set the destination uri.
$row->setSourceProperty(
'destination_uri',
sprintf('public://%s', $row->getSourceProperty('filename'))
);
// Update filepath to remove public:// directory portion.
$original_path = $row->getSourceProperty('uri');
$new_path = str_replace(
'public://',
'https://drupal9-new.lndo.site/sites/default/files/',
$original_path
);
$row->setSourceProperty('filepath', $new_path);
return parent::prepareRow($row);
}
}
<?php
namespace Drupal\uvf_migrate\Plugin\migrate\source;
use Drupal\migrate\Row;
use Drupal\migrate\Plugin\migrate\source\SqlBase;
/**
* Drupal 9 user source from database.
*
* @MigrateSource(
* id = "users",
* source_module = "user"
* )
*/
class Users extends SqlBase
{
/**
* {@inheritdoc}
*/
public function query()
{
// return $this->select('users_field_data', 'u')
// ->fields('u')
// ->condition('u.uid', 0, '>');
$query = $this->select('users_field_data', 'u');
$query->fields('u', ['uid','name','pass','mail','created','status',]);
$query->condition('u.uid', 0, '>');
return $query;
}
/**
* {@inheritdoc}
*/
public function fields()
{
$fields = $this->baseFields();
$fields['field_first_name'] = $this->t('First Name');
$fields['field_last_name'] = $this->t('First Name');
$fields['field_job_role'] = $this->t('First Name');
$fields['field_mailing_address'] = $this->t('First Name');
return $fields;
}
/**
* {@inheritdoc}
*/
public function prepareRow(Row $row)
{
// $roles = $this->select('users_roles', 'ur')
// ->fields('ur', ['rid'])
// ->condition('ur.uid', $row->getSourceProperty('uid'))
// ->execute()
// ->fetchCol();
// $row->setSourceProperty('roles', $roles);
// $row->setSourceProperty(
// 'data',
// unserialize($row->getSourceProperty('data'))
// );
$uid = $row->getSourceProperty('uid');
$row->setSourceProperty('uid', $uid);
return parent::prepareRow($row);
}
/**
* {@inheritdoc}
*/
public function getIds()
{
return [
'uid' => [
'type' => 'integer',
'alias' => 'u',
],
];
}
/**
* {@inheritdoc}
*/
protected function baseFields()
{
$fields = array(
'uid' => $this->t('User ID'),
'name' => $this->t('Username'),
'pass' => $this->t('Password'),
'mail' => $this->t('Email'),
'created' => $this->t('Created On'),
'status' => $this->t('Status')
);
return $fields;
}
}
......@@ -12,7 +12,11 @@ function uvf_migrate_uninstall() {
$configs = [
'migrate_plus.migration.article',
'migrate_plus.migration.custom_taxonomy_term',
'migrate_plus.migration.custom_taxonomy_vocabulary'
'migrate_plus.migration.custom_taxonomy_vocabulary',
'migrate_plus.migration.files.yml',
'migrate_plus.migration.users.yml',
'migrate_plus.migration.article_csv_import.yml',
'migrate_plus.migration.portfolio_csv_import.yml'
];
foreach($configs as $config){
$delete = \Drupal::database()->delete('config')
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment