You are here

function demo_get_dumps in Demonstration site (Sandbox / Snapshot) 8

Same name and namespace in other branches
  1. 5 demo.admin.inc \demo_get_dumps()
  2. 6 demo.admin.inc \demo_get_dumps()
  3. 7 demo.admin.inc \demo_get_dumps()

Get all the dumps from the private directory.

2 calls to demo_get_dumps()
DemoManageForm::buildForm in src/Form/DemoManageForm.php
Form constructor.
DemoResetConfirm::buildForm in src/Form/DemoResetConfirm.php
Form constructor.

File

./demo.module, line 57

Code

function demo_get_dumps() {
  $fileconfig = demo_get_fileconfig();

  // Fetch list of available info files.
  $files = \Drupal::service('file_system')
    ->scanDirectory($fileconfig['dumppath'], '/\\.info$/');
  foreach ($files as $file => $object) {
    $files[$file]->filemtime = filemtime($file);
    $files[$file]->filesize = filesize(substr($file, 0, -4) . 'sql');
  }

  // Sort snapshots by date (ascending file modification time).
  uasort($files, function ($a, $b) {
    return $a->filemtime < $b->filemtime;
  });
  $element = [
    '#type' => 'radios',
    '#title' => t('Snapshot'),
    '#required' => TRUE,
    '#parents' => [
      'filename',
    ],
    '#options' => [],
    '#attributes' => [
      'class' => [
        'demo-snapshots-widget',
      ],
    ],
    '#attached' => [
      'library' => [
        'demo/demo-library',
      ],
    ],
  ];
  foreach ($files as $filename => $file) {
    $info = demo_get_info($filename);

    // Prepare snapshot title.
    $title = t('@snapshot <small>(@date, @aize)</small>', [
      '@snapshot' => $info['filename'],
      '@date' => \Drupal::service('date.formatter')
        ->format($file->filemtime, 'small'),
      '@aize' => format_size($file->filesize),
    ]);

    // Prepare snapshot description.
    $description = '';
    if (!empty($info['description'])) {
      $description .= '<p>' . $info['description'] . '</p>';
    }

    // Add download links.
    $download_sql_url = Url::fromRoute('demo.download', $route_parameters = [
      'filename' => $file->name,
      'type' => 'sql',
    ]);
    $download_info_url = Url::fromRoute('demo.download', $route_parameters = [
      'filename' => $file->name,
      'type' => 'info',
    ]);
    $description .= '<p>' . t('Download: ' . Link::fromTextAndUrl(t('.info file'), $download_info_url) . ', ' . Link::fromTextAndUrl(t('.sql file'), $download_sql_url)) . '</p>';

    // Add module list.
    if (count($info['modules']) > 1) {

      // Remove required core modules and Demo from module list.
      $modules = array_diff($info['modules'], [
        'filter',
        'node',
        'system',
        'user',
        'demo',
      ]);

      // Sort module list alphabetically.
      sort($modules);
      $description .= t('Modules: @modules', [
        '@modules' => implode(', ', $modules),
      ]);
    }

    // Add the radio option element.
    $element['#options'][$info['filename']] = $title;
    $element[$info['filename']] = [
      '#description' => $description,
      '#file' => $file,
      '#info' => $info,
    ];
  }
  return $element;
}