Skip to content

Commit

Permalink
Issue #219: Fill in the field values list on the settings form and ca…
Browse files Browse the repository at this point in the history
…ncel clearing redundant caches
  • Loading branch information
lexhouk committed Jan 1, 2023
1 parent 360dd84 commit 50181a4
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
theme: 'slate'
cdn_theme: 'slate'
2 changes: 1 addition & 1 deletion modules/custom/d8_night/config/schema/d8_night.schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
5 changes: 3 additions & 2 deletions modules/custom/d8_night/d8_night.module
Original file line number Diff line number Diff line change
Expand Up @@ -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().
Expand All @@ -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;
}

Expand Down
27 changes: 22 additions & 5 deletions modules/custom/d8_night/src/Controller/D8NightController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -12,13 +13,21 @@
*/
class D8NightController extends ControllerBase {

/**
* The cache tags invalidator.
*
* @var \Drupal\Core\Cache\CacheTagsInvalidatorInterface
*/
private $invalidator;

/**
* {@inheritdoc}
*/
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;
}
Expand All @@ -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]);
Expand Down
29 changes: 20 additions & 9 deletions modules/custom/d8_night/src/Form/D8NightForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -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}
*/
Expand All @@ -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);
}
Expand All @@ -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);
Expand Down

0 comments on commit 50181a4

Please sign in to comment.