Does publish events not exist?

Hi everyone.

I’m just starting my journey into Pimcore and stumbled upon something that i would call essential but i can not find any information about this topic.

I want to integrate a custom made webshop with Pimcore and my first attempt was using the php api and listening for the onPostUpdate event.

This kinda works but i don’t want to update the products on my webshop when i save in Pimcore. I want them to be updated when i publish in Pimcore. And i want to be able to update products on my webshop by setting up a scheduled publish in Pimcore.

Surely this must be the default behavior?

How do i integrate on publish?

What about checking for $dataObject->isPublished() to be true? And then continue in your event listener?

Sounds right. I made a small change to my code to check for isPublished().

This is what i have. isPublished returns true even when just saving.
And this is correct. The object is published. Just not this version.
If i unpublish the object. false is returned by isPublished().
So i need some way of telling if the current “save” is a publish as well?

namespace AppBundle\ProductUpdateListener;

use Pimcore\Event\Model\ElementEventInterface;
use Pimcore\Event\Model\DataObjectEvent;
use AppBundle\Model\Product\Product;
use Symfony\Component\HttpClient\HttpClient;

class Listener {
    public function onPostUpdate (ElementEventInterface $e) {
        if ($e instanceof DataObjectEvent) {
            $object = $e->getObject();
            if ($object->isPublished())
            {
                $client = HttpClient::create();
                if ($object instanceof Product)
                {
                    // do something with the object
                }
            }
        }
    }
}
    /**
 * Arguments:
 *  - saveVersionOnly | is set if method saveVersion() was called instead of save()
 *
 * @Event("Pimcore\Event\Model\DataObjectEvent")
 *
 * @var string
 */
const PRE_UPDATE = 'pimcore.dataobject.preUpdate';

Check for saveVersionOnly equals false as well! That should be false if you have a published version but only press save version!

1 Like

Thanks msphn. You’re a life saver.

This is really not intuitive.
A data object that is not published will NOT have the saveVersionOnly when only saved.
A date object that is published will have the saveVersionOnly argument when only saved.

So to detect when object are published i have to check if isPublished() and saveVersionOnly argument is not set.

namespace AppBundle\ProductUpdateListener;
 
use Pimcore\Event\Model\ElementEventInterface;
use Pimcore\Event\Model\DataObjectEvent;
use AppBundle\Model\Product\Product;

class Listener {
    public function onPostUpdate (ElementEventInterface $e) {
        if ($e instanceof DataObjectEvent) {
            if (!$e->hasArgument("saveVersionOnly"))
            {
                $object = $e->getObject();
                if ($object->isPublished())
                {
                    // Do something when published.
                }
            }
        }
    }
}

I consider it untuitive, but probably only because I know too much about it …

https://pimcore.com/docs/5.x/Development_Documentation/Tools_and_Features/Versioning.html#page_Versioning

Take care of versions, e.g. if you use getById, you get not the last version but the last published version and so on.

P.S. https://www.itamarweiss.com/personal/2018/02/28/return-early-pattern.html

BTW. The return early version…

namespace AppBundle\ProductUpdateListener;
 
use Pimcore\Event\Model\ElementEventInterface;
use Pimcore\Event\Model\DataObjectEvent;
use AppBundle\Model\Product\Product;

class Listener {
    public function onPostUpdate (ElementEventInterface $e) {
        if (!$e instanceof DataObjectEvent)
            return;

        if ($e->hasArgument("saveVersionOnly"))
            return;

        $object = $e->getObject();

        if (!$object->isPublished())
            return;

        if (!$object instanceof Product)
            return;

        // Do something when a product is published.
    }
}
$object->getLatestVersion()->getData()

Is there a scenario i can’t think of where the data in $object is not the latest published data? (After passing my wall of ifs)

Since you’re talking about getting latest version?

Yes, everytime you have like one published version and another unpublished newer version your listings would return the published one instead of the latest version. Also methods like getById and so on. I’m actually not sure which one is passed to the event handler.

But this is all about NOT getting “non published” version out into the wild.

So I’m interested in only getting published versions at the moment they are published.

The currently published version should always be returned by listing, get methods and so on :).