1

How can I search for assets containing a custom field? The field on the asset is called lang. When I use this code no assets are found.

{% set folderId = craft.app.request.getQueryParam('folderId') %}
{% set query = craft.app.request.getQueryParam('q') %}
{% set lang = craft.app.request.getQueryParam('lang') %}

{# Search for assets in the downlaoads folder #}
{% set assets = craft.assets().volume('downloads') %}
{% set assets = assets.folderId(folderId).includeSubFolders() %}

{% set searchQuery = [] %}

{# Check if a query was supplied, if so then search for it. #}
{% if query is not empty %}
{%     set searchQuery = searchQuery|merge({query: query}) %}
{% endif %}

{% if lang is not empty %}
{%   set searchQuery = searchQuery|merge({query: 'lang:' ~ lang}) %}
{% endif %}

{% paginate assets.search(searchQuery).limit(10) as pageInfo, pageEntries %}

I also tried:

{% set query = query ~ ' lang: ' ~lang %}
{% paginate assets.search(query).limit(10) as pageInfo, pageEntries %}

If I remove the lang part results will show up.

The field is a Dropdown field with languages. Value is ISO code and Label the language name. The variable lang in the code contains nl, en etc.

3
  • Is your custom field searchable?
    – Wallace
    Commented May 6, 2021 at 11:04
  • I checked 'Use this field’s values as search keywords' but that didn't help. The field is a Dropdown field with languages. Value is ISO code and Label the language name. The variable lang in the code contains nl, en etc. Commented May 6, 2021 at 11:38
  • If your values are known/fixed then I'd consider adding that as an element query, then searching on the results of that?
    – Wallace
    Commented May 6, 2021 at 14:38

1 Answer 1

1

Caveat: This is totally untested, and could be nonsense. I'm sure you'll let me know. It assumes that the Craft element query methods can be used here.

If you have a 'lang' value, add a where to your basic assets element query

    {% set folderId = craft.app.request.getQueryParam('folderId') %}
    {% set query = craft.app.request.getQueryParam('q') %}
    {% set lang = craft.app.request.getQueryParam('lang') %}
    
    {# Search for assets in the downloads folder #}
    {% set assetsQuery = craft.assets().volume('downloads') %}
    {% set assetsQuery = assets.folderId(folderId).includeSubFolders() %}

    {# If there's a lang value, add a where to the element query #}
    {% if lang is not empty %}
    {%     set assetsQuery = assetsQuery.where(expression('[[field_lang]] = :lang', {lang: lang})) %}
    {% endif %}
    
    {% set searchQuery = [] %}

    {# Check if a query was supplied, if so then search for it. #}
    {% if query is not empty %}
    {%     set searchQuery = searchQuery|merge({query: query}) %}
    {% endif %}
    
    {% paginate assetsQuery.search(searchQuery).limit(10) as pageInfo, pageEntries %}

*Note that I've changed the variable 'assets' to 'assetsQuery'

3
  • Got an exception Twig\Error\SyntaxError: An array element must be followed by a comma. Unexpected token "arrow function" of value "=>" ("punctuation" expected with value ","). Looks like the arrow function in the where clause isn't allowed. We are on Craft version 3.6.13 Commented May 10, 2021 at 12:26
  • 1
    Looks like it needs to be an Expression, so try set assetsQuery = assetsQuery.where( expression('[[lang]] = :lang', {lang: lang}) );
    – Wallace
    Commented May 10, 2021 at 13:45
  • Thanks Andy, this is working great. Had to change [[lang]] to [[field_lang]] and it worked. Commented May 10, 2021 at 19:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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