Skip to content

Instantly share code, notes, and snippets.

@chriscalip
Created March 2, 2018 03:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chriscalip/b24fc4dbecee1aa48a9ea7b8a87bab5d to your computer and use it in GitHub Desktop.
Save chriscalip/b24fc4dbecee1aa48a9ea7b8a87bab5d to your computer and use it in GitHub Desktop.
<?php
namespace Drupal\capacitype_move2aa\Plugin\migrate\destination;
use Drupal\Core\Database\Connection;
use Drupal\Core\Database\Database;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\migrate\MigrateException;
use Drupal\migrate\MigrateSkipProcessException;
use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\migrate\Plugin\migrate\destination\DestinationBase;
use Drupal\migrate\Row;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides table destination plugin.
*
* Use this plugin for a table not registered with Drupal Schema API.
*
* @MigrateDestination(
* id = "c5table"
* )
*/
class C5table extends DestinationBase implements ContainerFactoryPluginInterface {
/**
* The name of the destination table.
*
* @var string
*/
protected $tableName;
/**
* IDMap compatible array of id fields.
*
* @var array
*/
protected $idFields;
/**
* Array of fields present on the destination table.
*
* @var array
*/
protected $fields;
/**
* Represents a map of destination keys to source id keys.
* eg. destination key is id, source key is nid : $mapping_of_keys['id'] = 'nid';
*
* @var array
*/
protected $idFieldsMap;
/**
* The database connection.
*
* @var \Drupal\Core\Database\Connection
*/
protected $dbConnection;
public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, Connection $connection) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $migration);
$this->dbConnection = $connection;
$this->tableName = $configuration['table_name'];
$this->idFields = $configuration['id_fields'];
$this->fields = isset($configuration['fields']) ? $configuration['fields'] : [];
$this->idFieldsMap = isset($configuration['id_fields_map']) ? $configuration['id_fields_map'] : [];
$this->supportsRollback = TRUE;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
$db_key = !empty($configuration['database_key']) ? $configuration['database_key'] : NULL;
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$migration,
Database::getConnection('default', $db_key)
);
}
/**
* {@inheritdoc}
*/
public function getIds() {
if (empty($this->idFields)) {
throw new MigrateException('Id fields are required for a table destination');
}
return $this->idFields;
}
/**
* {@inheritdoc}
*/
public function fields(MigrationInterface $migration = NULL) {
return $this->fields;
}
/**
* {@inheritdoc}
*
* NOTE: This assumes only one sourceid1 key and only one destid1
* @TODO more rigorous algorithm.
*/
public function import(Row $row, array $old_destination_id_values = []) {
$source_id_values = $row->getSourceIdValues();
$primary_source_id = array_keys($source_id_values)[0];
$primary_source_id_value = array_values($source_id_values)[0];
$destination_id_values = [];
if (count($source_id_values) != count($this->idFields)) {
throw new MigrateSkipProcessException('All the id fields are required for a table migration.');
}
$values = $row->getDestination();
$migrate_map_table = 'migrate_map_' . $this->migration->id();
if ($this->fields) {
$values = array_intersect_key($values, $this->fields);
}
$is_new = (empty($old_destination_id_values)) ? TRUE: FALSE;
if ($is_new == FALSE) {
$destination_id_value = db_query("SELECT destid1 FROM {$migrate_map_table} WHERE sourceid1 = :sid1", [':sid1' => $primary_source_id_value])->fetchField();
}
if (empty($destination_id_value)) {
$is_new = TRUE;
}
$feedback = NULL;
if ($is_new) {
$last_insert_id = $this->dbConnection->insert($this->tableName)->fields($values)->execute();
$feedback[$primary_source_id] = $last_insert_id;
}
else {
if ($this->idFieldsMap) {
$primary_destination_id = $this->idFieldsMap[$primary_source_id];
$destination_id_value = db_query("SELECT destid1 FROM {$migrate_map_table} WHERE sourceid1 = :sid1", [':sid1' => $primary_source_id_value])->fetchField();
$destination_id_values[$primary_destination_id] = $destination_id_value;
$feedback[$primary_source_id] = $destination_id_value;
$status = $this->dbConnection->merge($this->tableName)
->key($destination_id_values)
->fields($values)
->execute();
}
else {
throw new MigrateSkipProcessException('Unable to find map between keys of source ids and destination ids.');
}
}
return $feedback;
}
/**
* {@inheritdoc}
*/
public function rollback(array $destination_identifier) {
$delete = $this->dbConnection->delete($this->tableName);
foreach ($destination_identifier as $field => $value) {
$delete->condition($field, $value);
}
$delete->execute();
}
}
id: group2model
label: "Asset Nodes to asset model."
migration_group: d82tables
dependencies:
enforced:
module:
- capacitype_move2aa
langcode: en
status: true
source:
plugin: c5ontent_entity:taxonomy_term
bundle: group
include_translations: false
process:
name: 'name/0/value'
description: 'description/0/value'
created_at:
plugin: c5_timestamp_to_datetime
source: 'changed/0/value'
updated_at:
plugin: c5_timestamp_to_datetime
source: 'changed/0/value'
destination:
plugin: c5table
table_name: groups
database_key: aa
id_fields:
id:
type: integer
id_fields_map:
tid: id
fields:
id: 'id'
name: 'name'
description: 'description'
alias: alias
url: url
created_at: created_at
updated_at: updated_at
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment