<p>Check out the definition of the <code>ElementEvent</code> that you're passed:</p>

<pre><code>class ElementEvent extends Event
{
    // Properties
    // =========================================================================

    /**
     * @var ElementInterface|null The element model associated with the event.
     */
    public $element;

    /**
     * @var bool Whether the element is brand new
     */
    public $isNew = false;
}
</code></pre>

<p>So you can access the element (in your case, presumably an <code>Entry</code>) via:</p>

<pre><code>$event-&gt;element
</code></pre>

<p>Make sure to check for <code>null</code>, and depending on how you set up the event listener, that the element is of the type of you're looking for.</p>

<p>For instance, if you want only listen to <code>all</code> elements that are saved, you'd do:</p>

<pre><code>    Event::on(
        Elements::class,
        Elements::EVENT_AFTER_SAVE_ELEMENT,
        function (ElementEvent $event) {
            Craft::debug(
                'Elements::EVENT_AFTER_SAVE_ELEMENT',
                __METHOD__
            );
        }
    );
</code></pre>

<p>...but if you only care about <code>Entry</code> elements, you'd do:</p>

<pre><code>    Event::on(
        Entry::class,
        Entry::EVENT_AFTER_SAVE,
        function (ModelEvent $event) {
            Craft::debug(
                'Entry::EVENT_AFTER_SAVE',
                __METHOD__
            );
        }
    );
</code></pre>

CC BY-SA 4.0
2018-07-06T19:41:01.527Z
Ah it seems I have much to learn about Craft's new event model. I think my callback was getting called multiple times and the first time I just happened to be looking at a matrix. I tried your code with `Entry::class` instead of `Elements::class` but the callback never gets called. Is there something obvious I might be missing?
Jameal G
CC BY-SA 4.0
2018-07-06T19:51:14.243Z
Ended up getting it working with this:
`Event::on(Entry::class, Entry::EVENT_AFTER_SAVE, function (Event $event) {});`
Jameal G
CC BY-SA 4.0
2018-07-07T16:40:55.760Z
My bad, copy and paste error, I fixed the original answer
andrew.welch
CC BY-SA 4.0
2018-07-05T17:06:11.253Z
2018-07-07T16:48:24.193Z
2018-07-07T16:48:24.193Z
andrew.welch
<p>In my plugin I am registering a callback for <code>Elements::EVENT_AFTER_SAVE_ELEMENT</code> event. Previously, in Craft2, I could access the URL of the saved entry from the <code>$event</code> parameter passed into the callback, like so: <code>$event-&gt;params['entry']-&gt;getUrl()</code>. </p>

<p>However I can't figure out how to get a reference to the entry in the Event model in Craft 3. If I <code>var_dump($event)</code> I can see the data I want nested deep inside a private property which I cannot access. </p>

<p>How do I access the entry data from the event in Craft 3?</p>

<plugin-development><craft3><events>
How to get an entry url from EVENT_AFTER_SAVE_ELEMENT callback?
# How to get an entry url from EVENT_AFTER_SAVE_ELEMENT callback?

(Asked by: username_0 on 2018-07-05)

In my plugin I am registering a callback for `Elements::EVENT_AFTER_SAVE_ELEMENT` event. Previously, in Craft2, I could access the URL of the saved entry from the `$event` parameter passed into the callback, like so: `$event->params['entry']->getUrl()`.

However I can't figure out how to get a reference to the entry in the Event model in Craft 3. If I `var_dump($event)` I can see the data I want nested deep inside a private property which I cannot access.

How do I access the entry data from the event in Craft 3?

**Tags:** plugin-development, craft3, events

---

## 1 answers

### Answer by username_1 on 2018-07-05. Score: 3 | Accepted answer

Check out the definition of the ElementEvent that you're passed:
class ElementEvent extends Event
{
    // Properties
    // =========================================================================

    /**
     * @var ElementInterface|null The element model associated with the event.
     */
    public $element;

    /**
     * @var bool Whether the element is brand new
     */
    public $isNew = false;
}

So you can access the element (in your case, presumably an Entry) via:
$event->element

Make sure to check for null, and depending on how you set up the event listener, that the element is of the type of you're looking for.
For instance, if you want only listen to all elements that are saved, you'd do:
    Event::on(
        Elements::class,
        Elements::EVENT_AFTER_SAVE_ELEMENT,
        function (ElementEvent $event) {
            Craft::debug(
                'Elements::EVENT_AFTER_SAVE_ELEMENT',
                __METHOD__
            );
        }
    );

...but if you only care about Entry elements, you'd do:
    Event::on(
        Entry::class,
        Entry::EVENT_AFTER_SAVE,
        function (ModelEvent $event) {
            Craft::debug(
                'Entry::EVENT_AFTER_SAVE',
                __METHOD__
            );
        }
    );

> **Comments:**
> **username_0**: Ah it seems I have much to learn about Craft's new event model. I think my callback was getting called multiple times and the first time I just happened to be looking at a matrix. I tried your code with `Entry::class` instead of `Elements::class` but the callback never gets called. Is there something obvious I might be missing?  
> **username_0**: Ended up getting it working with this: `Event::on(Entry::class, Entry::EVENT_AFTER_SAVE, function (Event $event) {});`  
> **username_1**: My bad, copy and paste error, I fixed the original answer  

---

