avanzu / AdminThemeBundle

Admin Theme based on the AdminLTE Template for easy integration into symfony
MIT License
280 stars 148 forks source link

Allow dynamic option change during runtime #217

Open kevinpapst opened 6 years ago

kevinpapst commented 6 years ago

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 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?!?

kevinpapst commented 6 years ago

That should also fix #206

craph commented 6 years ago

When this PR will be merged ?

shakaran commented 6 years ago

@craph I have to spent some time to check if really the ContextHelper expect this behaviour in this part, not sure if it is the really use case