Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Workflow] Added explaination on context in events and initial marking #14678

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 11 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions components/workflow.rst
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,18 @@ you can retrieve a workflow from it and use it as follows::
$workflow->can($blogPost, 'publish'); // True
$workflow->getEnabledTransitions($blogPost); // $blogPost can perform transition "publish" or "reject"

Initialization
--------------

If the property of your object is null and you want to set it with the `initial_marking` from the configuration, you can call the `getMarking()` method to initialize the object property::

// ...
$blogPost = new BlogPost();
$workflow = $registry->get($blogPost);

// initiate workflow
$workflow->getMarking($blogPost);

Learn more
----------

Expand Down
23 changes: 23 additions & 0 deletions workflow.rst
Original file line number Diff line number Diff line change
Expand Up @@ -378,11 +378,34 @@ order:
* ``workflow.[workflow name].announce``
* ``workflow.[workflow name].announce.[transition name]``

You can avoid triggering those events by using the context::

$workflow->apply($subject, $transitionName, [Workflow::DISABLE_ANNOUNCE_EVENT => true]);

.. versionadded:: 5.1

The ``Workflow::DISABLE_ANNOUNCE_EVENT`` constant was introduced in Symfony 5.1.

.. versionadded:: 5.2

In Symfony 5.2, the context is accessible in all events::

// $context must be an array
$context = ['context_key' => 'context_value'];
$workflow->apply($subject, $transitionName, $context);

// in an event listener
$context = $event->getContext(); // returns ['context']

.. note::

The leaving and entering events are triggered even for transitions that stay
in same place.

.. note::

If you initialize the marking by calling ``$workflow->getMarking($object);``, then the ``workflow.[workflow name].entered.[initial place name]`` event will be called with a default context ``Workflow::DEFAULT_INITIAL_CONTEXT``.

Here is an example of how to enable logging for every time a "blog_publishing"
workflow leaves a place::

Expand Down