1

The craft docs on eager loading show you how to eager load image transforms in twig.

{% set entries = craft.entries({
   with: [
        ['coolImage', { withTransforms: ['transform'] }]
    ]
}) %}

However I can't figure out the syntax for achieving the same functionality within a plugin using the ElementCritereaModel.

I've made various attempts but they've all resulted in HUGE EXPLOSIONS of the php error variety.

$criterea->with = [
    "coolImage" => [
        "withTransforms" => ["transform"]
    ],
    "otherAssetField",
    "someCategoryField"
];

Of course above wouldn't work because you're switching between field slugs being keys/values in the array.

Using a similar syntax to the docs in twig would be:

$criterea->with = [
    "teaserImage",
    ["withTransforms" => ["peopleListing"]],
    "peoplePffice",
    "peopleCategories"
];

But that also results in explosions.

Help!!! (thanks)

1

I came across this post while searching for the same answer. Figured it out eventually. For future reference, the correct syntax in a (Craft 2) plugin would be:

$criterea->with = [
    ["coolImage", ["withTransforms" => ["transform"]]],
    "otherAssetField",
    "someCategoryField"
];
| improve this answer | |

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.