Skip to content

[TwigHooks] Keep the hookable context of live components across re-renders - #400

Open
loic425 wants to merge 1 commit into
Sylius:mainfrom
loic425:twig-hooks-live-context
Open

loic425 wants to merge 1 commit into
Sylius:mainfrom
loic425:twig-hooks-live-context

Conversation

@loic425

@loic425 loic425 commented Nov 2, 2026

Copy link
Copy Markdown
Member

Problem

HookableLiveComponentTrait dehydrates the hookableMetadata LiveProp to the hook name, the configuration and the prefixes only. On a live re-render, the hookable context is empty:

  • hookable_metadata.context.* is empty in the component template;
  • nested {% hook %} calls don't get the parent context;
  • the conditions and @= expressions of the nested hookables see an empty _context.

The context can't simply be serialized into the LiveProp: it usually holds objects (grid views, resource metadata, entities, forms), which are too large for the payload and must not be readable by the client.

Solution

Two overridable methods on the trait. Both do nothing by default, so there's no BC break, and the payload only changes when a component lists keys:

/** @return array<string> scalars, nulls or arrays of them; sent to the client */
protected function getDehydratedHookableContextKeys(): array { return []; }

/** @return array<string, mixed> merged on top of the hookable context at render time */
protected function provideHookableContext(): array { return []; }
  • provideHookableContext() is applied at render time, on the first render and on every re-render, through #[ExposeInTemplate(getter: ...)]. It runs after the live actions, and its values are never dehydrated. Use it to give back objects built from the component's own LiveProps.
  • getDehydratedHookableContextKeys() keeps plain values in the LiveProp. The LiveProp isn't writable, so the checksum covers them. Values that aren't scalars, nulls or arrays of them throw a LogicException.
  • Adds HookableMetadata::withContext().

Example, from the Sylius admin live grid (Sylius/Sylius#19289):

protected function provideHookableContext(): array
{
    return [
        'resources' => $this->getGridView(),
        'metadata' => $this->getMetadata(),
        'live_grid' => true,
        'are_criteria_set' => $this->hasActiveCriteria(),
    ];
}

With this, the component template goes from {% hook 'grid' with { resources: grid, metadata, live_grid: true, are_criteria_set: this.hasActiveCriteria() } %} to {% hook 'grid' %}.

Notes

  • The DataBagHydrationExtension (unregistered) is left untouched: it would dehydrate the whole bag, objects included.
  • Tests: unit tests for the trait, plus a functional test that does a real re-render with InteractsWithLiveComponents. That's why symfony/browser-kit and symfony/css-selector are added to the Twig Hooks require-dev; the root composer.json already has both.

…nders

The hookable context was dropped when dehydrating the HookableMetadata LiveProp,
so the component template, its nested hooks and their conditions saw an empty
context during live re-renders.

- getDehydratedHookableContextKeys(): opt-in list of scalar context values kept in the LiveProp
- provideHookableContext(): values (objects included) given back by the component at render time
- HookableMetadata::withContext()
loic425 added a commit to loic425/Sylius that referenced this pull request Nov 2, 2026
The live grid component gives the grid view, the resource metadata, the live flag and the active criteria
back to its hookables with provideHookableContext() (Sylius/Stack#400), so the template does not have to
pass them again to the "grid" hook.

Requires the twig-hooks-live-context branch of Sylius/Stack (not released yet).
@loic425
loic425 marked this pull request as ready for review November 2, 2026 14:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant