Skip to content

[BootstrapAdminUi] add apply_transition Twig template#369

Open
stlgaits wants to merge 1 commit into
Sylius:mainfrom
stlgaits:apply-transition-template
Open

[BootstrapAdminUi] add apply_transition Twig template#369
stlgaits wants to merge 1 commit into
Sylius:mainfrom
stlgaits:apply-transition-template

Conversation

@stlgaits

@stlgaits stlgaits commented Jun 24, 2026

Copy link
Copy Markdown
Contributor
  • Add default apply transition (for state machines) Twig template.
  • Add sylius.ui.apply_transition translation in UI translation bundle.

@stlgaits stlgaits self-assigned this Jun 24, 2026
@stlgaits

Copy link
Copy Markdown
Contributor Author

I didn't add an example in test app here because that would've required to add Symfony Workflow.

However, here is a example in my current project :

Grid :

use Sylius\Bundle\GridBundle\Builder\Action\ApplyTransitionAction;

#[AsGrid(
    resourceClass: CareTask::class,
    name: 'app_care_task',
)]
final class CareTaskGrid extends AbstractGrid
{
    public function __invoke(GridBuilderInterface $gridBuilder): void
    {
        $gridBuilder  
            ->withFields(
                TwigField::create('status', template: 'care_task/grid/field/task_status.html.twig')
                    ->setSortable(true)
                    ->setLabel('Status'),
                    // ....
            )
            ->addActionGroup(
                ItemActionGroup::create(...$this->getItemActions())
            )
        ;
    }

    public function getItemActions(): iterable
    {
        yield ApplyTransitionAction::create(name: 'complete', route: 'app_care_task_complete')
            ->setTemplate('care_task/grid/action/apply_transition.html.twig')
            ->setIcon('lets-icons:done-round-duotone-line')
            ->setLabel('Complete');

        yield ApplyTransitionAction::create(name: 'skip', route: 'app_care_task_skip')
            ->setTemplate('care_task/grid/action/apply_transition.html.twig')
            ->setIcon('fluent:skip-forward-tab-20-filled')
            ->setLabel('Skip');

        yield ApplyTransitionAction::create(name: 'reschedule', route: 'app_care_task_reschedule')
            ->setTemplate('care_task/grid/action/apply_transition.html.twig')  
            ->setIcon('noto:hourglass-not-done')
            ->setLabel('Reschedule');
    }
}

Resource :

use Sylius\Resource\Metadata\ApplyStateMachineTransition;

#[AsResource(
    formType: CareTaskFormType::class,
    templatesDir: '@SyliusAdminUi/crud',
    routePrefix: 'admin',
    operations: [
        new Index(
            grid: CareTaskGrid::class,
        ),
        new ApplyStateMachineTransition(stateMachineTransition: 'complete'),
        new ApplyStateMachineTransition(stateMachineTransition: 'reschedule'), 
        new ApplyStateMachineTransition(stateMachineTransition: 'skip'),  
    ]
)]
#[ORM\Entity(repositoryClass: CareTaskRepository::class)]
class CareTask implements ResourceInterface
{
    // ...

    #[ORM\Column(enumType: Status::class)]
    private Status $status = Status::PENDING;
}
  • Symfony Workflow config of course:
return static function (ContainerConfigurator $containerConfigurator): void {
    $containerConfigurator->extension('framework', [
        'workflows' => [
            'care_task' => [
                'type' => 'state_machine',
                'initial_marking' => 'pending',
                'marking_store' => [
                    'property' => 'status',
                ],
                'supports' => [
                    CareTask::class,
                ],
                'audit_trail' => [
                    'enabled' => true,
                ],
                'places' => [
                    Status::PENDING->value,
                    Status::OVERDUE->value,
                    Status::RESCHEDULED->value,
                    Status::DONE->value,
                    Status::SKIPPED->value,
                ],
                'transitions' => [
                    'complete' => [
                        'from' => [Status::PENDING->value, Status::OVERDUE->value, Status::RESCHEDULED->value, Status::SKIPPED->value],
                        'to' => Status::DONE->value,
                    ],
                    'skip' => [
                        'from' => [Status::PENDING->value, Status::OVERDUE->value, Status::RESCHEDULED->value],
                        'to' => Status::SKIPPED->value,
                    ],
                    'reschedule' => [
                        'from' => [Status::PENDING->value, Status::OVERDUE->value],
                        'to' => Status::RESCHEDULED->value,
                    ],
                    'overdue' => [
                        'from' => [Status::PENDING->value, Status::RESCHEDULED->value],
                        'to' => Status::OVERDUE->value,
                    ],
                ],
            ],
        ],
    ]);
};

As a result, transition actions are only displayed if the transition is possible.
image

@stlgaits stlgaits marked this pull request as ready for review June 24, 2026 09:36
@stlgaits stlgaits force-pushed the apply-transition-template branch from 928a878 to b065d46 Compare June 24, 2026 12:59
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