<p>Check out <code>Craft::$app-&gt;getElements()-&gt;getElementByUri()</code> -- or you might also use <code>Craft::$app-&gt;getUrlManager()-&gt;getMatchedElement()</code> depending on your use case.</p>

<ul>
<li>Your friendly neighborhood SEOmatic author :)</li>
</ul>

CC BY-SA 3.0
2018-04-21T22:06:02.850Z
From the man himself! Thanks Andrew, this is exactly what I've bene after.
Mark Notton
CC BY-SA 3.0
2018-04-23T01:22:58.120Z
No prob. Updated the answer with another possibility for you too, depending on what you're trying to do.
andrew.welch
CC BY-SA 3.0
2018-04-21T03:09:02.930Z
2018-04-23T01:22:26.687Z
2018-04-23T01:22:26.687Z
andrew.welch
<p>Is it possible to query the current entry or category element via a Craft 3 plugin/module?</p>

<p>In the 2.6 era, I would use a hook. The Craft 3 iteration of a hook looks something like this:</p>

<pre><code>$currentElement;

Craft::$app-&gt;getView()-&gt;hook('someHook', function(array &amp;$context) {
  if (isset($context['entry'])) {
    $currentElement = $context['entry'];
  } else if (isset($context['category'])) {
    $currentElement = $context['category'];
  }
});
</code></pre>

<p>With an accompanying Twig snippet in the template:</p>

<pre><code>{% hook 'someHook' %}
</code></pre>

<p>I'm wondering if there is a cleaner practice to accomplish the same goal? I find relying on Twig snippet reduces the flexibility of my plugin. </p>

<p>I've noticed the <a href="https://github.com/nystudio107/craft-seomatic" rel="nofollow noreferrer">SEOMatic</a> plugin is able to append entry specific data without any hooks in the template. I can't figure out how, but it gives me hope. </p>

<plugin-development><craft3><hook>
Getting current entry element via plugin in Craft 3 without a hook
# Getting current entry element via plugin in Craft 3 without a hook

(Asked by: username_0 on 2018-04-20)

Is it possible to query the current entry or category element via a Craft 3 plugin/module?
In the 2.6 era, I would use a hook. The Craft 3 iteration of a hook looks something like this:
$currentElement;

Craft::$app->getView()->hook('someHook', function(array &$context) {
  if (isset($context['entry'])) {
    $currentElement = $context['entry'];
  } else if (isset($context['category'])) {
    $currentElement = $context['category'];
  }
});

With an accompanying Twig snippet in the template:
{% hook 'someHook' %}

I'm wondering if there is a cleaner practice to accomplish the same goal? I find relying on Twig snippet reduces the flexibility of my plugin. 
I've noticed the SEOMatic plugin is able to append entry specific data without any hooks in the template. I can't figure out how, but it gives me hope.

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


---

## 1 answers

### Answer by username_1 on 2018-04-21. Score: 2 | Accepted answer

Check out `Craft::$app->getElements()->getElementByUri()` -- or you might also use `Craft::$app->getUrlManager()->getMatchedElement()` depending on your use case.

* Your friendly neighborhood SEOmatic author :)

> **Comments:**
> **username_0**: From the man himself! Thanks Andrew, this is exactly what I've bene after.  
> **username_1**: No prob. Updated the answer with another possibility for you too, depending on what you're trying to do.  

---

