Skip to content

Clarify how workflow can be injected #14106

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 43 additions & 15 deletions workflow.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -253,18 +279,22 @@ 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) {
// ...
}
// ...
}
}

.. tip::

You can find the list of available services with the following command ``php bin/console debug:autowiring workflow``

Using Events
------------

Expand Down Expand Up @@ -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.

Expand Down Expand Up @@ -700,7 +730,7 @@ The following example shows these functions in action:
{% if 'reviewed' in workflow_marked_places(post) %}
<span class="label">Reviewed</span>
{% endif %}

{# Loop through the transition blockers #}
{% for blocker in workflow_transition_blockers(post, 'publish') %}
<span class="error">{{ blocker.message }}</span>
Expand Down Expand Up @@ -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
;
Expand All @@ -869,7 +897,7 @@ In a :ref:`flash message <flash-messages>` 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'");

Expand Down