Skip to content

Documented the workflow metadata #9476

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 1 commit into from
Closed
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
142 changes: 142 additions & 0 deletions workflow/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -450,3 +450,145 @@ The following example shows these functions in action:
{% if 'waiting_some_approval' in workflow_marked_places(post) %}
<span class="label">PENDING</span>
{% endif %}

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
places, and their transitions using the ``metadata`` option. This metadata can
be as simple as the title of the workflow or as complex as your own application
requires:

.. configuration-block::

.. code-block:: yaml

# config/packages/workflow.yaml
framework:
workflows:
blog_publishing:
metadata: 'Blog Publishing Workflow'
# ...
places:
draft:
metadata:
max_num_of_words: 500
# ...
transitions:
to_review:
from: draft
to: review
metadata:
priority: 0.5
# ...

.. code-block:: xml

<!-- config/packages/workflow.xml -->
<?xml version="1.0" encoding="utf-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:framework="http://symfony.com/schema/dic/symfony"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd
http://symfony.com/schema/dic/symfony http://symfony.com/schema/dic/symfony/symfony-1.0.xsd"
>

<framework:config>
<framework:workflow name="blog_publishing" type="workflow">
<framework:metadata>
<framework:title>Blog Publishing Workflow</framework:title>
</framework:metadata>
<!-- ... -->

<framework:place name="draft">
<framework:metadata>
<framework:max-num-of-words>500</framework:max-num-of-words>
</framework:metadata>
</framework:place>
<!-- ... -->

<framework:transition name="to_review">
<framework:from>draft</framework:from>
<framework:to>review</framework:to>
<framework:metadata>
<framework:priority>0.5</framework:priority>
</framework:metadata>
</framework:transition>
<!-- ... -->
</framework:workflow>
</framework:config>
</container>

.. code-block:: php

// config/packages/workflow.php

$container->loadFromExtension('framework', array(
// ...
'workflows' => array(
'blog_publishing' => array(
'metadata' => array(
'title' => 'Blog Publishing Workflow',
),
// ...
'places' => array(
'draft' => array(
'max_num_of_words' => 500,
Copy link
Contributor

Choose a reason for hiding this comment

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

missing metadata array or i miss something? @javiereguiluz

),
// ...
),
'transitions' => array(
'to_review' => array(
'from' => 'draft',
'to' => 'review',
'metadata' => array(
'priority' => 0.5,
),
),
),
),
),
));

Then, you can access this metadata in your PHP code as follows::

// MISSING EXAMPLE HERE...
Copy link
Member

Choose a reason for hiding this comment

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

You can access it in different ways.

If you are using the workflow directly:

// I executed this code in the controller, so basically I'm fetching a Workflow instance
        $workflow = $this->get('workflow.article');

        $workflow
            ->getMetadataStore()
            ->getWorkflowMetadata()['title'] ?? false
        ;

        $workflow
            ->getMetadataStore()
            ->getPlaceMetadata('draft')['title'] ?? false
        ;

        $aTransition = $workflow->getDefinition()->getTransitions()[0];

        $workflow
            ->getMetadataStore()
            ->getTransitionMetadata($aTransition)['title'] ?? false
        ;

// There is a shortcut that work with everything, see https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Workflow/Metadata/MetadataStoreInterface.php#L34-L36

        $workflow
            ->getMetadataStore()
            ->getMetadata('title')
        ;

Use case: Flash message:

            // $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'");

In a listener:

<?php

namespace App\Listener\Workflow\Task;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Workflow\Event\GuardEvent;
use Symfony\Component\Workflow\TransitionBlocker;

class DoneGuard implements EventSubscriberInterface
{
    public function guardPublish(GuardEvent $event)
    {
        $timeLimit = $event->getMetadata('time_limit', $event->getTransition());

        if (date('Hi') <= $timeLimit) {
            return;
        }

        $explanation = $event->getMetadata('explaination', $event->getTransition());
        $event->addTransitionBlocker(new TransitionBlocker($explanation , 0));
    }

    public static function getSubscribedEvents()
    {
        return [
            'workflow.task.guard.done' => 'guardPublish',
        ];
    }
}

Copy link
Contributor

Choose a reason for hiding this comment

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

nice snippets :)

//
//
//
//

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

.. code-block:: twig

<h2>Metadata</h2>
<p>
<strong>Workflow</strong>:<br >
<code>{{ workflow_metadata(article, 'title') }}</code>
</p>
<p>
<strong>Current place(s)</strong>
<ul>
{% for place in workflow_marked_places(article) %}
<li>
{{ place }}:
<code>{{ workflow_metadata(article, 'max_num_of_words', place) ?: 'Unlimited'}}</code>
</li>
{% endfor %}
</ul>
</p>
<p>
<strong>Enabled transition(s)</strong>
<ul>
{% for transition in workflow_transitions(article) %}
<li>
{{ transition.name }}:
<code>{{ workflow_metadata(article, 'priority', transition) ?: '0' }}</code>
Copy link
Member

Choose a reason for hiding this comment

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

Super example !!!

</li>
{% endfor %}
</ul>
</p>