Skip to content

Commit

Permalink
bug #16252 [AbstractStateMachine] Add twig extension (Wojdylak)
Browse files Browse the repository at this point in the history
This PR was merged into the 1.13 branch.

Discussion
----------

| Q               | A
|-----------------|-----
| Branch?         | 1.13
| Bug fix?        | yes
| New feature?    | no
| BC breaks?      | no
| Deprecations?   | yes
| Related tickets | N/A
| License         | MIT

Commits
-------

13f76ec [AbstractionStateMachine] Add twig extension
737da6e Replace sm_can function to sylius_sm_can function
  • Loading branch information
GSadee committed May 15, 2024
2 parents 3e77be2 + 737da6e commit 383e573
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 5 deletions.
3 changes: 3 additions & 0 deletions src/Sylius/Abstraction/StateMachine/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
"winzou/state-machine": "^0.4",
"winzou/state-machine-bundle": "^0.6"
},
"suggest": {
"twig/twig": "Access the state machine in your twig templates (^2.12|^3.3)"
},
"require-dev": {
"matthiasnoback/symfony-config-test": "^4.2",
"phpunit/phpunit": "^9.5",
Expand Down
5 changes: 5 additions & 0 deletions src/Sylius/Abstraction/StateMachine/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,10 @@
<tag name="sylius.state_machine" key="winzou_state_machine" />
</service>
<service id="Sylius\Abstraction\StateMachine\StateMachineInterface $winzouStateMachine" alias="sylius_abstraction.state_machine.adapter.winzou_state_machine" />

<service id="sylius_abstraction.twig.extension.state_machine" class="Sylius\Abstraction\StateMachine\Twig\StateMachineExtension">
<argument type="service" id="sylius_abstraction.state_machine" />
<tag name="twig.extension" />
</service>
</services>
</container>
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Sylius Sp. z o.o.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Sylius\Abstraction\StateMachine\Twig;

use Sylius\Abstraction\StateMachine\StateMachineInterface;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;

final class StateMachineExtension extends AbstractExtension
{
public function __construct(private readonly StateMachineInterface $stateMachine)
{
}

public function getFunctions(): array
{
return [
new TwigFunction('sylius_sm_can', $this->stateMachine->can(...)),
new TwigFunction('sylius_sm_transitions', $this->stateMachine->getEnabledTransitions(...)),
];
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% if sm_can(payment, 'complete', 'sylius_payment') %}
{% if sylius_sm_can(payment, 'sylius_payment', 'complete') %}
<div class="ui segment">
<form action="{{ path('sylius_admin_order_payment_complete', {'orderId': order.id, 'id': payment.id}) }}" method="post" novalidate>
<input type="hidden" name="_csrf_token" value="{{ csrf_token(payment.id) }}" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% if sm_can(payment, 'refund', 'sylius_payment') %}
{% if sylius_sm_can(payment, 'sylius_payment', 'refund') %}
<div class="ui segment">
<form action="{{ path('sylius_admin_order_payment_refund', {'orderId': order.id, 'id': payment.id}) }}" method="post" novalidate>
<input type="hidden" name="_csrf_token" value="{{ csrf_token(payment.id) }}" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{% if sm_can(shipment, 'ship', 'sylius_shipment') %}
{% if sylius_sm_can(shipment, 'sylius_shipment', 'ship') %}
{{ render(path('sylius_admin_partial_shipment_ship', {'orderId': order.id, 'id': shipment.id})) }}
{% endif %}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% if sm_can(data, 'ship', 'sylius_shipment') %}
{% if sylius_sm_can(data, 'sylius_shipment', 'ship') %}
{{ render(controller('sylius.controller.shipment::updateAction', {
'_sylius': {
'event': 'ship',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{% set labeled = options.labeled is defined ? options.labeled : true %}

{% if sm_can(data, options.transition, options.graph) %}
{% if sylius_sm_can(data, options.graph, options.transition) %}
<form action="{{ path(options.link.route, options.link.parameters) }}" method="post">
<input type="hidden" name="_csrf_token" value="{{ csrf_token(data.id) }}">
<input type="hidden" name="_method" value="PUT">
Expand Down

0 comments on commit 383e573

Please sign in to comment.