Skip to content

[TwigComponent] Fix internal variables are dispatched #2220

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

Merged
merged 1 commit into from
Sep 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 19 additions & 20 deletions src/TwigComponent/src/ComponentRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,29 +112,28 @@ private function preRender(MountedComponent $mounted, array $context = []): PreR
$classProps = $isAnonymous ? [] : iterator_to_array($this->exposedVariables($component, $metadata->isPublicPropsExposed()));

// expose public properties and properties marked with ExposeInTemplate attribute
$props = array_merge($mounted->getInputProps(), $classProps);
$variables = array_merge(
// first so values can be overridden
$context,
$props = [...$mounted->getInputProps(), ...$classProps];
$event = new PreRenderEvent($mounted, $metadata, [
...$context,
...$props,
$metadata->getAttributesVar() => $mounted->getAttributes(),
]);

$this->dispatcher->dispatch($event);

$event->setVariables([
...$event->getVariables(),
// add the component as "this"
'this' => $component,
'computed' => new ComputedPropertiesProxy($component),
'outerScope' => $context,
// keep this line for BC break reasons
'__props' => $classProps,
// add the context in a separate variable to keep track
// of what is coming from outside the component, excluding props
// as they override initial context values
['__context' => array_diff_key($context, $props)],
// keep reference to old context
['outerScope' => $context],
// add the component as "this"
['this' => $component],
// add computed properties proxy
['computed' => new ComputedPropertiesProxy($component)],
$props,
// keep this line for BC break reasons
['__props' => $classProps],
// add attributes
[$metadata->getAttributesVar() => $mounted->getAttributes()],
);
$event = new PreRenderEvent($mounted, $metadata, $variables);

$this->dispatcher->dispatch($event);
'__context' => array_diff_key($context, $props),
]);

return $event;
}
Expand Down
Loading