GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [TwigHooks] Keep the hookable context of live components across re-renders by loic425 · Pull Request #400 · Sylius/Stack

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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@loic425
[TwigHooks] Keep the hookable context of live components across re-re…
b670bcd
…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 loic425 mentioned this pull request Nov 2, 2026
[Shop][Admin] Add experimental live grids Sylius/Sylius#19289
Draft
loic425 added a commit to loic425/Sylius that referenced this pull request Nov 2, 2026
@loic425
[Admin] Provide the hook context of the live grid on re-renders
8245a7c
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

Reviewers

No reviews

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

1 participant

@loic425
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.