1

In Craft 3, one of my 'User' fields is an assets type field with one selectable asset ('profilePicture').

I have routing setup to load my template when a url looks like:

domain.com/person/firstname-lastname

In my template I am getting the appropriate user with:

{% set secondSegment = craft.request.getSegment(2) %}

{% set people = craft.users.group('family') %}

{% for person in people %}
    {% set personName = person.firstName|lower ~ "-" ~ person.lastName|lower %}
    {% if secondSegment == personName %}
        {% set currentUser = person %}
    {% endif %}
{% endfor %}

All that works - I can query most fields, but I can't work out how to get my asset.

Works:

{{ currentUser.myTextField }}

None of these work (returns nothing and no error):

1.

{{ currentUser.profilePicture[0].url }}

2.

{% set asset = currentUser.profilePicture.first() %}

{% if asset %}
    {{ asset.getUrl }}
{% endif %}

3.

{% for asset in currentUser.profilePicture %}
    {{ asset.getUrl }}
{% endfor %}
2
  • Did you enable URLs for this asset sources? If not the getUrl function will return null Feb 5, 2018 at 5:51
  • @RobinSchambach Arg! <Smacks head> Thanks! If you want to write it up I'll mark it as correct. Feb 5, 2018 at 9:04

1 Answer 1

1

You have to make sure you enabled the option Assets in this volume have public urls in your volume settings at /admin/settings/assets/volumes/.

This checkbox is disabled by default thus your getUrl() function will always return an empty string/null.

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.