You are here

class BackupMigrateAdvancedBackupForm in Backup and Migrate 5.0.x

Same name and namespace in other branches
  1. 8.4 src/Form/BackupMigrateAdvancedBackupForm.php \Drupal\backup_migrate\Form\BackupMigrateAdvancedBackupForm

Provides a form for performing a 1-click site backup.

Hierarchy

Expanded class hierarchy of BackupMigrateAdvancedBackupForm

1 string reference to 'BackupMigrateAdvancedBackupForm'
backup_migrate.routing.yml in ./backup_migrate.routing.yml
backup_migrate.routing.yml

File

src/Form/BackupMigrateAdvancedBackupForm.php, line 12

Namespace

Drupal\backup_migrate\Form
View source
class BackupMigrateAdvancedBackupForm extends FormBase {

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'backup_migrate_ui_manual_backup_advanced';
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {

    // Leave a message about the Entire Site backup.
    // @see https://www.drupal.org/project/backup_migrate/issues/3151290
    $this
      ->messenger()
      ->addMessage($this
      ->t('It is recommended to not use the "Entire site" backup as it has a tendency of failing on anything but the tiniest of sites. Hopefully this will be fixed in a future release.'));
    $form = [];

    // Theme the form if we want it inline.
    // @FIXME
    // $form['#theme'] = 'backup_migrate_ui_manual_quick_backup_form_inline';
    $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, $this
      ->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, $this
      ->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;
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    parent::validateForm($form, $form_state);
    $bam = backup_migrate_get_service_object($form_state
      ->getValues());

    // Let the plugins validate their own config data.
    if ($plugin_errors = $bam
      ->plugins()
      ->map('configErrors', [
      'operation' => 'backup',
    ])) {
      $has_token_module = \Drupal::moduleHandler()
        ->moduleExists('token');
      foreach ($plugin_errors as $plugin_key => $errors) {
        if ($plugin_key == "namer" && isset($errors[0])) {
          if ($errors[0]
            ->getFieldKey() == "filename" && $has_token_module) {
            continue;
          }
        }
        foreach ($errors as $error) {
          $form_state
            ->setErrorByName($plugin_key . '][' . $error
            ->getFieldKey(), $this
            ->t($error
            ->getMessage(), $error
            ->getReplacement()));
        }
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $config = $form_state
      ->getValues();
    backup_migrate_perform_backup($config['source_id'], $config['destination_id'], $config);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BackupMigrateAdvancedBackupForm::buildForm public function Form constructor. Overrides FormInterface::buildForm
BackupMigrateAdvancedBackupForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
BackupMigrateAdvancedBackupForm::submitForm public function Form submission handler. Overrides FormInterface::submitForm
BackupMigrateAdvancedBackupForm::validateForm public function Form validation handler. Overrides FormBase::validateForm
DependencySerializationTrait::$_entityStorages protected property
DependencySerializationTrait::$_serviceIds protected property
DependencySerializationTrait::__sleep public function 2
DependencySerializationTrait::__wakeup public function 2
FormBase::$configFactory protected property The config factory. 3
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::config protected function Retrieves a configuration object.
FormBase::configFactory protected function Gets the config factory for this form. 3
FormBase::container private function Returns the service container.
FormBase::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create 105
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route.
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 27
MessengerTrait::messenger public function Gets the messenger. 27
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.