1

I want to run a search on Assets that are in use within a particular section. The Assets are added to entries via a Matrix field.

The search term is provided via a url parameter:

{% set searchTerm = craft.request.getParam('q') %}

Somewhat optimistically I tried this:

{% set imagesInUse = craft.assets.relatedTo({
  targetElement: craft.entries({
    section : 'agency'
  }),
  field : imageList.keyImage
}) %}

Where imageList is the Matrix field and keyImage is the Assets field. The block name is imageSpread.

This apparently returns nothing.

For the search part, I'm trying to search within 'imagesInUse':

{% for image in imagesInUse.search('*' ~ searchTerm ~ '*').order('score') %} ... {% endfor %}

It looks like a relatively complex query might be required, but I'm not sure where to go from here.

  • I've broken this down a bit more and discovered that the field imageList is reported as not found. The field name is definitely correct. I've also updated the entry call to filter by a specific entry type, one which definitely has that field present. And finally I've tried targetElement, element and sourceElement. Clearly I'm misunderstanding something here - any help greatly appreciated. – pumkincreative Jan 16 '15 at 12:02
2

Managed to make this work, corrected a couple of things and tidied up, but the main issue was realising the field param has to be a string (duh), not a variable. My code now looks like this:

{% set entries = craft.entries.type('artistFolio').limit(null) %}

{% set imagesInUse = craft.assets.relatedTo({
    sourceElement: entries,
    field: 'imageList'
}) %}

This gives me a model with all images appearing inside the Matrix field 'imageList' within the entry type 'artistFolio'. I can then search these as so:

{% set searchTerm = craft.request.getParam('q') %}
{% set results = imagesInUse.search('*' ~ searchTerm ~ '*').order('score') %}

{% for image in results %}
    ...
{ % endfor %}

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.