Answers.Body: <p>One possibility is using the <code>EVENT_AFTER_POPULATE_ELEMENT</code> event to override entries' <code>uri</code> property:</p>
  <pre class="lang-php prettyprint-override"><code>use yii\base\Event;
  use craft\elements\db\ElementQuery;
  use craft\elements\db\EntryQuery;
  use craft\events\PopulateElementEvent;
  
  Event::on(
      EntryQuery::class,
      ElementQuery::EVENT_AFTER_POPULATE_ELEMENT,
      static function (PopulateElementEvent $event) {
         $entry = $event-&gt;element;
         if (!$entry-&gt;uri) {
            return;
         }
         $entry-&gt;uri = $entry-&gt;uri . '?x-something-dynamic=dynamic-value';
      }
  );
  </code></pre>
  <p>Beware that this will add the query string to entry URIs <em>everywhere</em>, though (including inside the control panel, i.e. for the &quot;View&quot; button and any links inside entry indexes).</p>
  <p>If you only want it to occur for Twig templates (i.e. frontend requests), you might want to do something like</p>
  <pre class="lang-php prettyprint-override"><code>...
  if (!$entry-&gt;uri || !Craft::$app-&gt;getRequest()-&gt;getIsSiteRequest()) {
     return;
  }
  ...
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-02-24T13:33:19.260Z
Answers.LastActivityDate: 2022-02-24T13:40:42.930Z
Answers.LastEditDate: 2022-02-24T13:40:42.930Z
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Body: <p>Due to some SEO and infrastructure issues I would like to be able to intercept the getUrl() method of entries and modify its return value.</p>
  <p>e.g.</p>
  <pre><code>&lt;a href=&quot;{{ entry.getUrl() }}&quot;&gt;{{ entry.title }}&lt;/a&gt;
  </code></pre>
  <p>would normally return</p>
  <pre><code>&lt;a href=&quot;/whatever/here&quot;&gt;Whatever Here Title&lt;/a&gt;
  </code></pre>
  <p>but I want the same TWIG Code to ouput</p>
  <pre><code>&lt;a href=&quot;/whatever/here?x-something-dynamic=dynamic-value&quot;&gt;Whatever Here Title&lt;/a&gt;
  </code></pre>
  <p>Is this possible with a module?</p>
  <p>Thanks!</p>
  
Tags: <templating>
Title: Hack into .getUrl() method of an entry
ThreadText: # Hack into .getUrl() method of an entry
  
  (Asked by: username_0 on 2022-02-24)
  
  Due to some SEO and infrastructure issues I would like to be able to intercept the getUrl() method of entries and modify its return value.
  
  e.g.
  
  ```
  <a href="{{ entry.getUrl() }}">{{ entry.title }}</a>
  
  ```
  
  would normally return
  
  ```
  <a href="/whatever/here">Whatever Here Title</a>
  
  ```
  
  but I want the same TWIG Code to ouput
  
  ```
  <a href="/whatever/here?x-something-dynamic=dynamic-value">Whatever Here Title</a>
  
  ```
  
  Is this possible with a module?
  
  Thanks!
  
  **Tags:** templating
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2022-02-24. Score: 1
  
  One possibility is using the `EVENT_AFTER_POPULATE_ELEMENT` event to override entries' `uri` property:
  
  ```php
  use yii\base\Event;
  use craft\elements\db\ElementQuery;
  use craft\elements\db\EntryQuery;
  use craft\events\PopulateElementEvent;
  
  Event::on(
      EntryQuery::class,
      ElementQuery::EVENT_AFTER_POPULATE_ELEMENT,
      static function (PopulateElementEvent $event) {
         $entry = $event->element;
         if (!$entry->uri) {
            return;
         }
         $entry->uri = $entry->uri . '?x-something-dynamic=dynamic-value';
      }
  );
  
  ```
  
  Beware that this will add the query string to entry URIs *everywhere*, though (including inside the control panel, i.e. for the "View" button and any links inside entry indexes).
  
  If you only want it to occur for Twig templates (i.e. frontend requests), you might want to do something like
  
  ```php
  ...
  if (!$entry->uri || !Craft::$app->getRequest()->getIsSiteRequest()) {
     return;
  }
  ...
  
  ```
  
  
  ---
  
  