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

Use current Twig vars on HooksRuntime when no only option has been pa… #114

Merged
merged 1 commit into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
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
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
Loading