clean install

This commit is contained in:
2024-12-10 15:08:16 +01:00
commit e14eb2d8fd
31193 changed files with 3555714 additions and 0 deletions

View File

@@ -0,0 +1,62 @@
<?php
namespace Drupal\migrate_source_ui\Form;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
/**
* Settings form class.
*/
class MigrateSourceUiAdminSettingsForm extends ConfigFormBase {
const CONFIG_NAME = 'migrate_source_ui.settings';
/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
return [self::CONFIG_NAME];
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'migrate_source_ui_admin_settings_form';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this->config(self::CONFIG_NAME);
$form['file_temp_directory'] = [
'#type' => 'textfield',
'#title' => $this->t('Temporary file directory'),
'#description' => $this->t("Directory where the uploaded source
file will live temporarily. Should follow the full Drupal stream syntax,
e.g.: <code>private://tmp</code>. The module will use Drupal's default
<code>temporary://</code> stream if this is not set."),
'#default_value' => $config->get('file_temp_directory'),
];
return parent::buildForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$config = $this->config(self::CONFIG_NAME);
$values = $form_state->cleanValues()->getValues();
foreach ($values as $key => $value) {
$config->set($key, $value);
}
$config->save();
parent::submitForm($form, $form_state);
}
}

View File

@@ -0,0 +1,230 @@
<?php
namespace Drupal\migrate_source_ui\Form;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\migrate\Plugin\Migration;
use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\migrate\Plugin\MigrationPluginManager;
use Drupal\migrate_plus\Plugin\migrate_plus\data_parser\Json;
use Drupal\migrate_plus\Plugin\migrate_plus\data_parser\Xml;
use Drupal\migrate_source_csv\Plugin\migrate\source\CSV;
use Drupal\migrate_source_ui\StubMigrationMessage;
use Drupal\migrate_tools\MigrateBatchExecutable;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Contribute form.
*/
class MigrateSourceUiForm extends FormBase {
/**
* The migration plugin manager.
*
* @var \Drupal\migrate\Plugin\MigrationPluginManager
*/
protected $pluginManagerMigration;
/**
* The migration definitions.
*
* @var array
*/
protected $definitions;
/**
* Config object for migrate_source_ui.settings.
*
* @var \Drupal\Core\Config\ImmutableConfig
*/
protected $config;
/**
* @var FileSystemInterface
*/
protected $fileSystem;
/**
* MigrateSourceUiForm constructor.
*
* @param \Drupal\migrate\Plugin\MigrationPluginManager $plugin_manager_migration
* The migration plugin manager.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The config factory service.
* @param \Drupal\Core\File\FileSystemInterface $file_system
* The File System service.
*/
public function __construct(MigrationPluginManager $plugin_manager_migration, ConfigFactoryInterface $config_factory, FileSystemInterface $file_system) {
$this->pluginManagerMigration = $plugin_manager_migration;
$this->definitions = $this->pluginManagerMigration->getDefinitions();
$this->config = $config_factory->get('migrate_source_ui.settings');
$this->fileSystem = $file_system;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('plugin.manager.migration'),
$container->get('config.factory'),
$container->get('file_system')
);
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'migrate_source_ui_form';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$options = [];
foreach ($this->definitions as $definition) {
$migrationInstance = $this->pluginManagerMigration->createStubMigration($definition);
if ($migrationInstance->getSourcePlugin() instanceof CSV || $migrationInstance->getSourcePlugin() instanceof Json || $migrationInstance->getSourcePlugin() instanceof Xml) {
$id = $definition['id'];
$options[$id] = $this->t('%id (supports %file_type)', [
'%id' => $definition['label'] ?? $id,
'%file_type' => $this->getFileExtensionSupported($migrationInstance),
]);
}
}
natcasesort($options);
$form['migrations'] = [
'#type' => 'select',
'#title' => $this->t('Migrations'),
'#options' => $options,
];
$form['source_file'] = [
'#type' => 'file',
'#title' => $this->t('Upload the source file'),
'#upload_validators' => [
'file_validate_extensions' => [
'json csv xml',
],
],
];
$form['update_existing_records'] = [
'#type' => 'checkbox',
'#title' => $this->t('Update existing records'),
'#default_value' => 1,
];
$form['import'] = [
'#type' => 'submit',
'#value' => $this->t('Migrate'),
];
return $form;
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
parent::validateForm($form, $form_state);
$migration_id = $form_state->getValue('migrations');
$definition = $this->pluginManagerMigration->getDefinition($migration_id);
$migrationInstance = $this->pluginManagerMigration->createStubMigration($definition);
$extension = $this->getFileExtensionSupported($migrationInstance);
$validators = ['file_validate_extensions' => [$extension]];
// Check to see if a specific file temp directory is configured. If not,
// default the value to FALSE, which will instruct file_save_upload() to
// use Drupal's temporary files scheme.
$file_destination = $this->config->get('file_temp_directory');
if (is_null($file_destination)) {
$file_destination = FALSE;
}
$directory = $this->fileSystem->realpath($file_destination);
$this->fileSystem->prepareDirectory($directory, FileSystemInterface::CREATE_DIRECTORY);
$file = file_save_upload('source_file', $validators, $file_destination, 0, FileSystemInterface::EXISTS_REPLACE);
if (isset($file)) {
// File upload was attempted.
if ($file) {
$form_state->setValue('file_path', $file->getFileUri());
}
// File upload failed.
else {
$form_state->setErrorByName('source_file', $this->t('The file could not be uploaded.'));
}
}
else {
$form_state->setErrorByName('source_file', $this->t('You have to upload a source file.'));
}
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$migration_id = $form_state->getValue('migrations');
/** @var \Drupal\migrate\Plugin\Migration $migration */
$migration = $this->pluginManagerMigration->createInstance($migration_id);
// Reset status.
$status = $migration->getStatus();
if ($status !== MigrationInterface::STATUS_IDLE) {
$migration->setStatus(MigrationInterface::STATUS_IDLE);
$this->messenger()->addWarning($this->t('Migration @id reset to Idle', ['@id' => $migration_id]));
}
$executable = new MigrateBatchExecutable($migration, new StubMigrationMessage(), $this->getBatchOptions($form, $form_state));
$executable->batchImport();
}
/**
* Prepares an array of migrate batch options.
*/
protected function getBatchOptions($form, FormStateInterface $form_state) {
$options = [
'configuration' => [
'source' => [
'path' => $form_state->getValue('file_path'),
],
],
];
// Force updates or not.
if ($form_state->getValue('update_existing_records')) {
$options['update'] = 1;
}
return $options;
}
/**
* The allowed file extension for the migration.
*
* @param \Drupal\migrate\Plugin\Migration $migrationInstance
* The migration instance.
*
* @return string
* The file extension.
*/
public function getFileExtensionSupported(Migration $migrationInstance) {
$extension = 'csv';
if ($migrationInstance->getSourcePlugin() instanceof CSV) {
$extension = 'csv';
}
elseif ($migrationInstance->getSourcePlugin() instanceof Json) {
$extension = 'json';
}
elseif ($migrationInstance->getSourcePlugin() instanceof Xml) {
$extension = 'xml';
}
return $extension;
}
}

View File

@@ -0,0 +1,28 @@
<?php
namespace Drupal\migrate_source_ui;
use Drupal\Core\Messenger\MessengerTrait;
use Drupal\migrate\MigrateMessageInterface;
/**
* A stub migrate message.
*/
class StubMigrationMessage implements MigrateMessageInterface {
use MessengerTrait;
/**
* Output a message from the migration.
*
* @param string $message
* The message to display.
* @param string $type
* The type of message to display.
*
* @see drupal_set_message()
*/
public function display($message, $type = 'status') {
$this->messenger()->addMessage($message, $type);
}
}