Skip to content

[Workflow] Example on reacting to successful transitions #7416

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 2 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
61 changes: 57 additions & 4 deletions workflow/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -175,12 +175,65 @@ what actions are allowed on a blog post::
$transitions = $workflow->getEnabledTransitions($post);

Using Events
------------
============

To make your workflows even more powerful you could construct the ``Workflow``
object with an ``EventDispatcher``. You can now create event listeners to
block transitions (i.e. depending on the data in the blog post). The following
events are dispatched:
object with an ``EventDispatcher``. If you are using the ``Workflow`` component within
the Symfony standard edition, this is already done for you.

When a workflow subject's state is updated, the component dispatches several events that
enable you to alter the component's behaviour:

* ``workflow.[workflow name].guard.[transition name]``: used to conditionally block a transition.
Copy link
Member

@javiereguiluz javiereguiluz Jan 26, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should reword these descriptions a bit. The essential thing to document about events is if they are notified before or after something happened (that's why it's recommended to name the events post_* and pre_*). For example, a transition is being applied could be: notified just before applying a transition

* ``workflow.[workflow name].leave.[state name]``: a subject leaves a state.
* ``workflow.[workflow name].transition.[transition name]``: a transition is being applied.
* ``workflow.[workflow name].enter.[state name]``: a subject enters a state.
* ``workflow.[workflow name].announce.[transition name]``: a transition was applied to the subject.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps this deserves a note that a GuardEvent will be dispatched for guards instead of a base Workflow\Event.


Reacting to a successful transition
-----------------------------------

You can create an event listener that would perform some actions when a transition has been applied.
To do so, the event listener needs to have a method accepting the
:class:`Symfony\\Component\\Workflow\\Event\\Event` being dispatched, and a tag in the service
container.

The following example resembles a mailer that notifies an administrator of posts that have been sent
to review::

use Symfony\Component\Workflow\Event\Event;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class BlogPostProposedListener implements EventSubscriberInterface
{
// Passing the mailer to the listener is omitted for brevity
private $mailer;

public function notifyPostProposal(Event $event)
{
/** @var \AppBundle\Entity\BlogPost $post */
$post = $event->getSubject();

$message = \Swift_Message::newInstance('New blog post proposed');
$message->setTo(['admin@acme.co' => 'Admin']);
$message->setBody('A post has been submitted to review: '.$post->getTitle());

$this->mailer->send($message);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You forgot to really send the mail.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed :)


public static function getSubscribedEvents()
{
return array(
'workflow.blogpost.announce.to_review' => array('notifyPostProposal'),
);
}
}

Blocking transitions
--------------------

You can create Guard event listeners to block transitions (i.e. depending on the data in the blog
post). The following events are dispatched:
Copy link
Contributor Author

@kix kix Jan 26, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The events have already been listed in the previous section here. We could remove the list here, and add a note on GuardEvent instead.


* ``workflow.guard``
* ``workflow.[workflow name].guard``
Expand Down