1 backup.inc backup_settings_form(array $form, array &$form_state, array $options = array())

A general-purpose form builder for creating a backup.

This form builder is used by update.php to generate the backup form step of running system updates. It can also be used by contributed modules to quickly create a backup.

See the backup_test.module for a demonstration of how this can be used.

Parameters

array $form: A form API array to which the backup settings should be added.

array $form_state: The form state array.

array $options: An array of options containing the following keys:

  • show_settings: Whether each backup handler class should show the advanced settings for that type of backup. Defaults to FALSE.
  • show_default_targets: Whether the targets of the default database and the default configuration location should be shown as checkboxes, allowing them to not be backed up. If not shown, they are rendered as type = "hidden" elements. Defaults to FALSE.

See also

update_backup_form()

backup_test_settings_form()

File

core/includes/backup.inc, line 143
Contains functionality related to creating and restoring site backups.

Code

function backup_settings_form(array $form, array &$form_state, array $options = array()) {
  $options += array(
    'show_settings' => FALSE,
    'show_default_targets' => FALSE,
  );

  $backup_directory = backup_get_backup_directory();
  $backup_targets = settings_get('backup_targets', array());
  $backup_targets = array_merge($backup_targets, array(
    'db:default',
    'config:active',
  ));

  // Two expected elements:
  $form['options']['#tree'] = TRUE;
  $form['targets']['#tree'] = TRUE;

  foreach ($backup_targets as $backup_target) {
    list($backup_plugin_id, $backup_subkey) = explode(':', $backup_target, 2);
    $backup_class = backup_get_handler_name($backup_target);
    if (!$backup_class) {
      continue;
    }

    $backup_target_name = $backup_plugin_id . '_' . $backup_subkey;
    $backup_settings = array();
    $backup_instance = new $backup_class($backup_target_name, $backup_target, $backup_settings);
    $backup_form = $backup_instance->backupSettingsForm();

    if ($options['show_settings'] && $backup_form) {
      $form['targets'][$backup_target_name] = array(
        '#type' => 'fieldset',
        '#title' => t('@type backup settings', array(
          '@type' => $backup_instance->typeLabel(),
        )),
        '#collapsible' => FALSE,
      );

      $form['targets'][$backup_target_name]['settings'] = $backup_form;
      $form['targets'][$backup_target_name]['settings']['#type'] = 'container';
    }

    // Show a checkbox if non-default targets are present.
    if ($options['show_default_targets'] || ($backup_target != 'db:default' && $backup_target != 'config:active')) {
      $form['targets'][$backup_target_name]['enabled'] = array(
        '#type' => 'checkbox',
        '#title' => t('Optional backup: @target_name', array('@target_name' => $backup_target_name)),
        '#default_value' => TRUE,
        '#weight' => -1,
      );
    }
    else {
      $form['targets'][$backup_target_name]['enabled'] = array(
        '#type' => 'hidden',
        '#value' => TRUE,
      );
    }

    $form['targets'][$backup_target_name]['name'] = array(
      '#type' => 'hidden',
      '#value' => $backup_target_name,
    );
    $form['targets'][$backup_target_name]['target'] = array(
      '#type' => 'hidden',
      '#value' => $backup_target,
    );
  }

  $form['actions'] = array('#type' => 'actions');
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Create backup'),
    '#disabled' => empty($backup_directory),
  );

  return $form;
}