Blog Publishing Workflow
@@ -646,9 +647,6 @@ In your Controller::
{
$workflow = $registry->get($article);
- // Or, if you don't inject the Workflow Registry, fetch from the Container:
- $workflow = $this->get('workflow.article');
-
$workflow
->getMetadataStore()
->getWorkflowMetadata()['title'] ?? false
@@ -678,8 +676,9 @@ There is a shortcut that works with everything::
In a Flash message in your Controller::
// $transition = ...; (an instance of Transition)
- $title = $this->get('workflow.article')->getMetadataStore()->getMetadata('title', $transition);
- $request->getSession()->getFlashBag()->add('info', "You have successfully applied the transition with title: '$title'");
+ // $workflow is a WorkFlow instance retrieved from the Registry (see above)
+ $title = $workflow->getMetadataStore()->getMetadata('title', $transition);
+ $this->addFlash('info', "You have successfully applied the transition with title: '$title'");
In a listener, access via the Event::
@@ -691,7 +690,7 @@ In a listener, access via the Event::
use Symfony\Component\Workflow\Event\GuardEvent;
use Symfony\Component\Workflow\TransitionBlocker;
- class DoneGuard implements EventSubscriberInterface
+ class OverdueGuard implements EventSubscriberInterface
{
public function guardPublish(GuardEvent $event)
{
@@ -717,7 +716,7 @@ In a listener, access via the Event::
In Twig templates, metadata is available via the ``workflow_metadata()`` function:
-.. code-block:: twig
+.. code-block:: html+twig
Metadata
From c9ef2622bc16a288e3b4a4ac81338543def00fa4 Mon Sep 17 00:00:00 2001
From: Peter Bowyer
Date: Sat, 23 Mar 2019 13:34:46 +0000
Subject: [PATCH 05/17] Incorporate @OskarStark's feedback
---
workflow/usage.rst | 9 +++------
1 file changed, 3 insertions(+), 6 deletions(-)
diff --git a/workflow/usage.rst b/workflow/usage.rst
index cfb3b5d3ab7..41210ab8f15 100644
--- a/workflow/usage.rst
+++ b/workflow/usage.rst
@@ -538,6 +538,7 @@ Storing Metadata
----------------
.. versionadded:: 4.1
+
The feature to store metadata in workflows was introduced in Symfony 4.1.
In case you need it, you can store arbitrary metadata in workflows, their
@@ -639,9 +640,7 @@ requires:
),
));
-Then, you can access this metadata in your PHP code as follows:
-
-In your Controller::
+Then you can access this metadata in your controller as follows::
public function myControllerAction(Registry $registry, Article $article)
{
@@ -676,14 +675,12 @@ There is a shortcut that works with everything::
In a Flash message in your Controller::
// $transition = ...; (an instance of Transition)
- // $workflow is a WorkFlow instance retrieved from the Registry (see above)
+ // $workflow is a Workflow instance retrieved from the Registry (see above)
$title = $workflow->getMetadataStore()->getMetadata('title', $transition);
$this->addFlash('info', "You have successfully applied the transition with title: '$title'");
In a listener, access via the Event::
-
Date: Sat, 23 Mar 2019 13:47:24 +0000
Subject: [PATCH 06/17] Document how to see all configuration options (see
https://github.com/symfony/symfony-docs/pull/11209#discussion_r268392587)
---
workflow/usage.rst | 9 +++++++++
1 file changed, 9 insertions(+)
diff --git a/workflow/usage.rst b/workflow/usage.rst
index 41210ab8f15..b0865e78324 100644
--- a/workflow/usage.rst
+++ b/workflow/usage.rst
@@ -14,6 +14,15 @@ install the workflow feature before using it:
$ composer require symfony/workflow
+Configuration
+-------------
+
+To see all configuration options, if you are using the component inside a Symfony project run this command:
+
+.. code-block:: terminal
+
+ $ bin/console config:dump-reference framework workflows
+
Creating a Workflow
-------------------
From af71c1427bc8bfe78f5fcf963f57f8641e30c564 Mon Sep 17 00:00:00 2001
From: Peter Bowyer
Date: Sat, 23 Mar 2019 14:36:20 +0000
Subject: [PATCH 07/17] Add an introduction as suggested in
https://github.com/symfony/symfony-docs/pull/11209#discussion_r268391900
When https://github.com/symfony/symfony-docs/issues/9465 is finished, link to details on Transition Blockers.
---
workflow/usage.rst | 12 ++++++++++--
1 file changed, 10 insertions(+), 2 deletions(-)
diff --git a/workflow/usage.rst b/workflow/usage.rst
index b0865e78324..46b928160de 100644
--- a/workflow/usage.rst
+++ b/workflow/usage.rst
@@ -688,7 +688,15 @@ In a Flash message in your Controller::
$title = $workflow->getMetadataStore()->getMetadata('title', $transition);
$this->addFlash('info', "You have successfully applied the transition with title: '$title'");
-In a listener, access via the Event::
+In a listener, access via the Event
+
+Metadata can also be accessed in a Listener, from the Event object.
+
+The example below uses a new feature introduced in 4.1 called Transition Blockers. These let you
+return a user-friendly error message when you stop a transition from happening. In the example we
+get this user-friendly message from the Event's metadata, giving you an easy place to manage the
+text. This is a contrived example; in production systems you may prefer to use the
+:doc:`Translation ` component to manage text::
namespace App\Listener\Workflow\Task;
@@ -706,7 +714,7 @@ In a listener, access via the Event::
return;
}
- $explanation = $event->getMetadata('explaination', $event->getTransition());
+ $explanation = $event->getMetadata('explanation', $event->getTransition());
$event->addTransitionBlocker(new TransitionBlocker($explanation , 0));
}
From ea649921e5f70e8310d644f57fe6262d1840047a Mon Sep 17 00:00:00 2001
From: Peter Bowyer
Date: Sat, 23 Mar 2019 15:31:47 +0000
Subject: [PATCH 08/17] Incorporate further excellent feedback from @HeahDude
---
workflow/usage.rst | 12 ++++++------
1 file changed, 6 insertions(+), 6 deletions(-)
diff --git a/workflow/usage.rst b/workflow/usage.rst
index 46b928160de..0dcc55b61b8 100644
--- a/workflow/usage.rst
+++ b/workflow/usage.rst
@@ -688,15 +688,13 @@ In a Flash message in your Controller::
$title = $workflow->getMetadataStore()->getMetadata('title', $transition);
$this->addFlash('info', "You have successfully applied the transition with title: '$title'");
-In a listener, access via the Event
-
Metadata can also be accessed in a Listener, from the Event object.
-The example below uses a new feature introduced in 4.1 called Transition Blockers. These let you
+Using transition blockers you can
return a user-friendly error message when you stop a transition from happening. In the example we
-get this user-friendly message from the Event's metadata, giving you an easy place to manage the
-text. This is a contrived example; in production systems you may prefer to use the
-:doc:`Translation ` component to manage text::
+get this message from the :class:`Symfony\\Component\\Workflow\\Event\\Event`'s metadata, giving
+you an easy place to manage the text. This is a contrived example; in production code you may
+prefer to use the :doc:`Translation ` component to manage messages::
namespace App\Listener\Workflow\Task;
@@ -726,7 +724,9 @@ text. This is a contrived example; in production systems you may prefer to use t
}
}
+.. versionadded:: 4.1
+ The transition blockers were added in version 4.1.
In Twig templates, metadata is available via the ``workflow_metadata()`` function:
From 40fbaf3c48c904453be898b2a70bbd930e87aa8d Mon Sep 17 00:00:00 2001
From: Peter Bowyer
Date: Sat, 23 Mar 2019 15:36:03 +0000
Subject: [PATCH 09/17] Update arrays to use short syntax
---
workflow/usage.rst | 55 +++++++++++++++++++++++-----------------------
1 file changed, 28 insertions(+), 27 deletions(-)
diff --git a/workflow/usage.rst b/workflow/usage.rst
index 0dcc55b61b8..3fd766b6647 100644
--- a/workflow/usage.rst
+++ b/workflow/usage.rst
@@ -164,6 +164,7 @@ like this:
]);
As configured, the following property is used by the marking store::
+.. code-block:: php
class BlogPost
{
@@ -620,34 +621,34 @@ requires:
// config/packages/workflow.php
- $container->loadFromExtension('framework', array(
+$container->loadFromExtension('framework', [
+ // ...
+ 'workflows' => [
+ 'blog_publishing' => [
+ 'metadata' => [
+ 'title' => 'Blog Publishing Workflow',
+ ],
// ...
- 'workflows' => array(
- 'blog_publishing' => array(
- 'metadata' => array(
- 'title' => 'Blog Publishing Workflow',
- ),
- // ...
- 'places' => array(
- 'draft' => array(
- 'metadata' => array(
- 'max_num_of_words' => 500,
- ),
- ),
- // ...
- ),
- 'transitions' => array(
- 'to_review' => array(
- 'from' => 'draft',
- 'to' => 'review',
- 'metadata' => array(
- 'priority' => 0.5,
- ),
- ),
- ),
- ),
- ),
- ));
+ 'places' => [
+ 'draft' => [
+ 'metadata' => [
+ 'max_num_of_words' => 500,
+ ],
+ ],
+ // ...
+ ],
+ 'transitions' => [
+ 'to_review' => [
+ 'from' => 'draft',
+ 'to' => 'review',
+ 'metadata' => [
+ 'priority' => 0.5,
+ ],
+ ],
+ ],
+ ],
+ ],
+]);
Then you can access this metadata in your controller as follows::
From 94d17ded622899c04b94699376d63b893eebefcb Mon Sep 17 00:00:00 2001
From: Peter Bowyer
Date: Mon, 25 Mar 2019 17:08:06 +0000
Subject: [PATCH 10/17] Simplify the English and turn it into a tip
---
workflow/usage.rst | 8 ++++++--
1 file changed, 6 insertions(+), 2 deletions(-)
diff --git a/workflow/usage.rst b/workflow/usage.rst
index 3fd766b6647..3e4a301de42 100644
--- a/workflow/usage.rst
+++ b/workflow/usage.rst
@@ -694,8 +694,12 @@ Metadata can also be accessed in a Listener, from the Event object.
Using transition blockers you can
return a user-friendly error message when you stop a transition from happening. In the example we
get this message from the :class:`Symfony\\Component\\Workflow\\Event\\Event`'s metadata, giving
-you an easy place to manage the text. This is a contrived example; in production code you may
-prefer to use the :doc:`Translation ` component to manage messages::
+you an easy place to manage the text.
+
+.. tip::
+
+This is a simple example; in production you may prefer to use the :doc:`Translation `
+component to manage messages in one place::
namespace App\Listener\Workflow\Task;
From f716e81a181ca9f2648f13cfcf8c4dc0be828e04 Mon Sep 17 00:00:00 2001
From: Peter Bowyer
Date: Mon, 25 Mar 2019 19:10:51 +0000
Subject: [PATCH 11/17] Incorporate @OskarStark's feedback
---
workflow/usage.rst | 52 +++++++++++++++++++++++-----------------------
1 file changed, 26 insertions(+), 26 deletions(-)
diff --git a/workflow/usage.rst b/workflow/usage.rst
index 3e4a301de42..a2a1de8b74c 100644
--- a/workflow/usage.rst
+++ b/workflow/usage.rst
@@ -621,34 +621,34 @@ requires:
// config/packages/workflow.php
-$container->loadFromExtension('framework', [
- // ...
- 'workflows' => [
- 'blog_publishing' => [
- 'metadata' => [
- 'title' => 'Blog Publishing Workflow',
- ],
+ $container->loadFromExtension('framework', [
// ...
- 'places' => [
- 'draft' => [
+ 'workflows' => [
+ 'blog_publishing' => [
'metadata' => [
- 'max_num_of_words' => 500,
+ 'title' => 'Blog Publishing Workflow',
],
- ],
- // ...
- ],
- 'transitions' => [
- 'to_review' => [
- 'from' => 'draft',
- 'to' => 'review',
- 'metadata' => [
- 'priority' => 0.5,
+ // ...
+ 'places' => [
+ 'draft' => [
+ 'metadata' => [
+ 'max_num_of_words' => 500,
+ ],
+ ],
+ // ...
+ ],
+ 'transitions' => [
+ 'to_review' => [
+ 'from' => 'draft',
+ 'to' => 'review',
+ 'metadata' => [
+ 'priority' => 0.5,
+ ],
+ ],
],
],
],
- ],
- ],
-]);
+ ]);
Then you can access this metadata in your controller as follows::
@@ -694,12 +694,12 @@ Metadata can also be accessed in a Listener, from the Event object.
Using transition blockers you can
return a user-friendly error message when you stop a transition from happening. In the example we
get this message from the :class:`Symfony\\Component\\Workflow\\Event\\Event`'s metadata, giving
-you an easy place to manage the text.
+you a central place to manage the text.
.. tip::
-This is a simple example; in production you may prefer to use the :doc:`Translation `
-component to manage messages in one place::
+ This is a simple example; in production you may prefer to use the :doc:`Translation `
+ component to manage messages in one place::
namespace App\Listener\Workflow\Task;
@@ -731,7 +731,7 @@ component to manage messages in one place::
.. versionadded:: 4.1
- The transition blockers were added in version 4.1.
+ The transition blockers were introduced in version 4.1.
In Twig templates, metadata is available via the ``workflow_metadata()`` function:
From 225c2fe757009a01407b9749ee784a4a62c6c5a4 Mon Sep 17 00:00:00 2001
From: Oskar Stark
Date: Fri, 29 Mar 2019 21:12:57 +0000
Subject: [PATCH 12/17] Apply suggestions from code review
Co-Authored-By: pbowyer
---
workflow/usage.rst | 15 ++++++++-------
1 file changed, 8 insertions(+), 7 deletions(-)
diff --git a/workflow/usage.rst b/workflow/usage.rst
index a2a1de8b74c..c003c222dd5 100644
--- a/workflow/usage.rst
+++ b/workflow/usage.rst
@@ -583,12 +583,12 @@ requires:
.. code-block:: xml
-
+
@@ -684,10 +684,11 @@ There is a shortcut that works with everything::
In a Flash message in your Controller::
- // $transition = ...; (an instance of Transition)
- // $workflow is a Workflow instance retrieved from the Registry (see above)
- $title = $workflow->getMetadataStore()->getMetadata('title', $transition);
- $this->addFlash('info', "You have successfully applied the transition with title: '$title'");
+ // $transition = ...; (an instance of Transition)
+
+ // $workflow is a Workflow instance retrieved from the Registry (see above)
+ $title = $workflow->getMetadataStore()->getMetadata('title', $transition);
+ $this->addFlash('info', "You have successfully applied the transition with title: '$title'");
Metadata can also be accessed in a Listener, from the Event object.
From 55c91998d6145bb8ac273a5498bd64dcc18ae3b2 Mon Sep 17 00:00:00 2001
From: Peter Bowyer
Date: Fri, 29 Mar 2019 21:16:44 +0000
Subject: [PATCH 13/17] Indent PHP block by an additional 4 spaces
---
workflow/usage.rst | 40 ++++++++++++++++++++--------------------
1 file changed, 20 insertions(+), 20 deletions(-)
diff --git a/workflow/usage.rst b/workflow/usage.rst
index c003c222dd5..3532337de12 100644
--- a/workflow/usage.rst
+++ b/workflow/usage.rst
@@ -702,33 +702,33 @@ you a central place to manage the text.
This is a simple example; in production you may prefer to use the :doc:`Translation `
component to manage messages in one place::
- namespace App\Listener\Workflow\Task;
+ namespace App\Listener\Workflow\Task;
- use Symfony\Component\EventDispatcher\EventSubscriberInterface;
- use Symfony\Component\Workflow\Event\GuardEvent;
- use Symfony\Component\Workflow\TransitionBlocker;
+ use Symfony\Component\EventDispatcher\EventSubscriberInterface;
+ use Symfony\Component\Workflow\Event\GuardEvent;
+ use Symfony\Component\Workflow\TransitionBlocker;
- class OverdueGuard implements EventSubscriberInterface
- {
- public function guardPublish(GuardEvent $event)
+ class OverdueGuard implements EventSubscriberInterface
{
- $timeLimit = $event->getMetadata('time_limit', $event->getTransition());
+ public function guardPublish(GuardEvent $event)
+ {
+ $timeLimit = $event->getMetadata('time_limit', $event->getTransition());
- if (date('Hi') <= $timeLimit) {
- return;
- }
+ if (date('Hi') <= $timeLimit) {
+ return;
+ }
- $explanation = $event->getMetadata('explanation', $event->getTransition());
- $event->addTransitionBlocker(new TransitionBlocker($explanation , 0));
- }
+ $explanation = $event->getMetadata('explanation', $event->getTransition());
+ $event->addTransitionBlocker(new TransitionBlocker($explanation , 0));
+ }
- public static function getSubscribedEvents()
- {
- return [
- 'workflow.task.guard.done' => 'guardPublish',
- ];
+ public static function getSubscribedEvents()
+ {
+ return [
+ 'workflow.task.guard.done' => 'guardPublish',
+ ];
+ }
}
- }
.. versionadded:: 4.1
From 3765ddb198f7529359ecc3d8659e27ddf0684a9c Mon Sep 17 00:00:00 2001
From: Peter Bowyer
Date: Fri, 29 Mar 2019 21:19:13 +0000
Subject: [PATCH 14/17] Change code formatting of PHP snippet per
https://github.com/symfony/symfony-docs/pull/11209/files/d57fa38d903175d58d9cfbf63f20e7ced8d2fd01#r268391655
---
workflow/usage.rst | 5 ++---
1 file changed, 2 insertions(+), 3 deletions(-)
diff --git a/workflow/usage.rst b/workflow/usage.rst
index 3532337de12..7725d06f9e6 100644
--- a/workflow/usage.rst
+++ b/workflow/usage.rst
@@ -662,9 +662,8 @@ Then you can access this metadata in your controller as follows::
;
// or
- $workflow
- ->getMetadataStore()
- ->getPlaceMetadata('draft')['title'] ?? false
+ $workflow->getMetadataStore()
+ ->getWorkflowMetadata()['title'] ?? false
;
// or
From 6080aa8edc7be1314e9af877463c2f06b13298d1 Mon Sep 17 00:00:00 2001
From: Peter Bowyer
Date: Sun, 7 Apr 2019 10:06:32 +0100
Subject: [PATCH 15/17] Remove the word 'simple'
---
workflow/usage.rst | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/workflow/usage.rst b/workflow/usage.rst
index 7725d06f9e6..7290aad43b0 100644
--- a/workflow/usage.rst
+++ b/workflow/usage.rst
@@ -698,7 +698,7 @@ you a central place to manage the text.
.. tip::
- This is a simple example; in production you may prefer to use the :doc:`Translation `
+ This example has been simplified; in production you may prefer to use the :doc:`Translation `
component to manage messages in one place::
namespace App\Listener\Workflow\Task;
From a4c23c15b2b2b10ac6bb3fb80873243896d56d56 Mon Sep 17 00:00:00 2001
From: Peter Bowyer
Date: Sun, 7 Apr 2019 11:03:55 +0100
Subject: [PATCH 16/17] Add blank line between code block and sentence above.
Code block was not rendering.
---
workflow/usage.rst | 1 +
1 file changed, 1 insertion(+)
diff --git a/workflow/usage.rst b/workflow/usage.rst
index 7290aad43b0..b41c872a5e1 100644
--- a/workflow/usage.rst
+++ b/workflow/usage.rst
@@ -164,6 +164,7 @@ like this:
]);
As configured, the following property is used by the marking store::
+
.. code-block:: php
class BlogPost
From 7f3a0fd1cb05b7ccda150fb19e092f1ac1d38c77 Mon Sep 17 00:00:00 2001
From: Peter Bowyer
Date: Sun, 7 Apr 2019 12:06:09 +0100
Subject: [PATCH 17/17] Oskar's feedback
---
workflow/usage.rst | 4 +---
1 file changed, 1 insertion(+), 3 deletions(-)
diff --git a/workflow/usage.rst b/workflow/usage.rst
index b41c872a5e1..532837fe4bd 100644
--- a/workflow/usage.rst
+++ b/workflow/usage.rst
@@ -165,8 +165,6 @@ like this:
As configured, the following property is used by the marking store::
-.. code-block:: php
-
class BlogPost
{
// This property is used by the marking store
@@ -732,7 +730,7 @@ you a central place to manage the text.
.. versionadded:: 4.1
- The transition blockers were introduced in version 4.1.
+ The transition blockers were introduced in Symfony 4.1.
In Twig templates, metadata is available via the ``workflow_metadata()`` function: