Workflow: latest published Version

Hello

What is ‘best practice’ when you use workflows to review changes of Objects in PimCore?

Simple Description of our Project:
We got a mobile App, where we show the teams of a sports league. Each team can edit its own Team Object.

Expected behavior:

  1. Team updates fields in mobile App.
  2. Team Object updates to a new version, last published version is showed in the app
  3. workflow transition from ‘published’ to ‘edit_team’
  4. Administrator can review the changes in pimcore backend, makes transition from ‘edit_team’ back to published (or rejects the changes)
  5. if published again: approved content is displayed on the app

Problems:
how can we display the last ‘published’ version of the objects through the php api?

config.yml

pimcore:
    workflows:
        team_workflow:
            label: 'Team Workflow'
            type: 'state_machine'
            supports:
            - 'Pimcore\Model\DataObject\Team'
            initial_place: 'published'
            places:
                changed:
                    label: 'Geändert'
                    color: '#377ea9'
                    permissions:
                        - objectLayout: 1
                published:
                    label: 'Veröffentlicht'
                    title: 'Veröffentlicht'
                    color: '#d9ef36'
                    permissions:
                        - objectLayout: 2
            transitions:
                    publish_team:
                        from: changed
                        to: published
                        options:
                            label: 'Freigeben'
                            notes:
                                commentEnabled: true
                                commentRequired: false
                    edit_team:
                        from: published
                        to: changed
                        options:
                            label: 'Editieren'
                            notes:
                                commentEnabled: true
                                commentRequired: false

what we tried:

APICoontroller.php

// After updating the teamObject $t

$workflow = $this->workflowRegistry->get($t, ‘team_workflow’);

    if($workflow->can($t, 'edit_team')) {

        $workflow->apply($t, 'edit_team');
    }

    $t->save();

this code is working as expected.

Get Request, load the last published state of the Object:

$versions = $d->getVersions();
$published_version = $d;

    foreach($versions as $v){

        $workflow = $this->workflowRegistry->get($v->getData(), 'team_workflow');

        if($workflow->getMarking($v->getData())->getPlaces() === 'published'){

            $published_version = $v->getData();
            break;
        };
    }

We can’t load the last published state of the object, because the workflow is not saved in the different version-instances of the object. when the actual place is ‘published’, every version of the object returns published.

The only way we see to make it work would be, to clone the object after an update, unpublish the new object and replace the old one after the review process, but then the whole workflow process would be useless.

Is there a good way to solve it with workflows, or is the workflow only to fill and aprove objects before they’re published?
Thanks in advance
Dominik

I got a working version now, but it doesn’t seem to be the best way to go.

What I do:

  1. Update from php api: Set workflow to “edited”, clone the object as child, set child to unpublished.
  2. Check the object changes in Admin Backend, set workflow back to ‘published’
  3. Use the Symfony Workflow listener workflow.[workflow_name].leave.[place_name]
  4. Save the contents of the childobject in its parent, delete the child object.

It’s working, but with problems.

  • After de childobject is deleted, there is an error in Admin backend. The childobject tries to refresh its tab, but it doesn’t exists anymore ->

Server threw exception - could not perform action. Please reload the admin interface and try again.

Timestamp: Thu May 02 2019 11:35:04 GMT+0200 (Mitteleuropäische Sommerzeit)
Status: 500 | Internal Server Error
URL: /admin/object/get
Message: __clone method called on non-object
Trace:
#0 /var/www/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/HttpKernel.php(151): Pimcore\Bundle\AdminBundle\Controller\Admin\DataObject\DataObjectController->getAction(Object(Symfony\Component\HttpFoundation\Request), Object(Symfony\Component\HttpKernel\Debug\TraceableEventDispatcher))
#1 /var/www/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/HttpKernel.php(68): Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object(Symfony\Component\HttpFoundation\Request), 1)
#2 /var/www/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/Kernel.php(200): Symfony\Component\HttpKernel\HttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#3 /var/www/web/app.php(42): Symfony\Component\HttpKernel\Kernel->handle(Object(Symfony\Component\HttpFoundation\Request))
#4 {main}

After a manual refresh everything is as expected.

Someone some advice?

Thanks in advance
Dominik

Hi,

I remember dealing with similar thing some time ago. I had an approval workflow where with a change it created new version only (did not publish it) - for that I needed to change the workflow code of Pimcore though.
Some details I’ve written below



Ivan