@@ -43,7 +43,7 @@ like this:
43
43
audit_trail :
44
44
enabled : true
45
45
marking_store :
46
- type : ' multiple_state' # or 'single_state'
46
+ type : ' multiple_state' # or 'single_state', 'method' ('method' was added in 4.3)
57AE
span>
47
47
arguments :
48
48
- ' currentPlace'
49
49
supports :
@@ -127,7 +127,7 @@ like this:
127
127
'enabled' => true
128
128
],
129
129
'marking_store' => [
130
- 'type' => 'multiple_state', // or 'single_state'
130
+ 'type' => 'multiple_state', // or 'single_state', 'method' ('method' was added in 4.3)
131
131
'arguments' => ['currentPlace'],
132
132
],
133
133
'supports' => ['App\Entity\BlogPost'],
@@ -167,7 +167,7 @@ As configured, the following property is used by the marking store::
167
167
168
168
.. note ::
169
169
170
- The marking store type could be "multiple_state" or "single_state ".
170
+ The marking store type could be "multiple_state", "single_state" or "method ".
171
171
A single state marking store does not support a model being on multiple places
172
172
at the same time.
173
173
@@ -221,11 +221,87 @@ you can get the workflow by injecting the Workflow registry service::
221
221
// ... if the transition is not allowed
222
222
}
223
223
224
+ // Update the currentState on the post passing some contextual data
225
+ // to the whole workflow process
226
+ try {
227
+ $workflow->apply($post, 'publish', [
228
+ 'log_comment' => 'My logging comment for the publish transition.',
229
+ ]);
230
+ } catch (TransitionException $exception) {
231
+ // ... if the transition is not allowed
232
+ }
233
+
224
234
// See all the available transitions for the post in the current state
225
235
$transitions = $workflow->getEnabledTransitions($post);
226
236
}
227
237
}
228
238
239
+ .. versionadded :: 4.1
240
+
241
+ The :class: `Symfony\\ Component\\ Workflow\\ Exception\\ TransitionException `
242
+ class was introduced in Symfony 4.1.
243
+
244
+ .. versionadded :: 4.1
245
+
246
+ The :method: `Symfony\\ Component\\ Workflow\\ Registry::all ` method was
247
+ introduced in Symfony 4.1.
248
+
249
+ .. versionadded :: 4.3
250
+
251
+ The :method: `Symfony\\ Component\\ Workflow\\ Workflow::apply ` has now a new parameter ``$context ``
252
+ that is passed to the :class: `Symfony\\ Component\\ Workflow\\ MarkingStore\\ MarkingStoreInterface `
253
+ :method: `Symfony\\ Component\\ Workflow\\ MarkingStore\\ MarkingStoreInterface::setMarking ` method.
254
+
255
+ An example of usage with the ``$context `` parameter can be when you need,
256
+ in addition of marking your object in its new place, to contextualize this change.
257
+
258
+ .. tip ::
259
+
260
+ Configure the ``type `` as ``method `` of the ``marking_store `` option to use this feature
261
+ without implementing your own marking store.
262
+
263
+ You can also use this ``$context `` in your own marking store implementation.
264
+ A simple implementation example is when you want to store the place as integer instead of string in your object.
265
+
266
+ Lets say your object has a status property, stored as an integer in your storage, and you want to log an optional
267
+ comment any time the status changes::
268
+
269
+ // your own implementation class, to define in the configuration "marking_store"
270
+
271
+ class ObjectMarkingStore implements MarkingStoreInterface
272
+ {
EF5E
273
+ public function getMarking($subject)
274
+ {
275
+ $subject->getStatus();
276
+ // ...
277
+ // return a marking
278
+ }
279
+
280
+ public function setMarking($subject, Marking $marking, array $context);
281
+ {
282
+ // ...
283
+ $subject->setStatus($newStatus, $context['log_comment'] ?? null);
284
+ }
285
+ }
286
+
287
+ // and in your Object class
288
+
289
+ public function getStatus()
290
+ {
291
+ return $this->status;
292
+ }
293
+
294
+ public function setStatus(int $status, ?string $comment = null)
295
+ {
296
+ $this->status = $status;
297
+ $this->addStatusLogRecord(new StatusLog($this, $comment));
298
+
299
+ return $this;
300
+ }
301
+
302
+ // the StatusLog class can have a createdAt, a username,
303
+ // the new status, and finally your optional comment retrieved from the workflow context.
304
+
229
305
Using Events
230
306
------------
231
307
0 commit comments