Skip to content

Commit 349fb94

Browse files
committed
Incorporate the code examples in symfony#9476 (comment) into the documentation
1 parent 95404f2 commit 349fb94

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
@@ -555,13 +555,82 @@ requires:
555555
),
556556
));
557557
558-
Then, you can access this metadata in your PHP code as follows::
558+
Then, you can access this metadata in your PHP code as follows:
559+
560+
In your Controller::
561+
562+
public function myControllerAction(Registry $registry, Article $article)
563+
{
564+
$workflow = $registry->get($article);
565+
566+
// Or, if you don't inject the Workflow Registry, fetch from the Container:
567+
$workflow = $this->get('workflow.article');
568+
569+
$workflow
570+
->getMetadataStore()
571+
->getWorkflowMetadata()['title'] ?? false
572+
;
573+
574+
// or
575+
$workflow
576+
->getMetadataStore()
577+
->getPlaceMetadata('draft')['title'] ?? false
578+
;
579+
580+
// or
581+
$aTransition = $workflow->getDefinition()->getTransitions()[0];
582+
$workflow
583+
->getMetadataStore()
584+
->getTransitionMetadata($aTransition)['title'] ?? false
585+
;
586+
}
587+
588+
There is a shortcut that works with everything::
589+
590+
$workflow
591+
->getMetadataStore()
592+
->getMetadata('title')
593+
;
594+
595+
In a Flash message in your Controller::
596+
597+
// $transition = ...; (an instance of Transition)
598+
$title = $this->get('workflow.article')->getMetadataStore()->getMetadata('title', $transition);
599+
$request->getSession()->getFlashBag()->add('info', "You have successfully applied the transition with title: '$title'");
600+
601+
In a listener, access via the Event::
602+
603+
<?php
604+
605+
namespace App\Listener\Workflow\Task;
606+
607+
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
608+
use Symfony\Component\Workflow\Event\GuardEvent;
609+
use Symfony\Component\Workflow\TransitionBlocker;
610+
611+
class DoneGuard implements EventSubscriberInterface
612+
{
613+
public function guardPublish(GuardEvent $event)
614+
{
615+
$timeLimit = $event->getMetadata('time_limit', $event->getTransition());
616+
617+
if (date('Hi') <= $timeLimit) {
618+
return;
619+
}
620+
621+
$explanation = $event->getMetadata('explaination', $event->getTransition());
622+
$event->addTransitionBlocker(new TransitionBlocker($explanation , 0));
623+
}
624+
625+
public static function getSubscribedEvents()
626+
{
627+
return [
628+
'workflow.task.guard.done' => 'guardPublish',
629+
];
630+
}
631+
}
632+
559633

560-
// MISSING EXAMPLE HERE...
561-
//
562-
//
563-
//
564-
//
565634

566635
In Twig templates, metadata is available via the ``workflow_metadata()`` function:
567636

0 commit comments

Comments
 (0)