diff --git a/workflow.rst b/workflow.rst
index 21ae4192d58..f914e327276 100644
--- a/workflow.rst
+++ b/workflow.rst
@@ -236,8 +236,34 @@ what actions are allowed on a blog post::
Accessing the Workflow in a Class
---------------------------------
-To access workflow inside a class, use dependency injection and inject the
-registry in the constructor::
+To access workflow inside a class, use dependency injection and inject the typed + named parameter injection in the constructor::
+
+ use App\Entity\BlogPost;
+ use Symfony\Component\Workflow\WorkflowInterface;
+
+ class MyClass
+ {
+ private $blogPublishingWorkflow;
+
+ public function __construct(WorkflowInterface $blogPublishingWorkflow)
+ {
+ $this->blogPublishingWorkflow = $blogPublishingWorkflow;
+ }
+
+ public function toReview(BlogPost $post)
+ {
+ // Update the currentState on the post
+ try {
+ $this->blogPublishingWorkflow->apply($post, 'to_review');
+ } catch (LogicException $exception) {
+ // ...
+ }
+ // ...
+ }
+ }
+
+
+Or use the registry::
use App\Entity\BlogPost;
use Symfony\Component\Workflow\Registry;
@@ -253,11 +279,11 @@ registry in the constructor::
public function toReview(BlogPost $post)
{
- $workflow = $this->workflowRegistry->get($post);
+ $blogPublishingWorkflow = $this->workflowRegistry->get($post);
// Update the currentState on the post
try {
- $workflow->apply($post, 'to_review');
+ $blogPublishingWorkflow->apply($post, 'to_review');
} catch (LogicException $exception) {
// ...
}
@@ -265,6 +291,10 @@ registry in the constructor::
}
}
+.. tip::
+
+ You can find the list of available services with the following command ``php bin/console debug:autowiring workflow``
+
Using Events
------------
@@ -665,7 +695,7 @@ of domain logic in your templates:
``workflow_has_marked_place()``
Returns ``true`` if the marking of the given object has the given state.
-
+
``workflow_transition_blockers()``
Returns :class:`Symfony\\Component\\Workflow\\TransitionBlockerList` for the given transition.
@@ -700,7 +730,7 @@ The following example shows these functions in action:
{% if 'reviewed' in workflow_marked_places(post) %}
Reviewed
{% endif %}
-
+
{# Loop through the transition blockers #}
{% for blocker in workflow_transition_blockers(post, 'publish') %}
{{ blocker.message }}
@@ -828,25 +858,23 @@ Then you can access this metadata in your controller as follows::
// src/App/Controller/BlogPostController.php
use App\Entity\BlogPost;
- use Symfony\Component\Workflow\Registry;
+ use Symfony\Component\Workflow\WorkflowInterface;
// ...
- public function myAction(Registry $registry, BlogPost $post)
+ public function myAction(WorkflowInterface $blogPublishingWorkflow, BlogPost $post)
{
- $workflow = $registry->get($post);
-
- $title = $workflow
+ $title = $blogPublishingWorkflow
->getMetadataStore()
->getWorkflowMetadata()['title'] ?? 'Default title'
;
- $maxNumOfWords = $workflow
+ $maxNumOfWords = $blogPublishingWorkflow
->getMetadataStore()
->getPlaceMetadata('draft')['max_num_of_words'] ?? 500
;
- $aTransition = $workflow->getDefinition()->getTransitions()[0];
- $priority = $workflow
+ $aTransition = $blogPublishingWorkflow->getDefinition()->getTransitions()[0];
+ $priority = $blogPublishingWorkflow
->getMetadataStore()
->getTransitionMetadata($aTransition)['priority'] ?? 0
;
@@ -869,7 +897,7 @@ In a :ref:`flash message ` in your controller::
// $transition = ...; (an instance of Transition)
- // $workflow is a Workflow instance retrieved from the Registry (see above)
+ // $workflow is a Workflow instance retrieved from the Registry or injected directly (see above)
$title = $workflow->getMetadataStore()->getMetadata('title', $transition);
$this->addFlash('info', "You have successfully applied the transition with title: '$title'");