بنقرة واحدة
add-twig-context
Add extra variables to the Twig template of an operation — related data, stats, preloaded references.
التثبيت باستخدام Codex أو Claude انسخ هذا Prompt والصقه في Codex أو Claude أو مساعد آخر ليراجع صفحة Skill ويثبّتها لك.
القائمة
Add extra variables to the Twig template of an operation — related data, stats, preloaded references.
التثبيت باستخدام Codex أو Claude انسخ هذا Prompt والصقه في Codex أو Claude أو مساعد آخر ليراجع صفحة Skill ويثبّتها لك.
استنادا إلى تصنيف SOC المهني
Implement the fix described in a GitHub issue and open a Pull Request that closes it
Apply the latest review feedback to a Pull Request and push it to the PR branch
Add an admin autocomplete form type for a Sylius Resource (translatable or not), optionally injected into another form via an extension
Add an admin FormType for an existing Sylius Resource
Add a Sylius admin grid for an existing Sylius Resource
Add a multiple images collection (OneToMany) to an existing Sylius Resource
| name | add-twig-context |
| description | Add extra variables to the Twig template of an operation — related data, stats, preloaded references. |
| argument-hint | [ModelName] [Operation] |
| allowed-tools | AskUserQuestion, Bash, Read, Edit, Write, Glob, Grep |
Ask the user if not provided:
ModelName) and the target operation (typically Show, Index, Create, Update).{Operation}{ModelName}ContextFactory (e.g. ShowBookContextFactory).If the user only needs to change the template path of an operation, do not create a factory — use the template: option on the operation instead:
new Show(template: 'book/custom_show.html.twig')
src/Twig/Context/Factory/{FactoryName}.php
<?php
declare(strict_types=1);
namespace App\Twig\Context\Factory;
use Sylius\Resource\Context\Context;
use Sylius\Resource\Metadata\Operation;
use Sylius\Resource\Twig\Context\Factory\ContextFactoryInterface;
final class {FactoryName} implements ContextFactoryInterface
{
public function __construct(
private ContextFactoryInterface $decorated,
// TODO: inject the services used to build your extra variables (repositories, services…)
) {
}
public function create(mixed $data, Operation $operation, Context $context): array
{
return array_merge($this->decorated->create($data, $operation, $context), [
// TODO: add your extra variables here, e.g.:
// 'related_books' => $this->bookRepository->findRelatedTo($data),
// 'stats' => $this->statsService->for($data),
]);
}
}
The decorated default factory is kept in the constructor so the existing context (resource, {model_snake}, form, operation, resource_metadata, …) remains available.
Edit src/Entity/{ModelName}.php:
use App\Twig\Context\Factory\{FactoryName};
new Show(twigContextFactory: {FactoryName}::class),
If the operation uses the default @SyliusAdminUi/crud templates, surface the new variables through a Twig Hook hookable — see /sylius-stack:add-template. Example, inside a hookable:
{% set related_books = hookable_metadata.context.related_books %}
<ul>
{% for book in related_books %}
<li>{{ book.title }}</li>
{% endfor %}
</ul>
If the operation uses a custom template (template: '...'), the variables are accessible directly by name in that Twig file.
bin/console cache:clear
bin/console sylius:debug:resource 'App\Entity\{ModelName}'
The operation's twigContextFactory row should display your class FQCN.
template: option on the operation, no factory needed./sylius-stack:add-template./sylius-stack:add-responder.