Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow dynamic option change during runtime #217

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Allow dynamic option change during runtime #217

wants to merge 1 commit into from

Conversation

kevinpapst
Copy link
Contributor

@kevinpapst kevinpapst commented Jan 15, 2018

Hi @shakaran ,

another PR with two non BC break changes:

  • using context helper to load body_class options
  • added service alias for auto-wiring

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

using context helper to load body_class options
@kevinpapst
Copy link
Contributor Author

That should also fix #206

@craph
Copy link

craph commented Mar 2, 2018

When this PR will be merged ?

@shakaran
Copy link
Collaborator

shakaran commented Mar 2, 2018

@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants