Last active
May 29, 2023 05:51
-
-
Save junaidpv/b8fd3eda30449fbb9c2f7a966fc6d583 to your computer and use it in GitHub Desktop.
Patch to bring feature allowing to export config split configurations.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/config_split.module b/config_split.module | |
index 0cd7135..adcbb4b 100644 | |
--- a/config_split.module | |
+++ b/config_split.module | |
@@ -9,6 +9,8 @@ | |
use Drupal\Core\Form\FormStateInterface; | |
use Drupal\Core\Link; | |
use Drupal\Core\Routing\RouteMatchInterface; | |
+use Drupal\Core\StreamWrapper\StreamWrapperManager; | |
+use Drupal\config_split\Controller\ConfigDownloadController; | |
/** | |
* Implements hook_form_FORM_ID_alter(). | |
@@ -127,3 +129,20 @@ function config_split_config_readonly_whitelist_patterns() { | |
} | |
return $patterns; | |
} | |
+ | |
+/** | |
+ * Implements hook_file_download(). | |
+ */ | |
+function config_split_file_download($uri) { | |
+ $scheme = StreamWrapperManager::getScheme($uri); | |
+ $target = StreamWrapperManager::getTarget($uri); | |
+ if ($scheme == 'temporary' && str_starts_with($target, ConfigDownloadController::FILE_PREFIX)) { | |
+ if (\Drupal::currentUser()->hasPermission('administer configuration split')) { | |
+ $disposition = 'attachment; filename="' . $target . '"'; | |
+ return [ | |
+ 'Content-disposition' => $disposition, | |
+ ]; | |
+ } | |
+ return -1; | |
+ } | |
+} | |
diff --git a/config_split.routing.yml b/config_split.routing.yml | |
index 604a8a8..2d212b3 100644 | |
--- a/config_split.routing.yml | |
+++ b/config_split.routing.yml | |
@@ -65,3 +65,11 @@ config_split.diff_collection: | |
target_name: NULL | |
requirements: | |
_permission: 'administer configuration split' | |
+ | |
+config_split.entity_export_download: | |
+ path: '/admin/config/development/configuration/config-split/{config_split}/download' | |
+ defaults: | |
+ _controller: '\Drupal\config_split\Controller\ConfigDownloadController::download' | |
+ _title: 'Download' | |
+ requirements: | |
+ _permission: 'administer configuration split' | |
diff --git a/src/Controller/ConfigDownloadController.php b/src/Controller/ConfigDownloadController.php | |
new file mode 100644 | |
index 0000000..ebdce8e | |
--- /dev/null | |
+++ b/src/Controller/ConfigDownloadController.php | |
@@ -0,0 +1,108 @@ | |
+<?php | |
+ | |
+namespace Drupal\config_split\Controller; | |
+ | |
+use Symfony\Component\HttpFoundation\Request; | |
+use Drupal\system\FileDownloadController; | |
+use Drupal\Core\Archiver\ArchiveTar; | |
+use Drupal\Core\DependencyInjection\ContainerInjectionInterface; | |
+use Symfony\Component\DependencyInjection\ContainerInterface; | |
+use Drupal\Core\State\StateInterface; | |
+use Drupal\config_split\ConfigSplitManager; | |
+use Drupal\Core\Routing\RouteMatchInterface; | |
+use Drupal\Core\Serialization\Yaml; | |
+ | |
+class ConfigDownloadController implements ContainerInjectionInterface { | |
+ | |
+ const FILE_PREFIX = 'config-split--'; | |
+ | |
+ /** | |
+ * The state keyvalue collection. | |
+ * | |
+ * @var \Drupal\Core\State\StateInterface | |
+ */ | |
+ protected $state; | |
+ | |
+ /** | |
+ * The split manager. | |
+ * | |
+ * @var \Drupal\config_split\ConfigSplitManager | |
+ */ | |
+ protected $manager; | |
+ | |
+ /** | |
+ * @var \Drupal\Core\Routing\RouteMatchInterface | |
+ */ | |
+ protected $routeMatch; | |
+ | |
+ /** | |
+ * The constructor. | |
+ * | |
+ * @param \Drupal\Core\Config\StorageInterface $activeStorage | |
+ * The active storage. | |
+ * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager | |
+ * The entity type manager. | |
+ * @param \Drupal\Core\Config\ConfigManagerInterface $configManager | |
+ * The config manager. | |
+ * @param \Drupal\Core\Diff\DiffFormatter $diffFormatter | |
+ * The diff formatter. | |
+ * @param \Drupal\config_split\ConfigSplitManager $configSplitManager | |
+ * The split manager. | |
+ */ | |
+ public function __construct(StateInterface $state, ConfigSplitManager $configSplitManager, RouteMatchInterface $route_match) { | |
+ $this->state = $state; | |
+ $this->manager = $configSplitManager; | |
+ $this->routeMatch = $route_match; | |
+ } | |
+ | |
+ /** | |
+ * {@inheritdoc} | |
+ */ | |
+ public static function create(ContainerInterface $container) { | |
+ return new static( | |
+ $container->get('state'), | |
+ $container->get('config_split.manager'), | |
+ $container->get('current_route_match') | |
+ ); | |
+ } | |
+ | |
+ /** | |
+ * Get a split from the route. | |
+ * | |
+ * @return \Drupal\Core\Config\ImmutableConfig | |
+ * The split config. | |
+ */ | |
+ protected function getSplit() { | |
+ $split = $this->manager->getSplitConfig($this->routeMatch->getRawParameter('config_split')); | |
+ if ($split === NULL) { | |
+ throw new \UnexpectedValueException("Unknown split"); | |
+ } | |
+ return $split; | |
+ } | |
+ | |
+ public function download() { | |
+ $request = \Drupal::request(); | |
+ $file_system = \Drupal::service('file_system'); | |
+ $split = $this->getSplit(); | |
+ | |
+ $date = \DateTime::createFromFormat('U', $request->server->get('REQUEST_TIME')); | |
+ $date_string = $date->format('Y-m-d-H-i'); | |
+ | |
+ $filename = self::FILE_PREFIX . $split->get('id') . '-' . $date_string . '.tar.gz'; | |
+ | |
+ $archiver = new ArchiveTar($file_system->getTempDirectory() . '/' . $filename, 'gz'); | |
+ | |
+ $target = $this->manager->singleExportTarget($split); | |
+ | |
+ $names = $target->listAll(); | |
+ foreach ($names as $name) { | |
+ $archiver->addString($name . '.yml', Yaml::encode($target->read($name))); | |
+ } | |
+ | |
+ $request = new Request(['file' => $filename]); | |
+ | |
+ $file_download_controller = new FileDownloadController(\Drupal::service('stream_wrapper_manager')); | |
+ | |
+ return $file_download_controller->download($request, 'temporary'); | |
+ } | |
+} | |
\ No newline at end of file | |
diff --git a/src/Form/ConfigImportFormTrait.php b/src/Form/ConfigImportFormTrait.php | |
index 258dc51..add1eca 100644 | |
--- a/src/Form/ConfigImportFormTrait.php | |
+++ b/src/Form/ConfigImportFormTrait.php | |
@@ -98,6 +98,11 @@ protected function buildFormWithStorageComparer( | |
'#empty' => $this->t('There are no configuration changes to make.'), | |
]; | |
$form['actions']['#access'] = FALSE; | |
+ $form['download'] = [ | |
+ '#title' => $this->t('Download'), | |
+ '#type' => 'link', | |
+ '#url' => Url::fromRoute('config_split.entity_export_download', ['config_split' => $options['route']['config_split']]), | |
+ ]; | |
return $form; | |
} | |
elseif ($validate && !$storage_comparer->validateSiteUuid()) { |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment