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 6 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
10 changes: 10 additions & 0 deletions components/workflow.rst
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,16 @@ 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"

If you want to initiate your workflow, you can call ``getMarking``::
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
If you want to initiate your workflow, you can call ``getMarking``::
If you want to initiate your workflow, you can call ``getMarking()`` method::

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And I'm not super confortable with this sentence. What does mean "initiate your workflow"?
I do understand what you want to say, but for new comers, it could be confusing.

What about something like

When the object enter the workflow for the first time, the workflow will initialize the property with the initial_place.
If you want to have always a valid marking in your database, you can use the getMarking() method to initialize the object property with the initial place::

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I understand this sentence : When the object enter the workflow for the first time, the workflow will initialize the property with the initial_place.

And instead of initial_place, I think we should use initial_marking as in the configuration : https://symfony.com/doc/current/workflow.html#configuration

So, I would say :

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

WDYT ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you are right about initial marking :) I used the old name, sorry.

You sentence is correct, let's go for it.

Minor detail:

- in the configuration
+ from the configuration

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DONE !

If it's ok for you, I let you resolve the conversation and maybe... merge the PR ?


// ...
$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'];
$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