3

We have a channel of entries which all include the same asset field.

We are trying to fetch all assets that are actively in use, ergo the assets which have been linked to any of the entries in the channel via the specified asset field.

If possible we would also like to also get the title(s) of the entries to which the asset is linked, as part of the query.

This is happening in a module (PHP) not template (Twig).

Context: gallery of photos (assets) uploaded by teams (channel entries).

1

You can use Relations to find assets which are related to a set of entries (i.e. used in a field of the entries). Specifically, the relatedTo parameter allows you to limit an asset query to assets that are used in a specific entry or set of entries. One caveat is that you have to load the entries which contain the assets field first (at least as far as I can tell from the documentation).

So you need to queries, one to get all entries with assets on them and one to get all assets:

$teams = Entry::find()
    // replace with the name of the section your asset field is used on
    ->section('teams')
    // replace with name of your image field; find only entries with images in your image field 
    ->my_image_field(':notempty:')
    ->all();

$assetsUsedInTeams = Asset::find()
    ->relatedTo([
        'element' => $teams,
        'field' => 'my_image_field',
    ])
    ->all();

Now getting the titles of entries where the asset is used is slightly more involved. You can use the same approach, but you will probably need another query for each image to find related entries. For example:

foreach ($assetsUsedInTeams as $asset) {
    $entriesContainingAsset = Entry::find()
        ->relatedTo([
            'targetElement' => $asset,
            'field' => 'my_image_field'
        ])
        ->all();
}

Not sure about that last query, might need some adjustments to work properly. Also, those are a lot of N+1 queries, so once it's working as you want it to make sure to optimize your queries with Eager-loading and maybe add some caching as well.

5
  • 1
    Thank you for this response, this is extremely helpful. I did not realise that you can parse a set of entries into the targetElement param when using relatedTo()
    – Jake
    Apr 15 at 11:27
  • @Jake You're welcome! That info is a bit hidden, it's described under Simple Relationships: Passing an array of elements returns results relating to any one of the supplied items, meaning one or the other by default
    – MoritzLost
    Apr 15 at 11:32
  • Ah I see it now, thanks for the link. I don't currently have the reputation required to make your answer the official one, but will do it when I can!
    – Jake
    Apr 15 at 16:50
  • @Jake Glad I could help! Upvoting requires 15 reputation, but accepting an answer does not, you should see a little gray checkbox below the up/downvote button you can use to mark the answer as accepted :)
    – MoritzLost
    Apr 15 at 18:21
  • 1
    My bad! Thanks again.
    – Jake
    Apr 16 at 21:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.