Skip to content

Instantly share code, notes, and snippets.

@mikeryan776
Created January 12, 2017 23:07
Show Gist options
  • Save mikeryan776/276e376be7cf57b6caca89a7a2e45d5c to your computer and use it in GitHub Desktop.
Save mikeryan776/276e376be7cf57b6caca89a7a2e45d5c to your computer and use it in GitHub Desktop.
<?php
namespace Drupal\foo_migrate\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\migrate_plus\Entity\MigrationGroup;
/**
* Interactive configuration of the Foo migration process.
*/
class MigrationConfigurationForm extends FormBase {
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'foo_migrate_migration_configuration_form';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$form['#tree'] = TRUE;
$group = MigrationGroup::load('food7');
$shared_configuration = $group->get('shared_configuration');
if (isset($shared_configuration['source']['database'])) {
$database = $shared_configuration['source']['database'];
}
else {
$database = [
'database' => NULL,
'username' => NULL,
'host' => NULL,
'port' => 3306,
];
}
$form['database']['database'] = [
'#type' => 'textfield',
'#title' => $this->t('Database'),
'#default_value' => $database['database'],
];
$form['database']['username'] = [
'#type' => 'textfield',
'#title' => $this->t('Username'),
'#default_value' => $database['username'],
];
$form['database']['password'] = [
'#type' => 'password',
'#title' => $this->t('Password'),
];
$form['database']['host'] = [
'#type' => 'textfield',
'#title' => $this->t('Host'),
'#default_value' => $database['host'],
];
$form['database']['port'] = [
'#type' => 'textfield',
'#title' => $this->t('Port'),
'#default_value' => $database['port'],
];
$form['actions'] = ['#type' => 'actions'];
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this->t('Submit'),
'#button_type' => 'primary',
];
return $form;
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$group = MigrationGroup::load('food7');
$shared_configuration = $group->get('shared_configuration');
$shared_configuration['source']['key'] = 'foo7';
$shared_configuration['source']['database'] = $form_state->getValue('database');
$shared_configuration['source']['database']['driver'] = 'mysql';
$shared_configuration['source']['database']['namespace'] = 'Drupal\\Core\\Database\\Driver\\mysql';
$shared_configuration['source']['database']['prefix'] = '';
$group->set('shared_configuration', $shared_configuration);
$group->save();
drupal_set_message($this->t('Database configuration saved.'));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment