From 60aff39db4dc724f5534226a394d11d46a103052 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20L=C3=B3pez=20//=20plopesc?= Date: Wed, 3 Apr 2024 09:50:28 +0200 Subject: [PATCH 1/5] Issue #3437162: Move twig_debug and other development toggles into raw key/value --- .../Compiler/DevelopmentSettingsPass.php | 12 +++--- .../src/Form/DevelopmentSettingsForm.php | 37 ++++++++++++------- core/modules/system/system.install | 7 ++-- 3 files changed, 32 insertions(+), 24 deletions(-) diff --git a/core/lib/Drupal/Core/DependencyInjection/Compiler/DevelopmentSettingsPass.php b/core/lib/Drupal/Core/DependencyInjection/Compiler/DevelopmentSettingsPass.php index 0d385e2c3628..29df89feeda8 100644 --- a/core/lib/Drupal/Core/DependencyInjection/Compiler/DevelopmentSettingsPass.php +++ b/core/lib/Drupal/Core/DependencyInjection/Compiler/DevelopmentSettingsPass.php @@ -15,12 +15,10 @@ class DevelopmentSettingsPass implements CompilerPassInterface { * {@inheritdoc} */ public function process(ContainerBuilder $container): void { - // 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); + /** @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): void { $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..e9163e0e52f7 100644 --- a/core/modules/system/src/Form/DevelopmentSettingsForm.php +++ b/core/modules/system/src/Form/DevelopmentSettingsForm.php @@ -5,7 +5,8 @@ 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 Drupal\Core\KeyValueStore\KeyValueStoreInterface; use Symfony\Component\DependencyInjection\ContainerInterface; /** @@ -15,18 +16,26 @@ */ class DevelopmentSettingsForm extends FormBase { + /** + * Development settings key-value storage + * + * @var \Drupal\Core\KeyValueStore\KeyValueStoreInterface + */ + protected KeyValueStoreInterface $keyValueStore; + /** * 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 ) { + $this->keyValueStore = $this->keyValueFactory->get('development_settings'); } /** @@ -34,7 +43,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')); @@ -56,8 +65,8 @@ public function buildForm(array $form, FormStateInterface $form_state) { '#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 = $this->keyValueStore->get('twig_debug', FALSE); + $twig_cache_disable = $this->keyValueStore->get('twig_cache_disable', FALSE); $twig_development_state_conditions = [ 'input[data-drupal-selector="edit-twig-development-mode"]' => [ 'checked' => TRUE, @@ -101,7 +110,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' => $this->keyValueStore->get('disable_rendered_output_cache_bins', FALSE), ]; $form['actions']['#type'] = 'actions'; @@ -118,28 +127,28 @@ 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); + $disable_rendered_output_cache_bins_previous = $this->keyValueStore->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); + $this->keyValueStore->set('disable_rendered_output_cache_bins', TRUE); } else { - $this->state->delete('disable_rendered_output_cache_bins'); + $this->keyValueStore->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 = $this->keyValueStore->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); + $this->keyValueStore->setMultiple($twig_development); } else { $invalidate_container = TRUE; - $this->state->deleteMultiple(array_keys($twig_development)); + $this->keyValueStore->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 1cea9ec49176..a53f50228d46 100644 --- a/core/modules/system/system.install +++ b/core/modules/system/system.install @@ -1536,8 +1536,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'), @@ -1550,7 +1551,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'), -- GitLab From c0fbd807e7438207efbe4872476c6917b8a15c01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20L=C3=B3pez=20//=20plopesc?= Date: Wed, 3 Apr 2024 09:54:24 +0200 Subject: [PATCH 2/5] Issue #3437162: Fix PHPCS --- core/modules/system/src/Form/DevelopmentSettingsForm.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/core/modules/system/src/Form/DevelopmentSettingsForm.php b/core/modules/system/src/Form/DevelopmentSettingsForm.php index e9163e0e52f7..5532eac2d2d8 100644 --- a/core/modules/system/src/Form/DevelopmentSettingsForm.php +++ b/core/modules/system/src/Form/DevelopmentSettingsForm.php @@ -17,7 +17,7 @@ class DevelopmentSettingsForm extends FormBase { /** - * Development settings key-value storage + * Development settings key-value storage. * * @var \Drupal\Core\KeyValueStore\KeyValueStoreInterface */ @@ -66,7 +66,7 @@ public function buildForm(array $form, FormStateInterface $form_state) { ]; $twig_debug = $this->keyValueStore->get('twig_debug', FALSE); - $twig_cache_disable = $this->keyValueStore->get('twig_cache_disable', FALSE); + $twig_cache_disable = $this->keyValueStore->get('twig_cache_disable', FALSE); $twig_development_state_conditions = [ 'input[data-drupal-selector="edit-twig-development-mode"]' => [ 'checked' => TRUE, @@ -110,7 +110,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->keyValueStore->get('disable_rendered_output_cache_bins', FALSE), + '#default_value' => $this->keyValueStore->get('disable_rendered_output_cache_bins', FALSE), ]; $form['actions']['#type'] = 'actions'; @@ -127,7 +127,7 @@ public function buildForm(array $form, FormStateInterface $form_state) { * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { - $disable_rendered_output_cache_bins_previous = $this->keyValueStore->get('disable_rendered_output_cache_bins', FALSE); + $disable_rendered_output_cache_bins_previous = $this->keyValueStore->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->keyValueStore->set('disable_rendered_output_cache_bins', TRUE); -- GitLab From c57df3c9f82117e22b05741073f075bcba106514 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20L=C3=B3pez=20//=20plopesc?= Date: Wed, 3 Apr 2024 10:09:44 +0200 Subject: [PATCH 3/5] Issue #3437162: Post Update hook --- core/modules/system/system.post_update.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/core/modules/system/system.post_update.php b/core/modules/system/system.post_update.php index 13b15470f402..6639bcbaecea 100644 --- a/core/modules/system/system.post_update.php +++ b/core/modules/system/system.post_update.php @@ -223,3 +223,15 @@ function system_post_update_set_cron_logging_setting_to_boolean(): void { $config->set('logging', (bool) $logging)->save(); } } + +/** + * Move development settings from state to raw key-value storage. + */ +function system_post_update_move_development_settings_to_keyvalue(): void { + $state = \Drupal::state()->getMultiple([ + 'twig_debug', + 'twig_cache_disable', + 'disable_rendered_output_cache_bins', + ]); + \Drupal::keyValue('development_settings')->setMultiple($state); +} -- GitLab From 6de7841b6e54b40f5ef6ac58f4517ddbd845edf2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20L=C3=B3pez=20//=20plopesc?= Date: Thu, 4 Apr 2024 14:12:59 +0200 Subject: [PATCH 4/5] Issue #3437162: Get rid of keyValueStore class property --- .../src/Form/DevelopmentSettingsForm.php | 29 +++++++------------ 1 file changed, 11 insertions(+), 18 deletions(-) diff --git a/core/modules/system/src/Form/DevelopmentSettingsForm.php b/core/modules/system/src/Form/DevelopmentSettingsForm.php index 5532eac2d2d8..efa6ec332003 100644 --- a/core/modules/system/src/Form/DevelopmentSettingsForm.php +++ b/core/modules/system/src/Form/DevelopmentSettingsForm.php @@ -6,7 +6,6 @@ use Drupal\Core\Form\FormBase; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\KeyValueStore\KeyValueFactoryInterface; -use Drupal\Core\KeyValueStore\KeyValueStoreInterface; use Symfony\Component\DependencyInjection\ContainerInterface; /** @@ -16,13 +15,6 @@ */ class DevelopmentSettingsForm extends FormBase { - /** - * Development settings key-value storage. - * - * @var \Drupal\Core\KeyValueStore\KeyValueStoreInterface - */ - protected KeyValueStoreInterface $keyValueStore; - /** * Constructs a new development settings form. * @@ -35,7 +27,6 @@ public function __construct( protected KeyValueFactoryInterface $keyValueFactory, protected DrupalKernelInterface $kernel ) { - $this->keyValueStore = $this->keyValueFactory->get('development_settings'); } /** @@ -61,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->keyValueStore->get('twig_debug', FALSE); - $twig_cache_disable = $this->keyValueStore->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, @@ -110,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->keyValueStore->get('disable_rendered_output_cache_bins', FALSE), + '#default_value' => $development_settings->get('disable_rendered_output_cache_bins', FALSE), ]; $form['actions']['#type'] = 'actions'; @@ -127,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->keyValueStore->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->keyValueStore->set('disable_rendered_output_cache_bins', TRUE); + $development_settings->set('disable_rendered_output_cache_bins', TRUE); } else { - $this->keyValueStore->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->keyValueStore->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->keyValueStore->setMultiple($twig_development); + $development_settings->setMultiple($twig_development); } else { $invalidate_container = TRUE; - $this->keyValueStore->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) { -- GitLab From 5b25d6540d04bf5f3dca2326261403d73476bc62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20L=C3=B3pez=20//=20plopesc?= Date: Fri, 5 Apr 2024 06:48:06 +0200 Subject: [PATCH 5/5] Issue #3437162: Remove obsolete state entries --- core/modules/system/system.post_update.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/core/modules/system/system.post_update.php b/core/modules/system/system.post_update.php index da01839cbda7..952f25d9ede3 100644 --- a/core/modules/system/system.post_update.php +++ b/core/modules/system/system.post_update.php @@ -237,10 +237,12 @@ function system_post_update_sdc_uninstall() { * Move development settings from state to raw key-value storage. */ function system_post_update_move_development_settings_to_keyvalue(): void { - $state = \Drupal::state()->getMultiple([ + $state = \Drupal::state(); + $development_settings = $state->getMultiple([ 'twig_debug', 'twig_cache_disable', 'disable_rendered_output_cache_bins', ]); - \Drupal::keyValue('development_settings')->setMultiple($state); + \Drupal::keyValue('development_settings')->setMultiple($development_settings); + $state->deleteMultiple(array_keys($development_settings)); } -- GitLab