@@ -120,43 +120,40 @@ like this:
120
120
121
121
// config/packages/workflow.php
122
122
use App\Entity\BlogPost;
123
-
124
- $container->loadFromExtension('framework', [
125
- 'workflows' => [
126
- 'blog_publishing' => [
127
- 'type' => 'workflow', // or 'state_machine'
128
- 'audit_trail' => [
129
- 'enabled' => true
130
- ],
131
- 'marking_store' => [
132
- 'type' => 'method',
133
- 'property' => 'currentPlace',
134
- ],
135
- 'supports' => [BlogPost::class],
136
- 'initial_marking' => 'draft',
137
- 'places' => [
138
- 'draft',
139
- 'reviewed',
140
- 'rejected',
141
- 'published',
142
- ],
143
- 'transitions' => [
144
- 'to_review' => [
145
- 'from' => 'draft',
146
- 'to' => 'reviewed',
147
- ],
148
- 'publish' => [
149
- 'from' => 'reviewed',
150
- 'to' => 'published',
151
- ],
152
- 'reject' => [
153
- 'from' => 'reviewed',
154
- 'to' => 'rejected',
155
- ],
156
- ],
157
- ],
158
- ],
159
- ]);
123
+ use Symfony\Config\FrameworkConfig;
124
+
125
+ return static function (FrameworkConfig $framework) {
126
+ $blogPublishing = $framework->workflows()->workflows('blog_publishing');
127
+ $blogPublishing
128
+ ->type('workflow') // or 'state_machine'
129
+ ->supports([BlogPost::class])
130
+ ->initialMarking(['draft']);
131
+
132
+ $blogPublishing->auditTrail()->enabled(true);
133
+ $blogPublishing->markingStore()
134
+ ->type('method')
135
+ ->property('currentPlace');
136
+
137
+ $blogPublishing->place()->name('draft');
138
+ $blogPublishing->place()->name('reviewed');
139
+ $blogPublishing->place()->name('rejected');
140
+ $blogPublishing->place()->name('published');
141
+
142
+ $blogPublishing->transition()
143
+ ->name('to_review')
144
+ ->from(['draft'])
145
+ ->to(['reviewed']);
146
+
147
+ $blogPublishing->transition()
148
+ ->name('publish')
149
+ ->from(['reviewed'])
150
+ ->to(['published']);
151
+
152
+ $blogPublishing->transition()
153
+ ->name('reject')
154
+ ->from(['reviewed'])
155
+ ->to(['rejected']);
156
+ };
160
157
161
158
.. tip ::
162
159
@@ -553,23 +550,25 @@ to :ref:`Guard events <workflow-usage-guard-events>`, which are always fired:
553
550
.. code-block :: php
554
551
555
552
// config/packages/workflow.php
556
- $container->loadFromExtension('framework', [
553
+ use Symfony\Config\FrameworkConfig;
554
+
555
+ return static function (FrameworkConfig $framework) {
557
556
// ...
558
- 'workflows' => [
559
- 'blog_publishing' => [
560
- // you can pass one or more event names
561
- 'events_to_dispatch' => [
562
- 'workflow.leave',
563
- 'workflow.completed',
564
- ] ,
565
-
566
- // pass an empty array to not dispatch any event
567
- 'events_to_dispatch' => [],
568
-
569
- // ...
570
- ],
571
- ],
572
- ]) ;
557
+
558
+ $blogPublishing = $framework->workflows()->workflows( 'blog_publishing');
559
+
560
+ // ...
561
+ // you can pass one or more event names
562
+ $blogPublishing->eventsToDispatch([
563
+ 'workflow.leave' ,
564
+ 'workflow.completed',
565
+ ]);
566
+
567
+ // pass an empty array to not dispatch any event
568
+ $blogPublishing->eventsToDispatch([]);
569
+
570
+ // ...
571
+ } ;
573
572
574
573
You can also disable a specific event from being fired when applying a transition::
575
574
@@ -731,36 +730,33 @@ transition. The value of this option is any valid expression created with the
731
730
.. code-block :: php
732
731
733
732
// config/packages/workflow.php
734
- use App\Entity\BlogPost;
735
-
736
- $container->loadFromExtension('framework', [
737
- 'workflows' => [
738
- 'blog_publishing' => [
739
- // ... previous configuration
740
-
741
- 'transitions' => [
742
- 'to_review' => [
743
- // the transition is allowed only if the current user has the ROLE_REVIEWER role.
744
- 'guard' => 'is_granted("ROLE_REVIEWER")',
745
- 'from' => 'draft',
746
- 'to' => 'reviewed',
747
- ],
748
- 'publish' => [
749
- // or "is_anonymous", "is_remember_me", "is_fully_authenticated", "is_granted"
750
- 'guard' => 'is_authenticated',
751
- 'from' => 'reviewed',
752
- 'to' => 'published',
753
- ],
754
- 'reject' => [
755
- // or any valid expression language with "subject" referring to the post
756
- 'guard' => 'is_granted("ROLE_ADMIN") and subject.isStatusReviewed()',
757
- 'from' => 'reviewed',
758
- 'to' => 'rejected',
759
- ],
760
- ],
761
- ],
762
- ],
763
- ]);
733
+ use Symfony\Config\FrameworkConfig;
734
+
735
+ return static function (FrameworkConfig $framework) {
736
+ $blogPublishing = $framework->workflows()->workflows('blog_publishing');
737
+ // ... previous configuration
738
+
739
+ $blogPublishing->transition()
740
+ ->name('to_review')
741
+ // the transition is allowed only if the current user has the ROLE_REVIEWER role.
742
+ ->guard('is_granted("ROLE_REVIEWER")')
743
+ ->from(['draft'])
744
+ ->to(['reviewed']);
745
+
746
+ $blogPublishing->transition()
747
+ ->name('publish')
748
+ // or "is_anonymous", "is_remember_me", "is_fully_authenticated", "is_granted"
749
+ ->guard('is_authenticated')
750
+ ->from(['reviewed'])
751
+ ->to(['published']);
752
+
753
+ $blogPublishing->transition()
754
+ ->name('reject')
755
+ // or any valid expression language with "subject" referring to the post
756
+ ->guard('is_granted("ROLE_ADMIN") and subject.isStatusReviewed()')
757
+ ->from(['reviewed'])
758
+ ->to(['rejected']);
759
+ };
764
760
765
761
You can also use transition blockers to block and return a user-friendly error
766
762
message when you stop a transition from happening.
@@ -945,42 +941,43 @@ be only the title of the workflow or very complex objects:
945
941
.. code-block :: php
946
942
947
943
// config/packages/workflow.php
948
- $container->loadFromExtension('framework', [
944
+ use Symfony\Config\FrameworkConfig;
945
+
946
+ return static function (FrameworkConfig $framework) {
947
+ $blogPublishing = $framework->workflows()->workflows('blog_publishing');
948
+ // ... previous configuration
949
+
950
+ $blogPublishing->metadata([
951
+ 'title' => 'Blog Publishing Workflow'
952
+ ]);
953
+
949
954
// ...
950
- 'workflows' => [
951
- 'blog_publishing' => [
952
- 'metadata' => [
953
- 'title' => 'Blog Publishing Workflow',
954
- ],
955
- // ...
956
- 'places' => [
957
- 'draft' => [
958
- 'metadata' => [
959
- 'max_num_of_words' => 500,
960
- ],
961
- ],
962
- // ...
963
- ],
964
- 'transitions' => [
965
- 'to_review' => [
966
- 'from' => 'draft',
967
- 'to' => 'review',
968
- 'metadata' => [
969
- 'priority' => 0.5,
970
- ],
971
- ],
972
- 'publish' => [
973
- 'from' => 'reviewed',
974
- 'to' => 'published',
975
- 'metadata' => [
976
- 'hour_limit' => 20,
977
- 'explanation' => 'You can not publish after 8 PM.',
978
- ],
979
- ],
980
- ],
981
- ],
982
- ],
983
- ]);
955
+
956
+ $blogPublishing->place()
957
+ ->name('draft')
958
+ ->metadata([
959
+ 'max_num_of_words' => 500,
960
+ ]);
961
+
962
+ // ...
963
+
964
+ $blogPublishing->transition()
965
+ ->name('to_review')
966
+ ->from(['draft'])
967
+ ->to(['reviewed'])
968
+ ->metadata([
969
+ 'priority' => 0.5,
970
+ ]);
971
+
972
+ $blogPublishing->transition()
973
+ ->name('publish')
974
+ ->from(['reviewed'])
975
+ ->to(['published'])
976
+ ->metadata([
977
+ 'hour_limit' => 20,
978
+ 'explanation' => 'You can not publish after 8 PM.',
979
+ ]);
980
+ };
984
981
985
982
Then you can access this metadata in your controller as follows::
986
983
0 commit comments