I’d like to create a module capable of overriding some of the theme’s css (for any theme). Think of small things like hiding the image, changing font sizes and colors, etc.
My initial thought was to toggle this using a session variable:
/** * Implements hook_library_info_build(). */ function my_module_library_info_build() { $libraries = []; $libraries['override'] = [ 'version' => '1.0', 'css' => [ 'base' => [ 'override.css' => [], ], ], ]; return $libraries; } /** * Implements hook_preprocess_HOOK(). */ function my_module_preprocess_page(&$variables) { if ($_SESSION['override']['enabled']) { $variables['#attached']['library'][] = 'my_module/override'; } }
This works very well for logged in users, but for anonymous users it only caches the first version of the page (using the status of $_SESSION['override']['enabled']
at that time). So some pages show the regular lay-out, and some only show the overridden version.
I realized that I probably have to add a cache tag, so I tried adding
$variables['page']['#cache']['tags'][] = 'override:' . $_SESSION['override']['enabled'];
Assuming this would allow 2 versions of the page to be cached, either for override
enabled or disabled. But that does not work either.
What needs to be done to allow multiple versions of the same page to be cached and served, depending on a $_SESSION
variable?