1

I'm using an entry field to set an image transform handle:

{% set format = block.imageCrop %}

Then I'm trying to pass that to an include:

{% include 'partials/_img' with { format: format } %}

This is the _img template:

{% do image.setTransform(format) %}
{{ tag('img', {
  src: image.url,
  width: image.width,
  height: image.height,
  srcset: image.getSrcset(['1.5x', '2x', '3x']),
  alt: image.alt ? image.alt : image.title,
}) }}

This is causing this error: "Setting unknown property: craft\models\ImageTransform::label"

If I change my include to the name of a transform -- with { format: 'square' } -- it works fine.

I'm not sure what the issue is here -- is there an error in my syntax? I have other instances of this include where I do manually specify the transform, rather than using a custom field (with { format: 'square' }), so ideally, I'd like to be able to do it both ways.

2
  • If you're using an entries field, you probably have to do block.imageCrop.one(). Commented Sep 16, 2023 at 4:50
  • It’s just a drop-down field in an entry. I edited the post to clarify.
    – artmem
    Commented Sep 16, 2023 at 13:53

1 Answer 1

1

Dropdown fields don't return the selected option as a string. Instead, they return a SingleOptionFieldData object that you can use to access the value of the selected option as well as its label. The setTransform method doesn't know what to do with that object, so you get an error.

This will give you the value of the selected option as a string:

{% set format = block.imageCrop.value %}

Keep in mind that this may return null if no option is selected, so you may want to set a default:

{% set format = block.imageCrop.value | default('square') %}

In general, if you're not sure what's going on in your template, dumping the value of variables with the dd tag is a good way to find out:

{% set format = block.imageCrop %}
{% dd format %}

This will show you that you're dealing with an object instead of a string in this case.

1
  • 1
    That did it. I didn't know about the dd tag. Thanks!
    – artmem
    Commented Sep 16, 2023 at 23:04

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.