<p>I believe that there is a propogation step required during duplicating an entry. Removing the condition for <code>!$element-&gt;propagating</code> should make your logic work.</p>
<pre class="lang-php prettyprint-override"><code>Event::on(
        Elements::class,
        Elements::EVENT_BEFORE_SAVE_ELEMENT,
        function (ElementEvent $event) {
            $element = $event-&gt;element;
            if(
                $element
                &amp;&amp; $element instanceof Entry
                &amp;&amp; $element-&gt;getSection()-&gt;handle == 'emailCampaigns'
                &amp;&amp; !$element-&gt;getIsRevision() // is not revision
                &amp;&amp; !$element-&gt;getIsDraft() // is not draft
                &amp;&amp; !$element-&gt;resaving // not during resaving (avoid batch resaving)
            ) {
                $isNew = $event-&gt;isNew;
                $isDuplicate = $element-&gt;duplicateOf;
                if($isNew &amp;&amp; $isDuplicate instanceof Entry) {
                    //update post date and author
                    $element-&gt;dateCreated = new \DateTime();
                    $element-&gt;postDate = new \DateTime();
                    $currentuser = Craft::$app-&gt;getUser()-&gt;getIdentity();
                    $element-&gt;author = $currentuser;
                    $element-&gt;authorId = $currentuser-&gt;getId();
                }
            }
        }
    );
</code></pre>

CC BY-SA 4.0
2022-07-26T08:24:32.927Z
2022-07-26T08:24:32.927Z
Ben Croker
<p>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.</p>
<p>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 &gt; 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:</p>
<pre><code>Event::on(
        Elements::class,
        Elements::EVENT_BEFORE_SAVE_ELEMENT,
        function (ElementEvent $event) {
            $element = $event-&gt;element;
            if(
                $element
                &amp;&amp; $element instanceof Entry
                &amp;&amp; $element-&gt;getSection()-&gt;handle == 'emailCampaigns'
                &amp;&amp; !$element-&gt;getIsRevision() // is not revision
                &amp;&amp; !$element-&gt;getIsDraft() // is not draft
                &amp;&amp; !$element-&gt;propagating // not during propagating (avoid batch propagating)
                &amp;&amp; !$element-&gt;resaving // not during resaving (avoid batch resaving)
            ) {
                $isNew = $event-&gt;isNew;
                $isDuplicate = $element-&gt;duplicateOf;
                if($isNew &amp;&amp; $isDuplicate instanceof Entry) {
                    //update post date and author
                    $element-&gt;dateCreated = new \DateTime();
                    $element-&gt;postDate = new \DateTime();
                    $currentuser = Craft::$app-&gt;getUser()-&gt;getIdentity();
                    $element-&gt;author = $currentuser;
                    $element-&gt;authorId = $currentuser-&gt;getId();
                }
            }
        }
    );
</code></pre>
<p>By print_r'ing <code>$element</code> 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.</p>
<p>Any ideas?</p>

<plugin-development><plugins><datetime><author>
Can't change author and dateCreated/postDate on duplicate entry action in Control Panel
# Can't change author and dateCreated/postDate on duplicate entry action in Control Panel

(Asked by: username_0 on 2022-04-08)

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?

**Tags:** plugin-development, plugins, datetime, author

---

## 1 answers

### Answer by username_1 on 2022-07-26. Score: 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();
                }
            }
        }
    );


---

