config.module 4.91 KB
Newer Older
Crell's avatar
Crell committed
1
<?php
2 3 4 5 6 7

/**
 * @file
 * Allows site administrators to modify configuration.
 */

8
use Drupal\Core\Routing\RouteMatchInterface;
9 10
use Drupal\Core\StreamWrapper\StreamWrapperManager;
use Drupal\Core\Url;
11

12 13 14
/**
 * Implements hook_help().
 */
15
function config_help($route_name, RouteMatchInterface $route_match) {
16 17
  switch ($route_name) {
    case 'help.page.config':
18 19
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
20
      $output .= '<p>' . t('The Configuration Manager module provides a user interface for importing and exporting configuration changes between installations of your website in different environments. Configuration is stored in YAML format. For more information, see the <a href=":url">online documentation for the Configuration Manager module</a>.', [':url' => 'https://www.drupal.org/documentation/administer/config']) . '</p>';
21 22 23
      $output .= '<h3>' . t('Uses') . '</h3>';
      $output .= '<dl>';
      $output .= '<dt>' . t('Exporting the full configuration') . '</dt>';
24
      $output .= '<dd>' . t('You can create and download an archive consisting of all your site\'s configuration exported as <em>*.yml</em> files on the <a href=":url">Export</a> page.', [':url' => Url::fromRoute('config.export_full')->toString()]) . '</dd>';
25
      $output .= '<dt>' . t('Importing a full configuration') . '</dt>';
26
      $output .= '<dd>' . t('You can upload a full site configuration from an archive file on the <a href=":url">Import</a> page. When importing data from a different environment, the site and import files must have matching configuration values for UUID in the <em>system.site</em> configuration item. That means that your other environments should initially be set up as clones of the target site. Migrations are not supported.', [':url' => Url::fromRoute('config.import_full')->toString()]) . '</dd>';
27
      $output .= '<dt>' . t('Synchronizing configuration') . '</dt>';
28
      $output .= '<dd>' . t('You can review differences between the active configuration and an imported configuration archive on the <a href=":synchronize">Synchronize</a> page to ensure that the changes are as expected, before finalizing the import. The Synchronize page also shows configuration items that would be added or removed.', [':synchronize' => Url::fromRoute('config.sync')->toString()]) . '</dd>';
29
      $output .= '<dt>' . t('Exporting a single configuration item') . '</dt>';
30
      $output .= '<dd>' . t('You can export a single configuration item by selecting a <em>Configuration type</em> and <em>Configuration name</em> on the <a href=":single-export">Single export</a> page. The configuration and its corresponding <em>*.yml file name</em> are then displayed on the page for you to copy.', [':single-export' => Url::fromRoute('config.export_single')->toString()]) . '</dd>';
31
      $output .= '<dt>' . t('Importing a single configuration item') . '</dt>';
32
      $output .= '<dd>' . t('You can import a single configuration item by pasting it in YAML format into the form on the <a href=":single-import">Single import</a> page.', [':single-import' => Url::fromRoute('config.import_single')->toString()]) . '</dd>';
33
      $output .= '</dl>';
34 35
      return $output;

36
    case 'config.sync':
37
      $output = '';
38
      $output .= '<p>' . t('Compare the configuration uploaded to your sync directory with the active configuration before completing the import.') . '</p>';
39
      return $output;
40

41 42
    case 'config.export_full':
      $output = '';
43
      $output .= '<p>' . t('Export and download the full configuration of this site as a gzipped tar file.') . '</p>';
44 45
      return $output;

46 47
    case 'config.import_full':
      $output = '';
48
      $output .= '<p>' . t('Upload a full site configuration archive to the sync directory. It can then be compared and imported on the Synchronize page.') . '</p>';
49 50 51 52
      return $output;

    case 'config.export_single':
      $output = '';
53
      $output .= '<p>' . t('Choose a configuration item to display its YAML structure.') . '</p>';
54 55 56 57
      return $output;

    case 'config.import_single':
      $output = '';
58
      $output .= '<p>' . t('Import a single configuration item by pasting its YAML structure into the text field.') . '</p>';
59
      return $output;
60 61 62
  }
}

63 64 65 66
/**
 * Implements hook_file_download().
 */
function config_file_download($uri) {
67 68
  $scheme = StreamWrapperManager::getScheme($uri);
  $target = StreamWrapperManager::getTarget($uri);
69
  if ($scheme == 'temporary' && $target == 'config.tar.gz') {
70 71 72 73 74
    if (\Drupal::currentUser()->hasPermission('export configuration')) {
      $request = \Drupal::request();
      $date = DateTime::createFromFormat('U', $request->server->get('REQUEST_TIME'));
      $date_string = $date->format('Y-m-d-H-i');
      $hostname = str_replace('.', '-', $request->getHttpHost());
75
      $filename = 'config-' . $hostname . '-' . $date_string . '.tar.gz';
76
      $disposition = 'attachment; filename="' . $filename . '"';
77
      return [
78
        'Content-disposition' => $disposition,
79
      ];
80 81
    }
    return -1;
82 83
  }
}