View source
<?php
namespace Drupal\agreement\Plugin\migrate\destination;
use Drupal\Core\Database\Connection;
use Drupal\Core\Database\Database;
use Drupal\Core\Database\DatabaseExceptionWrapper;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\migrate\MigrateSkipProcessException;
use Drupal\migrate\Plugin\migrate\destination\DestinationBase;
use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\migrate\Row;
use Symfony\Component\DependencyInjection\ContainerInterface;
class Agreement extends DestinationBase implements ContainerFactoryPluginInterface {
protected $connection;
public function __construct(array $configuration, $plugin_id, array $plugin_definition, MigrationInterface $migration, Connection $connection) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $migration);
$this->connection = $connection;
}
public function getIds() {
return [
'id' => [
'type' => 'integer',
],
];
}
public function fields(MigrationInterface $migration = NULL) {
return [
'id' => $this
->t('Unique Identifier'),
'type' => $this
->t('Agreement type name'),
'uid' => $this
->t('User Identifier'),
'sid' => $this
->t('Session Identifier'),
'agreed' => $this
->t('Agreed?'),
'agreed_date' => $this
->t('Agreement timestamp'),
];
}
public function import(Row $row, array $old_destination_id_values = []) {
$values = array_intersect_key($row
->getDestination(), $this
->fields());
try {
$status = $this->connection
->merge('agreement')
->key('id')
->fields($values)
->execute();
} catch (DatabaseExceptionWrapper $e) {
throw new MigrateSkipProcessException($e
->getMessage());
}
return $status ? [
$row
->getDestinationProperty('id'),
] : NULL;
}
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));
}
}