<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>

CC BY-SA 4.0
2022-02-24T13:33:19.260Z
2022-02-24T13:40:42.930Z
2022-02-24T13:40:42.930Z
Mats Mikkel Rummelhoff
<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>

<templating>
Hack into .getUrl() method of an entry
# 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.
{{ entry.title }}

would normally return
Whatever Here Title

but I want the same TWIG Code to ouput
Whatever Here Title

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:
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
...
if (!$entry->uri || !Craft::$app->getRequest()->getIsSiteRequest()) {
   return;
}
...


---

