Assuming there's not a ton of relatedArticles
entries, and considering the fact that there's only one image per entry, the above might not make a huge impact on your performance. To take things a step further, you might consider eager loading the actual Asset field(s) as well as their transforms. There's a bit more code involved withcouple of ways to do this – you could use the same approach, though, as you can't queryabove, adding the eager loading criteria to the Entries field entry.relatedArticles
field directly, but will need to go through:
{% set relatedArticles = entry.relatedArticles.find({
with: [
['bannerImage', {
withTransforms: ['cardImage']
}]
]
}) %}
{% for article in relatedArticles %}
...
{% set bannerImage = article.bannerImage[0] ?? null %}
{% if bannerImage %}
<img src="{{ bannerImage.getUrl('cardImage') }}" />
{% endif %}
{% endfor %}
...or you could use a more "traditional" eager loading approach, using craft.entries
query usingand the relatedTo
parameter. Also worth noting is the slightly different syntax when the Assets are eager loaded (article.bannerImage[0]
instead of article.bannerImage.first()
). Here's how it could look: