Skip to content

Add expression-based conditions to enable or disable Twig hookables - #385

Merged
loic425 merged 1 commit into
Sylius:mainfrom
loic425:twig-hookable-condition
Sep 25, 2026
Merged

loic425 merged 1 commit into
Sylius:mainfrom
loic425:twig-hookable-condition

Conversation

@loic425

@loic425 loic425 commented Sep 24, 2026

Copy link
Copy Markdown
Member

Introduce a new 'condition' configuration option for hookables. The value is an Expression Language expression prefixed with '@=' that is evaluated at runtime against the hook context. The hookable is rendered only when the expression returns true.

Conditions are linted at the application boot to fail fast on invalid expressions, and evaluated by a new HookableConditionChecker injected into the HookRenderer. 'enabled' remains a static boot-time flag, keeping the two mechanisms clearly separated.

@loic425
loic425 marked this pull request as draft September 24, 2026 10:10
Comment thread src/TwigHooks/src/Hookable/Checker/HookableConditionChecker.php Outdated
@loic425
loic425 force-pushed the twig-hookable-condition branch from f6e142f to 525d275 Compare September 24, 2026 12:37
Introduce a new 'condition' configuration option for hookables. The value is an Expression Language expression prefixed with '@=' that is evaluated at runtime against the hook context. The hookable is rendered only when the expression returns true.

Conditions are linted at the application boot to fail fast on invalid expressions, and evaluated by a new HookableConditionChecker injected into the HookRenderer. 'enabled' remains a static boot-time flag, keeping the two mechanisms clearly separated.
@loic425
loic425 force-pushed the twig-hookable-condition branch from 525d275 to fc2d8c0 Compare September 24, 2026 12:41
@loic425
loic425 marked this pull request as ready for review September 24, 2026 12:44
@loic425
loic425 merged commit b4dc974 into Sylius:main Sep 25, 2026
8 checks passed
@loic425
loic425 deleted the twig-hookable-condition branch September 25, 2026 06:45
{
$condition = $hookable['condition'] ?? null;

if (null === $condition || !defined(Parser::class . '::IGNORE_UNKNOWN_VARIABLES')) {

@diimpp diimpp Sep 25, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This defined early return essentially disables lint for older than SF 7.1.

::IGNORE_UNKNOWN_VARIABLES was introduced in 7.1 and only changes method signature, not behaviour. Previosly we should have supplied null for the same result.

So something like this instead of early return.

-            $expressionLanguage->lint(substr($condition, 2), [], Parser::IGNORE_UNKNOWN_VARIABLES);
+            $expression = substr($condition, 2);
+
+            if (defined(Parser::class . '::IGNORE_UNKNOWN_VARIABLES')) {
+                $expressionLanguage->lint(
+                    $expression,
+                    [],
+                    Parser::IGNORE_UNKNOWN_VARIABLES
+                );
+            } else {
+                $expressionLanguage->lint(
+                    $expression,
+                    null
+                );
+            }
Image

symfony/expression-language@684ac49#diff-1a7c1850f88287936713a7072af9331818e14581fc71feed100c41e1747593ceR37-L103

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, but I think we still need the "try/catch" to have a better error.

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.

4 participants