1

We have a Craft-based email campaign builder that is up to date with Craft 3.7.x and plugins. I have a business logic plugin that works for a lot of custom functions but I can't seem to get this going.

Basically, our users clone a lot of existing entries (email campaigns) to create new ones, rather than creating them from scratch. When they clone (in the Control Panel using Save > Save as New Entry), I am running EVENT_BEFORE_SAVE_ELEMENT in the plugin, which is firing. Here's what's going on in that event:

Event::on(
        Elements::class,
        Elements::EVENT_BEFORE_SAVE_ELEMENT,
        function (ElementEvent $event) {
            $element = $event->element;
            if(
                $element
                && $element instanceof Entry
                && $element->getSection()->handle == 'emailCampaigns'
                && !$element->getIsRevision() // is not revision
                && !$element->getIsDraft() // is not draft
                && !$element->propagating // not during propagating (avoid batch propagating)
                && !$element->resaving // not during resaving (avoid batch resaving)
            ) {
                $isNew = $event->isNew;
                $isDuplicate = $element->duplicateOf;
                if($isNew && $isDuplicate instanceof Entry) {
                    //update post date and author
                    $element->dateCreated = new \DateTime();
                    $element->postDate = new \DateTime();
                    $currentuser = Craft::$app->getUser()->getIdentity();
                    $element->author = $currentuser;
                    $element->authorId = $currentuser->getId();
                }
            }
        }
    );

By print_r'ing $element I can see that the author and dates have been changed as I wish in the element, but once the save is complete and I am looking at the new entry in the Control Panel, the author and dates remain the same as the old entry.

Any ideas?

1 Answer 1

1

I believe that there is a propogation step required during duplicating an entry. Removing the condition for !$element->propagating should make your logic work.

Event::on(
        Elements::class,
        Elements::EVENT_BEFORE_SAVE_ELEMENT,
        function (ElementEvent $event) {
            $element = $event->element;
            if(
                $element
                && $element instanceof Entry
                && $element->getSection()->handle == 'emailCampaigns'
                && !$element->getIsRevision() // is not revision
                && !$element->getIsDraft() // is not draft
                && !$element->resaving // not during resaving (avoid batch resaving)
            ) {
                $isNew = $event->isNew;
                $isDuplicate = $element->duplicateOf;
                if($isNew && $isDuplicate instanceof Entry) {
                    //update post date and author
                    $element->dateCreated = new \DateTime();
                    $element->postDate = new \DateTime();
                    $currentuser = Craft::$app->getUser()->getIdentity();
                    $element->author = $currentuser;
                    $element->authorId = $currentuser->getId();
                }
            }
        }
    );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.