From d31ade0f626069013913552aa4508cbe2fbd1e81 Mon Sep 17 00:00:00 2001
From: catch <catch@35733.no-reply.drupal.org>
Date: Mon, 8 Apr 2024 08:15:25 +0100
Subject: [PATCH] Issue #3437162 by plopesc, smustgrave, Berdir: Move
 twig_debug and other development toggles into raw key/value

(cherry picked from commit be5ddf733749e78ccfd7ab3b2f0d8446c3c0743f)
---
 .../Compiler/DevelopmentSettingsPass.php      | 14 ++++-----
 .../src/Form/DevelopmentSettingsForm.php      | 30 ++++++++++---------
 core/modules/system/system.install            |  7 +++--
 core/modules/system/system.post_update.php    | 23 ++++++++++++++
 4 files changed, 49 insertions(+), 25 deletions(-)

diff --git a/core/lib/Drupal/Core/DependencyInjection/Compiler/DevelopmentSettingsPass.php b/core/lib/Drupal/Core/DependencyInjection/Compiler/DevelopmentSettingsPass.php
index 2ab8b22729f2..29df89feeda8 100644
--- a/core/lib/Drupal/Core/DependencyInjection/Compiler/DevelopmentSettingsPass.php
+++ b/core/lib/Drupal/Core/DependencyInjection/Compiler/DevelopmentSettingsPass.php
@@ -14,13 +14,11 @@ class DevelopmentSettingsPass implements CompilerPassInterface {
   /**
    * {@inheritdoc}
    */
-  public function process(ContainerBuilder $container) {
-    // This does access the state key value store directly to avoid edge-cases
-    // with lazy ghost objects during early bootstrap.
-    /** @var \Drupal\Core\KeyValueStore\KeyValueStoreInterface $state */
-    $state = $container->get('keyvalue')->get('state');
-    $twig_debug = $state->get('twig_debug', FALSE);
-    $twig_cache_disable = $state->get('twig_cache_disable', FALSE);
+  public function process(ContainerBuilder $container): void {
+    /** @var \Drupal\Core\KeyValueStore\KeyValueStoreInterface $development_settings */
+    $development_settings = $container->get('keyvalue')->get('development_settings');
+    $twig_debug = $development_settings->get('twig_debug', FALSE);
+    $twig_cache_disable = $development_settings->get('twig_cache_disable', FALSE);
     if ($twig_debug || $twig_cache_disable) {
       $twig_config = $container->getParameter('twig.config');
       $twig_config['debug'] = $twig_debug;
@@ -28,7 +26,7 @@ public function process(ContainerBuilder $container) {
       $container->setParameter('twig.config', $twig_config);
     }
 
-    if ($state->get('disable_rendered_output_cache_bins', FALSE)) {
+    if ($development_settings->get('disable_rendered_output_cache_bins', FALSE)) {
       $cache_bins = ['page', 'dynamic_page_cache', 'render'];
       if (!$container->hasDefinition('cache.backend.null')) {
         $container->register('cache.backend.null', NullBackendFactory::class);
diff --git a/core/modules/system/src/Form/DevelopmentSettingsForm.php b/core/modules/system/src/Form/DevelopmentSettingsForm.php
index 282d0f02cf2f..efa6ec332003 100644
--- a/core/modules/system/src/Form/DevelopmentSettingsForm.php
+++ b/core/modules/system/src/Form/DevelopmentSettingsForm.php
@@ -5,7 +5,7 @@
 use Drupal\Core\DrupalKernelInterface;
 use Drupal\Core\Form\FormBase;
 use Drupal\Core\Form\FormStateInterface;
-use Drupal\Core\State\StateInterface;
+use Drupal\Core\KeyValueStore\KeyValueFactoryInterface;
 use Symfony\Component\DependencyInjection\ContainerInterface;
 
 /**
@@ -18,13 +18,13 @@ class DevelopmentSettingsForm extends FormBase {
   /**
    * Constructs a new development settings form.
    *
-   * @param \Drupal\Core\State\StateInterface $state
-   *   The state service.
+   * @param \Drupal\Core\KeyValueStore\KeyValueFactoryInterface $keyValueFactory
+   *   The key value factory.
    * @param \Drupal\Core\DrupalKernelInterface $kernel
    *   The Drupal kernel.
    */
   public function __construct(
-    protected StateInterface $state,
+    protected KeyValueFactoryInterface $keyValueFactory,
     protected DrupalKernelInterface $kernel
   ) {
   }
@@ -34,7 +34,7 @@ public function __construct(
    */
   public static function create(ContainerInterface $container) {
     $instance = new static(
-      $container->get('state'),
+      $container->get('keyvalue'),
       $container->get('kernel')
     );
     $instance->setMessenger($container->get('messenger'));
@@ -52,12 +52,13 @@ public function getFormId() {
    * {@inheritdoc}
    */
   public function buildForm(array $form, FormStateInterface $form_state) {
+    $development_settings = $this->keyValueFactory->get('development_settings');
     $form['description'] = [
       '#plain_text' => $this->t('These settings should only be enabled on development environments and never on production.'),
     ];
 
-    $twig_debug = $this->state->get('twig_debug', FALSE);
-    $twig_cache_disable = $this->state->get('twig_cache_disable', FALSE);
+    $twig_debug = $development_settings->get('twig_debug', FALSE);
+    $twig_cache_disable = $development_settings->get('twig_cache_disable', FALSE);
     $twig_development_state_conditions = [
       'input[data-drupal-selector="edit-twig-development-mode"]' => [
         'checked' => TRUE,
@@ -101,7 +102,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
       '#type' => 'checkbox',
       '#title' => $this->t('Do not cache markup'),
       '#description' => $this->t('Disables render cache, dynamic page cache, and page cache.'),
-      '#default_value' => $this->state->get('disable_rendered_output_cache_bins', FALSE),
+      '#default_value' => $development_settings->get('disable_rendered_output_cache_bins', FALSE),
     ];
 
     $form['actions']['#type'] = 'actions';
@@ -118,28 +119,29 @@ public function buildForm(array $form, FormStateInterface $form_state) {
    * {@inheritdoc}
    */
   public function submitForm(array &$form, FormStateInterface $form_state) {
-    $disable_rendered_output_cache_bins_previous = $this->state->get('disable_rendered_output_cache_bins', FALSE);
+    $development_settings = $this->keyValueFactory->get('development_settings');
+    $disable_rendered_output_cache_bins_previous = $development_settings->get('disable_rendered_output_cache_bins', FALSE);
     $disable_rendered_output_cache_bins = (bool) $form_state->getValue('disable_rendered_output_cache_bins');
     if ($disable_rendered_output_cache_bins) {
-      $this->state->set('disable_rendered_output_cache_bins', TRUE);
+      $development_settings->set('disable_rendered_output_cache_bins', TRUE);
     }
     else {
-      $this->state->delete('disable_rendered_output_cache_bins');
+      $development_settings->delete('disable_rendered_output_cache_bins');
     }
 
     $twig_development_mode = (bool) $form_state->getValue('twig_development_mode');
-    $twig_development_previous = $this->state->getMultiple(['twig_debug', 'twig_cache_disable']);
+    $twig_development_previous = $development_settings->getMultiple(['twig_debug', 'twig_cache_disable']);
     $twig_development = [
       'twig_debug' => (bool) $form_state->getValue('twig_debug'),
       'twig_cache_disable' => (bool) $form_state->getValue('twig_cache_disable'),
     ];
     if ($twig_development_mode) {
       $invalidate_container = $twig_development_previous !== $twig_development;
-      $this->state->setMultiple($twig_development);
+      $development_settings->setMultiple($twig_development);
     }
     else {
       $invalidate_container = TRUE;
-      $this->state->deleteMultiple(array_keys($twig_development));
+      $development_settings->deleteMultiple(array_keys($twig_development));
     }
 
     if ($invalidate_container || $disable_rendered_output_cache_bins_previous !== $disable_rendered_output_cache_bins) {
diff --git a/core/modules/system/system.install b/core/modules/system/system.install
index a98661a40c6d..97d01080da09 100644
--- a/core/modules/system/system.install
+++ b/core/modules/system/system.install
@@ -1549,8 +1549,9 @@ function (callable $hook, string $module) use (&$module_list, $update_registry,
 
   // Add warning when twig debug option is enabled.
   if ($phase === 'runtime') {
-    $twig_debug = \Drupal::state()->get('twig_debug', FALSE);
-    $twig_cache_disable = \Drupal::state()->get('twig_cache_disable', FALSE);
+    $development_settings = \Drupal::keyValue('development_settings');
+    $twig_debug = $development_settings->get('twig_debug', FALSE);
+    $twig_cache_disable = $development_settings->get('twig_cache_disable', FALSE);
     if ($twig_debug || $twig_cache_disable) {
       $requirements['twig_debug_enabled'] = [
         'title' => t('Twig development mode'),
@@ -1563,7 +1564,7 @@ function (callable $hook, string $module) use (&$module_list, $update_registry,
         'severity' => REQUIREMENT_WARNING,
       ];
     }
-    $render_cache_disabled = \Drupal::state()->get('disable_rendered_output_cache_bins', FALSE);
+    $render_cache_disabled = $development_settings->get('disable_rendered_output_cache_bins', FALSE);
     if ($render_cache_disabled) {
       $requirements['render_cache_disabled'] = [
         'title' => t('Markup caching disabled'),
diff --git a/core/modules/system/system.post_update.php b/core/modules/system/system.post_update.php
index 13b15470f402..952f25d9ede3 100644
--- a/core/modules/system/system.post_update.php
+++ b/core/modules/system/system.post_update.php
@@ -223,3 +223,26 @@ function system_post_update_set_cron_logging_setting_to_boolean(): void {
     $config->set('logging', (bool) $logging)->save();
   }
 }
+
+/**
+ * Uninstall the sdc module if installed.
+ */
+function system_post_update_sdc_uninstall() {
+  if (\Drupal::moduleHandler()->moduleExists('sdc')) {
+    \Drupal::service('module_installer')->uninstall(['sdc'], FALSE);
+  }
+}
+
+/**
+ * Move development settings from state to raw key-value storage.
+ */
+function system_post_update_move_development_settings_to_keyvalue(): void {
+  $state = \Drupal::state();
+  $development_settings = $state->getMultiple([
+    'twig_debug',
+    'twig_cache_disable',
+    'disable_rendered_output_cache_bins',
+  ]);
+  \Drupal::keyValue('development_settings')->setMultiple($development_settings);
+  $state->deleteMultiple(array_keys($development_settings));
+}
-- 
GitLab