diff --git a/workflow/usage.rst b/workflow/usage.rst index 44451510ba1..7be2a9ddc44 100644 --- a/workflow/usage.rst +++ b/workflow/usage.rst @@ -475,3 +475,70 @@ The following example shows these functions in action: {% if 'waiting_some_approval' in workflow_marked_places(post) %} PENDING {% endif %} + +Transition Blockers +------------------- + +.. versionadded:: 4.1 + Transition Blockers were introduced in Symfony 4.1. + +Transition Blockers provide a simple way to return a human-readable message for why a transition +was blocked. You can access the message from Twig. Here's an example: + + +.. code-block:: php + + use Symfony\Component\Workflow\Event\GuardEvent; + use Symfony\Component\EventDispatcher\EventSubscriberInterface; + + class BlogPostPublishListener implements EventSubscriberInterface + { + public function guardPublish(GuardEvent $event) + { + /** @var \App\Entity\BlogPost $post */ + $post = $event->getSubject(); + + // If it's after 9pm, prevent publication + if (date('H') > 21) { + $event->addTransitionBlocker( + new TransitionBlocker( + "You can not publish this blog post because it's too late. Try again tomorrow morning." + ) + ); + } + } + + public static function getSubscribedEvents() + { + return [ + 'workflow.blogpost.guard.publish' => ['guardPublish'], + ]; + } + } + +.. code-block:: html+twig + +
{{ blocker.parameters.expression }}
+ {% endif %}
+