diff --git a/modules/custom/d8_night/config/install/d8_night.settings.yml b/modules/custom/d8_night/config/install/d8_night.settings.yml index 32462791..ea928e28 100644 --- a/modules/custom/d8_night/config/install/d8_night.settings.yml +++ b/modules/custom/d8_night/config/install/d8_night.settings.yml @@ -1 +1 @@ -theme: 'slate' +cdn_theme: 'slate' diff --git a/modules/custom/d8_night/config/schema/d8_night.schema.yml b/modules/custom/d8_night/config/schema/d8_night.schema.yml index 40ac371c..0569f4be 100644 --- a/modules/custom/d8_night/config/schema/d8_night.schema.yml +++ b/modules/custom/d8_night/config/schema/d8_night.schema.yml @@ -4,6 +4,6 @@ d8_night.settings: type: config_object label: 'D8+ Night settings' mapping: - text: + cdn_theme: type: string label: 'Bootstrap CDN theme for dark mode' diff --git a/modules/custom/d8_night/d8_night.module b/modules/custom/d8_night/d8_night.module index bb1ff3c9..6d270ae4 100644 --- a/modules/custom/d8_night/d8_night.module +++ b/modules/custom/d8_night/d8_night.module @@ -8,6 +8,7 @@ use Drupal\bootstrap\Bootstrap; use Drupal\Core\Block\BlockPluginInterface; use Drupal\Core\Routing\RouteMatchInterface; +use Drupal\d8_night\Form\D8NightForm; /** * Implements hook_block_view_BASE_BLOCK_ID_alter(). @@ -18,8 +19,8 @@ function d8_night_block_view_dark_mode_switch_block_alter( ) { $build['#attached']['library'][] = 'd8_night/base'; - $active_theme = Bootstrap::getTheme('d8_theme')->getSetting('cdn_theme'); - $needed_theme = \Drupal::config('d8_night.settings')->get('theme'); + $active_theme = Bootstrap::getTheme('d8_theme')->getSetting(D8NightForm::NAME); + $needed_theme = \Drupal::config('d8_night.settings')->get(D8NightForm::NAME); $build['#attached']['drupalSettings']['d8_night'] = $active_theme === $needed_theme; } diff --git a/modules/custom/d8_night/src/Controller/D8NightController.php b/modules/custom/d8_night/src/Controller/D8NightController.php index 6c20ac52..11e906d0 100644 --- a/modules/custom/d8_night/src/Controller/D8NightController.php +++ b/modules/custom/d8_night/src/Controller/D8NightController.php @@ -4,6 +4,7 @@ use Drupal\bootstrap\Bootstrap; use Drupal\Core\Controller\ControllerBase; +use Drupal\d8_night\Form\D8NightForm; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\HttpFoundation\JsonResponse; @@ -12,6 +13,13 @@ */ class D8NightController extends ControllerBase { + /** + * The cache tags invalidator. + * + * @var \Drupal\Core\Cache\CacheTagsInvalidatorInterface + */ + private $invalidator; + /** * {@inheritdoc} */ @@ -19,6 +27,7 @@ public static function create(ContainerInterface $container) { $instance = parent::create($container); $instance->configFactory = $container->get('config.factory'); + $instance->invalidator = $container->get('cache_tags.invalidator'); return $instance; } @@ -36,13 +45,21 @@ public static function create(ContainerInterface $container) { * The JSON response. */ public function switch($mode) { - $theme = Bootstrap::getTheme('d8_theme'); - $sub_theme = $this->config('d8_night.settings')->get('theme'); - $was = $theme->getSetting('cdn_theme') === $sub_theme; + $settings = ($theme = Bootstrap::getTheme('d8_theme'))->settings(); + $sub_theme = $this->config('d8_night.settings')->get(D8NightForm::NAME); + $was = $settings->get(D8NightForm::NAME) === $sub_theme; if ($update = ($now = !empty($mode)) !== $was) { - $theme->setSetting('cdn_theme', $now ? $sub_theme : 'bootstrap'); - drupal_flush_all_caches(); + $settings + ->set(D8NightForm::NAME, $now ? $sub_theme : 'bootstrap') + ->clear('cdn_cache') + ->save(); + + if ($tags = $theme->getSettingPlugin(D8NightForm::NAME)->getCacheTags()) { + $this->invalidator->invalidateTags($tags); + } + + $theme->getCache('settings')->deleteAll(); } return new JsonResponse(['update' => $update]); diff --git a/modules/custom/d8_night/src/Form/D8NightForm.php b/modules/custom/d8_night/src/Form/D8NightForm.php index 4b870065..89eadb50 100644 --- a/modules/custom/d8_night/src/Form/D8NightForm.php +++ b/modules/custom/d8_night/src/Form/D8NightForm.php @@ -2,14 +2,21 @@ namespace Drupal\d8_night\Form; +use Drupal\bootstrap\Bootstrap; use Drupal\Core\Form\ConfigFormBase; use Drupal\Core\Form\FormStateInterface; +use Drupal\Core\Render\Element; /** * Configure D8+ Night settings for this site. */ class D8NightForm extends ConfigFormBase { + /** + * The Bootstrap CDN theme setting name. + */ + const NAME = 'cdn_theme'; + /** * {@inheritdoc} */ @@ -28,14 +35,18 @@ public function getFormId() { * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state) { - $form['theme'] = [ - '#type' => 'select', - '#title' => $this->t('Theme'), - '#description' => $this->t('Bootstrap CDN theme for dark mode.'), - '#options' => [], - '#default_value' => $this->config($this->getEditableConfigNames()[0]) - ->get('theme'), - ]; + Bootstrap::getTheme('d8_theme')->getSettingPlugin(self::NAME) + ->alterForm( + $form, + $form_state->setValue( + self::NAME, + $this->config($this->getEditableConfigNames()[0])->get(self::NAME) + ) + ); + + foreach (Element::children($form) as $key) { + $form[$key]['#type'] = 'container'; + } return parent::buildForm($form, $form_state); } @@ -45,7 +56,7 @@ public function buildForm(array $form, FormStateInterface $form_state) { */ public function submitForm(array &$form, FormStateInterface $form_state) { $this->config($this->getEditableConfigNames()[0]) - ->set('theme', $form_state->getValue('theme')) + ->set(self::NAME, $form_state->getValue(self::NAME)) ->save(); parent::submitForm($form, $form_state);