Skip to content

[TwigHooks][RFC] improving the DX #167

@loic425

Description

@loic425

Before

namespace Symfony\Component\DependencyInjection\Loader\Configurator;

return static function (ContainerConfigurator $container): void {
    $container->extension('sylius_twig_hooks', [
        'hooks' => [
            'sylius_admin.book.show.content' => [
                'body' => [
                    'template' => 'library/book/show/content/body.html.twig',
                    'priority' => 100,
                ],
                 'dropdown' => [
                    'component' => DropdownComponent::class,
                    'priority' => 0,
                    'props' => [],
                ],
            ],
        ],
    ]);
};

After

namespace Symfony\Component\DependencyInjection\Loader\Configurator;

return static function (TwigHooksConfig $config): void {
    $config->withHook(
        (new Hook('sylius_admin.book.show.content'))
            ->withHookable(
                (new HookableTemplate('body', 'library/book/show/content/body.html.twig'))
                    ->withPriority(100)
            )     
            ->withHookable(
                (new HookableComponent('dropdown', DropdownComponent::class))
                    ->withProps([])
                    ->withPriority(0)
            )     
    );
};

Going further on resource operation
Then we could go further and configure them on a specific resource operation.

#[AsResource(
   //...
    operations: [
        new Show(provider: BookItemProvider::class, hooks: [
             (new Hook('sylius_admin.book.show.content'))
                ->withHookable(
                    (new HookableTemplate('body', 'library/book/show/content/body.html.twig'))
                        ->withPriority(100)
                )     
                ->withHookable(
                    (new HookableComponent('dropdown', DropdownComponent::class))
                        ->withProps([])
                        ->withPriority(0)
                ) 
            ]),
    ],
)]
class ShowBookResource implements ResourceInterface

Note: In userland, we do not need to configure a lot of Twig hooks for our custom operations.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions