Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-04-21T22:06:02.850Z
Answers.Text: From the man himself! Thanks Andrew, this is exactly what I've bene after.
Answers.UserDisplayName: Mark Notton
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-04-23T01:22:58.120Z
Answers.Text: No prob. Updated the answer with another possibility for you too, depending on what you're trying to do.
Answers.UserDisplayName: andrew.welch
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2018-04-21T03:09:02.930Z
Answers.LastActivityDate: 2018-04-23T01:22:26.687Z
Answers.LastEditDate: 2018-04-23T01:22:26.687Z
Answers.UserDisplayName: andrew.welch
Body: <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>
  
Tags: <plugin-development><craft3><hook>
Title: Getting current entry element via plugin in Craft 3 without a hook
ThreadText: # 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](https://github.com/nystudio107/craft-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.  
  
  ---
  
  