View source
<?php
namespace Drupal\backup_migrate\Form;
use BackupMigrate\Drupal\Config\DrupalConfigHelper;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
class BackupMigrateAdvancedBackupForm extends FormBase {
public function getFormId() {
return 'backup_migrate_ui_manual_backup_advanced';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$form = [];
$bam = backup_migrate_get_service_object();
$form['source'] = [
'#type' => 'fieldset',
"#title" => $this
->t("Source"),
"#collapsible" => TRUE,
"#collapsed" => FALSE,
"#tree" => FALSE,
];
$form['source']['source_id'] = DrupalConfigHelper::getSourceSelector($bam, t('Backup Source'));
$form['source']['source_id']['#default_value'] = \Drupal::config('backup_migrate.settings')
->get('backup_migrate_source_id');
$form += DrupalConfigHelper::buildAllPluginsForm($bam
->plugins(), 'backup');
if (\Drupal::moduleHandler()
->moduleExists('token')) {
$filename_token = [
'#theme' => 'token_tree_link',
'#token_types' => [
'site',
],
'#dialog' => TRUE,
'#click_insert' => TRUE,
'#show_restricted' => TRUE,
'#group' => 'file',
];
}
else {
$filename_token = [
'#type' => 'markup',
'#markup' => 'In order to use tokens for File Name, please install & enable <a href="https://www.drupal.org/project/token" arget="_blank">Token module</a>. <p></p>',
];
}
array_splice($form['file'], 4, 0, [
'filename_token' => $filename_token,
]);
$form['destination'] = [
'#type' => 'fieldset',
"#title" => $this
->t("Destination"),
"#collapsible" => TRUE,
"#collapsed" => FALSE,
"#tree" => FALSE,
];
$form['destination']['destination_id'] = DrupalConfigHelper::getDestinationSelector($bam, t('Backup Destination'));
$form['destination']['destination_id']['#default_value'] = \Drupal::config('backup_migrate.settings')
->get('backup_migrate_destination_id');
$form['quickbackup']['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Backup now'),
'#weight' => 1,
];
return $form;
}
public function validateForm(array &$form, FormStateInterface $form_state) {
parent::validateForm($form, $form_state);
$bam = backup_migrate_get_service_object($form_state
->getValues());
if ($plugin_errors = $bam
->plugins()
->map('configErrors', [
'operation' => 'backup',
])) {
foreach ($plugin_errors as $plugin_key => $errors) {
foreach ($errors as $error) {
$form_state
->setErrorByName($plugin_key . '][' . $error
->getFieldKey(), $this
->t($error
->getMessage(), $error
->getReplacement()));
}
}
}
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$config = $form_state
->getValues();
backup_migrate_perform_backup($config['source_id'], $config['destination_id'], $config);
}
}