class MigrateSourceUiForm in Migrate source UI 8
Contribute form.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\migrate_source_ui\Form\MigrateSourceUiForm
Expanded class hierarchy of MigrateSourceUiForm
1 string reference to 'MigrateSourceUiForm'
File
- src/
Form/ MigrateSourceUiForm.php, line 22
Namespace
Drupal\migrate_source_ui\FormView source
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),
]);
}
}
$form['migrations'] = [
'#type' => 'select',
'#title' => $this
->t('Migrations'),
'#options' => $options,
];
$form['source_file'] = [
'#type' => 'file',
'#title' => $this
->t('Upload the source file'),
];
$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());
}
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,
]));
}
$options = [
'file_path' => $form_state
->getValue('file_path'),
];
// Force updates or not.
if ($form_state
->getValue('update_existing_records')) {
$options['update'] = TRUE;
}
$executable = new MigrateBatchExecutable($migration, new StubMigrationMessage(), $options);
$executable
->batchImport();
}
/**
* 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;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
FormBase:: |
protected | property | The config factory. | 1 |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Retrieves a configuration object. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 1 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
protected | function | Gets the current user. | |
FormBase:: |
protected | function | Gets the request object. | |
FormBase:: |
protected | function | Gets the route match. | |
FormBase:: |
protected | function | Gets the logger for a specific channel. | |
FormBase:: |
protected | function |
Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: |
|
FormBase:: |
public | function | Resets the configuration factory. | |
FormBase:: |
public | function | Sets the config factory for this form. | |
FormBase:: |
public | function | Sets the request stack object to use. | |
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
MigrateSourceUiForm:: |
protected | property | Config object for migrate_source_ui.settings. | |
MigrateSourceUiForm:: |
protected | property | The migration definitions. | |
MigrateSourceUiForm:: |
protected | property | ||
MigrateSourceUiForm:: |
protected | property | The migration plugin manager. | |
MigrateSourceUiForm:: |
public | function |
Form constructor. Overrides FormInterface:: |
|
MigrateSourceUiForm:: |
public static | function |
Instantiates a new instance of this class. Overrides FormBase:: |
|
MigrateSourceUiForm:: |
public | function | The allowed file extension for the migration. | |
MigrateSourceUiForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
MigrateSourceUiForm:: |
public | function |
Form submission handler. Overrides FormInterface:: |
|
MigrateSourceUiForm:: |
public | function |
Form validation handler. Overrides FormBase:: |
|
MigrateSourceUiForm:: |
public | function | MigrateSourceUiForm constructor. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. |