@@ -467,3 +467,70 @@ The following example shows these functions in action:
467467 {% if 'waiting_some_approval' in workflow_marked_places(post) %}
468468 <span class="label">PENDING</span>
469469 {% endif %}
470+
471+ Transition Blockers
472+ -------------------
473+
474+ .. versionadded :: 4.1
475+ Transition Blockers were introduced in Symfony 4.1.
476+
477+ Transition Blockers provide a simple way to return a human-readable message for why a transition
478+ was blocked. You can access the message from Twig. Here's an example:
479+
480+
481+ .. code-block :: php
482+
483+ use Symfony\Component\Workflow\Event\GuardEvent;
484+ use Symfony\Component\EventDispatcher\EventSubscriberInterface;
485+
486+ class BlogPostPublishListener implements EventSubscriberInterface
487+ {
488+ public function guardPublish(GuardEvent $event)
489+ {
490+ /** @var \App\Entity\BlogPost $post */
491+ $post = $event->getSubject();
492+
493+ // If it's after 9pm, prevent publication
494+ if (date('H') > 21) {
495+ $event->addTransitionBlocker(
496+ new TransitionBlocker(
497+ "You can not publish this blog post because it's too late. Try again tomorrow morning."
498+ )
499+ );
500+ }
501+ }
502+
503+ public static function getSubscribedEvents()
504+ {
505+ return [
506+ 'workflow.blogpost.guard.publish' => ['guardPublish'],
507+ ];
508+ }
509+ }
510+
511+ .. code-block :: html+twig
512+
513+ <h2>Publication was blocked because:</h2>
514+ <ul>
515+ {% for transition in workflow_all_transitions(article) %}
516+ {% if not workflow_can(article, transition.name) %}
517+ <li>
518+ <strong>{{ transition.name }}</strong>:
519+ <ul>
520+ {% for blocker in workflow_build_transition_blocker_list(article, transition.name) %}
521+ <li>
522+ {{ blocker.message }}
523+ {% if blocker.parameters.expression is defined %}
524+ <code>{{ blocker.parameters.expression }}</code>
525+ {% endif %}
526+ </li>
527+ {% endfor %}
528+ </ul>
529+ </li>
530+ {% endif %}
531+ {% endfor %}
532+ </ul>
533+
534+ Don't need a human-readable message? You can still use::
535+
536+ $event->setBlocked('true');
0 commit comments