class BackupController in Backup and Migrate 5.0.x
Same name and namespace in other branches
- 8.4 src/Controller/BackupController.php \Drupal\backup_migrate\Controller\BackupController
@package Drupal\backup_migrate\Controller
Hierarchy
- class \Drupal\Core\Controller\ControllerBase implements ContainerInjectionInterface uses LoggerChannelTrait, MessengerTrait, RedirectDestinationTrait, StringTranslationTrait
- class \Drupal\backup_migrate\Controller\BackupController
Expanded class hierarchy of BackupController
File
- src/
Controller/ BackupController.php, line 17
Namespace
Drupal\backup_migrate\ControllerView source
class BackupController extends ControllerBase {
/**
* @var \Drupal\backup_migrate\Core\Destination\DestinationInterface
*/
public $destination;
/**
*
*/
public function listAll() {
$storage = \Drupal::entityTypeManager()
->getStorage('backup_migrate_destination');
$out = [];
foreach ($storage
->getQuery()
->execute() as $key) {
$entity = $storage
->load($key);
$destination = $entity
->getObject();
$label = $destination
->confGet('name');
$out[$key] = [
'title' => [
'#markup' => '<h2>' . $this
->t('Most recent backups in %dest', [
'%dest' => $label,
]) . '</h2>',
],
'list' => $this::listDestinationBackups($destination, $key, 5),
];
// Add the more link.
if ($entity
->access('backups') && $entity
->hasLinkTemplate('backups')) {
$out[$key]['link'] = $entity
->toLink($this
->t('View all backups in %dest', [
'%dest' => $label,
]), 'backups')
->toRenderable();
}
}
return $out;
}
/**
* Get the title for the listing page of a destination entity.
*
* @param \Drupal\backup_migrate\Entity\Destination $backup_migrate_destination
*
* @return \Drupal\Core\StringTranslation\TranslatableMarkup
*/
public function listDestinationEntityBackupsTitle(Destination $backup_migrate_destination) {
return $this
->t('Backups in @destination_name', [
'@destination_name' => $backup_migrate_destination
->label(),
]);
}
/**
* List the backups in the given destination.
*
* @param \Drupal\backup_migrate\Entity\Destination $backup_migrate_destination
*
* @return mixed
*/
public function listDestinationEntityBackups(Destination $backup_migrate_destination) {
$destination = $backup_migrate_destination
->getObject();
return $this
->listDestinationBackups($destination, $backup_migrate_destination
->id());
}
/**
* List the backups in the given destination.
*
* @param \Drupal\backup_migrate\Core\Destination\ListableDestinationInterface $destination
* @param $backup_migrate_destination_id
* @param int $count
*
* @return mixed
*/
public function listDestinationBackups(ListableDestinationInterface $destination, $backup_migrate_destination_id, $count = NULL) {
// Get a sorted list of files.
$rows = [];
$header = [
[
'data' => $this
->t('Name'),
'class' => [
RESPONSIVE_PRIORITY_MEDIUM,
],
'field' => 'name',
],
[
'data' => $this
->t('Date'),
'class' => [
RESPONSIVE_PRIORITY_MEDIUM,
],
'field' => 'datestamp',
'sort' => 'asc',
],
[
'data' => $this
->t('Size'),
'class' => [
RESPONSIVE_PRIORITY_MEDIUM,
],
'field' => 'filesize',
'sort' => 'desc',
],
[
'data' => $this
->t('Operations'),
'class' => [
RESPONSIVE_PRIORITY_LOW,
],
],
];
$order = TableSort::getOrder($header, \Drupal::request());
$sort = TableSort::getSort($header, \Drupal::request());
$php_sort = $sort == 'desc' ? SORT_DESC : SORT_ASC;
$backups = $destination
->queryFiles([], $order['sql'], $php_sort, $count);
foreach ($backups as $backup_id => $backup) {
$rows[] = [
'data' => [
// Cells.
$backup
->getFullName(),
\Drupal::service('date.formatter')
->format($backup
->getMeta('datestamp')),
format_size($backup
->getMeta('filesize')),
[
'data' => [
'#type' => 'operations',
'#links' => [
'restore' => [
'title' => $this
->t('Restore'),
'url' => Url::fromRoute('entity.backup_migrate_destination.backup_restore', [
'backup_migrate_destination' => $backup_migrate_destination_id,
'backup_id' => $backup_id,
]),
],
'download' => [
'title' => $this
->t('Download'),
'url' => Url::fromRoute('entity.backup_migrate_destination.backup_download', [
'backup_migrate_destination' => $backup_migrate_destination_id,
'backup_id' => $backup_id,
]),
],
'delete' => [
'title' => $this
->t('Delete'),
'url' => Url::fromRoute('entity.backup_migrate_destination.backup_delete', [
'backup_migrate_destination' => $backup_migrate_destination_id,
'backup_id' => $backup_id,
]),
],
],
],
],
],
];
}
$build['backups_table'] = [
'#type' => 'table',
'#header' => $header,
'#rows' => $rows,
'#empty' => $this
->t('There are no backups in this destination.'),
];
return $build;
}
/**
* Download a backup via the browser.
*
* @param \Drupal\backup_migrate\Entity\Destination $backup_migrate_destination
* @param $backup_id
*/
public function download(Destination $backup_migrate_destination, $backup_id) {
$destination = $backup_migrate_destination
->getObject();
$file = $destination
->getFile($backup_id);
$file = $destination
->loadFileForReading($file);
$browser = new DrupalBrowserDownloadDestination();
$browser
->saveFile($file);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
BackupController:: |
public | property | ||
BackupController:: |
public | function | Download a backup via the browser. | |
BackupController:: |
public | function | ||
BackupController:: |
public | function | List the backups in the given destination. | |
BackupController:: |
public | function | List the backups in the given destination. | |
BackupController:: |
public | function | Get the title for the listing page of a destination entity. | |
ControllerBase:: |
protected | property | The configuration factory. | |
ControllerBase:: |
protected | property | The current user service. | 1 |
ControllerBase:: |
protected | property | The entity form builder. | |
ControllerBase:: |
protected | property | The entity type manager. | |
ControllerBase:: |
protected | property | The form builder. | 2 |
ControllerBase:: |
protected | property | The key-value storage. | 1 |
ControllerBase:: |
protected | property | The language manager. | 1 |
ControllerBase:: |
protected | property | The module handler. | 2 |
ControllerBase:: |
protected | property | The state service. | |
ControllerBase:: |
protected | function | Returns the requested cache bin. | |
ControllerBase:: |
protected | function | Retrieves a configuration object. | |
ControllerBase:: |
private | function | Returns the service container. | |
ControllerBase:: |
public static | function |
Instantiates a new instance of this class. Overrides ContainerInjectionInterface:: |
46 |
ControllerBase:: |
protected | function | Returns the current user. | 1 |
ControllerBase:: |
protected | function | Retrieves the entity form builder. | |
ControllerBase:: |
protected | function | Retrieves the entity type manager. | |
ControllerBase:: |
protected | function | Returns the form builder service. | 2 |
ControllerBase:: |
protected | function | Returns a key/value storage collection. | 1 |
ControllerBase:: |
protected | function | Returns the language manager service. | 1 |
ControllerBase:: |
protected | function | Returns the module handler. | 2 |
ControllerBase:: |
protected | function | Returns a redirect response object for the specified route. | |
ControllerBase:: |
protected | function | Returns the state storage service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 27 |
MessengerTrait:: |
public | function | Gets the messenger. | 27 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 4 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |