Skip to content

Commit 663639b

Browse files
committed
Incorporate the code examples in #9476 (comment) into the documentation
1 parent 17eae8c commit 663639b

File tree

1 file changed

+75
-6
lines changed

1 file changed

+75
-6
lines changed

workflow/usage.rst

Lines changed: 75 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -638,13 +638,82 @@ requires:
638638
),
639639
));
640640
641-
Then, you can access this metadata in your PHP code as follows::
641+
Then, you can access this metadata in your PHP code as follows:
642+
643+
In your Controller::
644+
645+
public function myControllerAction(Registry $registry, Article $article)
646+
{
647+
$workflow = $registry->get($article);
648+
649+
// Or, if you don't inject the Workflow Registry, fetch from the Container:
650+
$workflow = $this->get('workflow.article');
651+
652+
$workflow
653+
->getMetadataStore()
654+
->getWorkflowMetadata()['title'] ?? false
655+
;
656+
657+
// or
658+
$workflow
659+
->getMetadataStore()
660+
->getPlaceMetadata('draft')['title'] ?? false
661+
;
662+
663+
// or
664+
$aTransition = $workflow->getDefinition()->getTransitions()[0];
665+
$workflow
666+
->getMetadataStore()
667+
->getTransitionMetadata($aTransition)['title'] ?? false
668+
;
669+
}
670+
671+
There is a shortcut that works with everything::
672+
673+
$workflow
674+
->getMetadataStore()
675+
->getMetadata('title')
676+
;
677+
678+
In a Flash message in your Controller::
679+
680+
// $transition = ...; (an instance of Transition)
681+
$title = $this->get('workflow.article')->getMetadataStore()->getMetadata('title', $transition);
682+
$request->getSession()->getFlashBag()->add('info', "You have successfully applied the transition with title: '$title'");
683+
684+
In a listener, access via the Event::
685+
686+
<?php
687+
688+
namespace App\Listener\Workflow\Task;
689+
690+
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
691+
use Symfony\Component\Workflow\Event\GuardEvent;
692+
use Symfony\Component\Workflow\TransitionBlocker;
693+
694+
class DoneGuard implements EventSubscriberInterface
695+
{
696+
public function guardPublish(GuardEvent $event)
697+
{
698+
$timeLimit = $event->getMetadata('time_limit', $event->getTransition());
699+
700+
if (date('Hi') <= $timeLimit) {
701+
return;
702+
}
703+
704+
$explanation = $event->getMetadata('explaination', $event->getTransition());
705+
$event->addTransitionBlocker(new TransitionBlocker($explanation , 0));
706+
}
707+
708+
public static function getSubscribedEvents()
709+
{
710+
return [
711+
'workflow.task.guard.done' => 'guardPublish',
712+
];
713+
}
714+
}
715+
642716

643-
// MISSING EXAMPLE HERE...
644-
//
645-
//
646-
//
647-
//
648717

649718
In Twig templates, metadata is available via the ``workflow_metadata()`` function:
650719

0 commit comments

Comments
 (0)