The Service alias is required for auto-wiring via constructor annotation.
Using the context helper allows us to change the body class options during runtime.
Use-Case: I added a EventSubscriber that listen on the kernel.controller even, which dynamically loads the skin and other options (like boxed_layout) from a user db. I used ContextHelper->setOption() but the options were not applied in the twig template.
Now in works like that (adapted for demo):
class UserPreferenceSubscriber implements EventSubscriberInterface
{
protected $storage;
protected $helper;
public function __construct(TokenStorageInterface $storage, ContextHelper $helper) {
$this->storage = $storage;
$this->helper = $helper;
}
public static function getSubscribedEvents(): array
{
return [KernelEvents::CONTROLLER => [['loadPreferences', 200]]];
}
public function loadPreferences(KernelEvent $event): void
{
if (!$event->isMasterRequest() || $this->storage->getToken() === null) {
return;
}
/** @var User $user */
$user = $this->storage->getToken()->getUser();
$this->helper->setOption('skin', $user->getSkin());
$this->helper->setOption('boxed_layout', $user->isBoxedLayout());
}
}
Why? Currently in master the Twig helper gets a static copy of the options array, where another copy is injected into the ContextHelper. This builds the internal options from that copy & the user config but doesn't use it (at least not in the TwigExtensions helper).
I believe this is a programmatically bug or I completely misunderstood the ContextHelper?!?
Hi @shakaran ,
another PR with two non BC break changes:
The Service alias is required for auto-wiring via constructor annotation. Using the context helper allows us to change the body class options during runtime.
Use-Case: I added a EventSubscriber that listen on the
kernel.controller
even, which dynamically loads theskin
and other options (likeboxed_layout
) from a user db. I usedContextHelper->setOption()
but the options were not applied in the twig template.Now in works like that (adapted for demo):
Why? Currently in master the Twig helper gets a static copy of the options array, where another copy is injected into the ContextHelper. This builds the internal options from that copy & the user config but doesn't use it (at least not in the TwigExtensions helper).
I believe this is a programmatically bug or I completely misunderstood the ContextHelper?!?