3 namespace Drupal\migrate_plus\Plugin\migrate\destination;
5 use Drupal\Core\Database\Connection;
6 use Drupal\Core\Database\Database;
7 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
8 use Drupal\migrate\MigrateException;
9 use Drupal\migrate\MigrateSkipProcessException;
10 use Drupal\migrate\Plugin\MigrationInterface;
11 use Drupal\migrate\Plugin\migrate\destination\DestinationBase;
12 use Drupal\migrate\Row;
13 use Symfony\Component\DependencyInjection\ContainerInterface;
16 * Provides table destination plugin.
18 * Use this plugin for a table not registered with Drupal Schema API.
20 * @MigrateDestination(
24 class Table extends DestinationBase implements ContainerFactoryPluginInterface {
27 * The name of the destination table.
34 * IDMap compatible array of id fields.
41 * Array of fields present on the destination table.
48 * The database connection.
50 * @var \Drupal\Core\Database\Connection
52 protected $dbConnection;
54 public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, Connection $connection) {
55 parent::__construct($configuration, $plugin_id, $plugin_definition, $migration);
56 $this->dbConnection = $connection;
57 $this->tableName = $configuration['table_name'];
58 $this->idFields = $configuration['id_fields'];
59 $this->fields = isset($configuration['fields']) ? $configuration['fields'] : [];
60 $this->supportsRollback = TRUE;
66 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
67 $db_key = !empty($configuration['database_key']) ? $configuration['database_key'] : NULL;
74 Database::getConnection('default', $db_key)
81 public function getIds() {
82 if (empty($this->idFields)) {
83 throw new MigrateException('Id fields are required for a table destination');
85 return $this->idFields;
91 public function fields(MigrationInterface $migration = NULL) {
98 public function import(Row $row, array $old_destination_id_values = []) {
99 $id = $row->getSourceIdValues();
100 if (count($id) != count($this->idFields)) {
101 throw new MigrateSkipProcessException('All the id fields are required for a table migration.');
104 $values = $row->getDestination();
107 $values = array_intersect_key($values, $this->fields);
110 $status = $this->dbConnection->merge($this->tableName)
115 return $status ? $id : NULL;
121 public function rollback(array $destination_identifier) {
122 $delete = $this->dbConnection->delete($this->tableName);
123 foreach ($destination_identifier as $field => $value) {
124 $delete->condition($field, $value);