Skip to content

Commit

Permalink
Use current Twig vars on HooksRuntime when no only option has been pa… (
Browse files Browse the repository at this point in the history
#114)

…ssed

Redo the changes reverted here
#72

We will release that on 0.5 (next release). That means we'll not break
Sylius shop, until we allow Twig Hooks "^0.5".
I've created a 0.4 branch to release our 0.4.1.

From my investigation, I've managed to fix one issue on Shop.

```twig
<!-- ShopBundle/templates/checkout/address/form/addresses/address/form.html.twig -->

{% hook 'address' with { _prefixes: prefixes, 'form': attribute(form, field) } only %}
```
  • Loading branch information
loic425 authored Oct 28, 2024
2 parents 491640c + 9cb87b7 commit b5c7107
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/TwigHooks/src/Twig/Node/HookNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function compile(Compiler $compiler): void
$compiler->raw(', ');
$compiler->subcompile($this->getNode('hook_level_context'));
$compiler->raw(', ');
$compiler->raw('$context["hookable_metadata"] ?? null');
$compiler->raw('$context');
$compiler->raw(', ');
$compiler->raw($this->getAttribute('only') ? 'true' : 'false');
$compiler->raw(");\n");
Expand Down
15 changes: 11 additions & 4 deletions src/TwigHooks/src/Twig/Runtime/HooksRuntime.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Sylius\TwigHooks\Hookable\Metadata\HookableMetadata;
use Twig\Error\RuntimeError;
use Twig\Extension\RuntimeExtensionInterface;
use Webmozart\Assert\Assert;

final class HooksRuntime implements RuntimeExtensionInterface
{
Expand Down Expand Up @@ -80,18 +81,23 @@ public function isHookable(array $context): bool

/**
* @param string|array<string> $hookNames
* @param array<string, mixed> $twigVars
* @param array<string, mixed> $hookContext
*/
public function renderHook(
string|array $hookNames,
array $hookContext = [],
?HookableMetadata $hookableMetadata = null,
array $twigVars = [],
bool $only = false,
): string {
$hookNames = is_string($hookNames) ? [$hookNames] : $hookNames;
$hookNames = array_map([$this->nameNormalizer, 'normalize'], $hookNames);

$context = $this->getContext($hookContext, $hookableMetadata, $only);
$hookableMetadata = $twigVars[self::HOOKABLE_METADATA] ?? null;
Assert::nullOrIsInstanceOf($hookableMetadata, HookableMetadata::class);
unset($twigVars[self::HOOKABLE_METADATA]);

$context = $this->getContext($hookContext, $twigVars, $hookableMetadata, $only);
$prefixes = $this->getPrefixes($hookContext, $hookableMetadata);

if (false === $this->enableAutoprefixing || [] === $prefixes) {
Expand Down Expand Up @@ -134,17 +140,18 @@ private function getPrefixes(array $hookContext, ?HookableMetadata $hookableMeta

/**
* @param array<string, mixed> $hookContext
* @param array<string, mixed> $twigVars
*
* @return array<string, mixed>
*/
private function getContext(array $hookContext, ?HookableMetadata $hookableMetadata, bool $only = false): array
private function getContext(array $hookContext, array $twigVars, ?HookableMetadata $hookableMetadata, bool $only = false): array
{
if ($only) {
return $hookContext;
}

$context = $hookableMetadata?->context->all() ?? [];

return array_merge($context, $hookContext);
return array_merge($twigVars, $context, $hookContext);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@

is "some" defined: {{ hookable_context.has('some') ? 'Yes' : 'No' }}
is "other" defined: {{ hookable_context.has('other') ? 'Yes' : 'No' }}
is "var in template" defined: {{ hookable_context.has('var_in_template') ? 'Yes' : 'No' }}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public function testItRendersSingleHookName(): void
is "some" defined: No
is "other" defined: No
is "var in template" defined: No
<!-- END HOOKABLE | hook: "restricting_context_scope.index.with_only", name: "some", template: "restricting_context_scope/index/block/some.html.twig", priority: 0 -->
<!-- END HOOK | name: "restricting_context_scope.index.with_only" -->
<!-- END HOOKABLE | hook: "restricting_context_scope.index", name: "with_only", template: "restricting_context_scope/index/with_only.html.twig", priority: 0 -->
Expand All @@ -43,6 +44,7 @@ public function testItRendersSingleHookName(): void
is "some" defined: Yes
is "other" defined: Yes
is "var in template" defined: Yes
<!-- END HOOKABLE | hook: "restricting_context_scope.index.without_only", name: "some", template: "restricting_context_scope/index/block/some.html.twig", priority: 0 -->
<!-- END HOOK | name: "restricting_context_scope.index.without_only" -->
<!-- END HOOKABLE | hook: "restricting_context_scope.index", name: "without_only", template: "restricting_context_scope/index/without_only.html.twig", priority: 0 -->
Expand Down

0 comments on commit b5c7107

Please sign in to comment.