<p>You can access the field in the entry by using the normal array syntax, <code>$entry[$fieldHandle]</code>. Depending on whether or not the field is eager-loaded,  this will now be either an ElementQuery, which you can run <code>-&gt;all()</code> on, or an array of assets.</p>
<p>When you have the asset, you can use <code>$asset-&gt;getUrl($transform)</code> to get the URL to a transformed image.</p>
<pre><code>use craft\elements\db\ElementQuery;

...

$elements = $entry[$fieldHandle];
$assets = ($elements instanceof ElementQuery) ? $elements-&gt;all() : $elements;

if (is_array($assets) &amp;&amp; !empty($assets)) {
    foreach ($assets as $asset) {
        foreach ($transforms as $transform) {
            $transformedUrl = $asset-&gt;getUrl($transform);
        }
    }
}
</code></pre>

CC BY-SA 4.0
2021-10-07T18:35:20.333Z
Dear André Elvan, thank you so much for your generous input. this is exactly what I was looking for. I finished the module I was creating and it all is working very well. Very much appreciated.
mauricio
CC BY-SA 4.0
2021-10-07T08:37:31.180Z
2021-10-07T08:37:31.180Z
André Elvan
<p>I'm learning to write my own modules, so there's a lot that I still don't know. I have looked at other modules/plugins, videos, but I arrived at a dead end.</p>
<p>I'm writing a module that generates formatted, responsive html tags. The module sets a function that can be invoked from within a template --this is working already. The main arguments it takes are the entry object (<code>entry</code>), the image field handle (<code>fieldHandle</code>) that needs to be queried, and the desired image transforms (<code>transforms</code> array) that are required.</p>
<p>With that I want to obtain the image loaded to the field, and access the urls of the transforms.</p>
<p>I was able to get this to work on a macro, but I'm having a hard time with how to do the same from within a module.</p>
<p>If anyone has an example or suggestions it would be really appreciated.</p>
<p>Thank you</p>
<p>NOTE: just in case, I'm not interested in assets bundles associated with the module. I'm trying to query an asset from a entry uploaded though the back end.</p>

<assets><modules>
Querying media asset in module
# Querying media asset in module

(Asked by: username_0 on 2021-10-06)

I'm learning to write my own modules, so there's a lot that I still don't know. I have looked at other modules/plugins, videos, but I arrived at a dead end.

I'm writing a module that generates formatted, responsive html tags. The module sets a function that can be invoked from within a template --this is working already. The main arguments it takes are the entry object (`entry`), the image field handle (`fieldHandle`) that needs to be queried, and the desired image transforms (`transforms` array) that are required.

With that I want to obtain the image loaded to the field, and access the urls of the transforms.

I was able to get this to work on a macro, but I'm having a hard time with how to do the same from within a module.

If anyone has an example or suggestions it would be really appreciated.

Thank you

NOTE: just in case, I'm not interested in assets bundles associated with the module. I'm trying to query an asset from a entry uploaded though the back end.

**Tags:** assets, modules

---

## 1 answers

### Answer by username_1 on 2021-10-07. Score: 1 | Accepted answer

You can access the field in the entry by using the normal array syntax, $entry[$fieldHandle]. Depending on whether or not the field is eager-loaded,  this will now be either an ElementQuery, which you can run ->all() on, or an array of assets.
When you have the asset, you can use $asset->getUrl($transform) to get the URL to a transformed image.
use craft\elements\db\ElementQuery;

...

$elements = $entry[$fieldHandle];
$assets = ($elements instanceof ElementQuery) ? $elements->all() : $elements;

if (is_array($assets) && !empty($assets)) {
    foreach ($assets as $asset) {
        foreach ($transforms as $transform) {
            $transformedUrl = $asset->getUrl($transform);
        }
    }
}

> **Comments:**
> **username_0**: Dear André Elvan, thank you so much for your generous input. this is exactly what I was looking for. I finished the module I was creating and it all is working very well. Very much appreciated.  

---

