BackupDatabaseForm.php in Backup Database 8
File
src/Form/BackupDatabaseForm.php
View source
<?php
namespace Drupal\backup_db\Form;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Form\FormBase;
use Drupal\backup_db\Adapter\BackupDatabaseLocalAdapter;
use Drupal\backup_db\Adapter\BackupDatabaseRemoteAdapter;
use Drupal\backup_db\Adapter\BackupDatabaseS3Adapter;
class BackupDatabaseForm extends FormBase {
public function getFormId() {
return 'backup_db_export';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this
->config('backup_db.settings');
$site_name = \Drupal::config('system.site')
->get('name');
$form['filename'] = array(
'#type' => 'textfield',
'#title' => $this
->t('Filename'),
'#description' => $this
->t('The prefix name of the sql dump file.'),
'#default_value' => $config
->get('filename') ? $config
->get('filename') : $site_name,
);
$form['type'] = array(
'#type' => 'radios',
'#title' => $this
->t('Export type'),
'#options' => array(
'local' => $this
->t('Local'),
'download' => $this
->t('Download'),
),
'#description' => $this
->t('Export backup to local server or download.'),
'#default_value' => 'download',
);
$form['actions']['#type'] = 'actions';
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => $this
->t('Export'),
'#button_type' => 'primary',
);
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$values = $form_state
->getValues();
\Drupal::configFactory()
->getEditable('backup_db.settings')
->set('filename', $values['filename'])
->save();
$client = \Drupal::service('backup_db.client');
if ($values['type'] == 'download') {
$handler = new BackupDatabaseRemoteAdapter($client);
}
if ($values['type'] == 'local') {
$handler = new BackupDatabaseLocalAdapter($client);
}
if ($handler
->export()) {
drupal_set_message(t('Backup has been successfully completed.'), 'status');
}
else {
drupal_set_message(t('Backup has failed, please review recent log messages.'), 'warning');
}
}
}