spacecatninja / craft-imager-x

Image transforms, optimizations and manipulations for your Craft CMS site.
Other
26 stars 16 forks source link

Craft CMS 5 matrix automatic generation of transforms #270

Closed ArtDepartmentMJ closed 1 month ago

ArtDepartmentMJ commented 1 month ago

Not really and issue but more of a request to update the docs...

I've been struggling a lot with this and I think it is mainly due to my lack of understanding but also the docs are quite misleading when it comes to Craft 5 and the new way matrix fields work with entry types (unless I'm missing something).

If I have a block in a matrix field with an image field that needs a transform, previously I would conifgure it like this:

imager-x-generate.php

return [
    'elements' => [
        [
            'elementType' =>  Entry::class,
            'criteria' => [
                'section' => 'home',
            ],
            'fields' => ['matrixFieldHandle:blockHandle.imageFieldHandle'],
            'transforms' => ['imageTransform']
        ],
    ],
];

Through trial and a lot of error, I've figured out (I think) that we shouldn't be targeting the matrix field at all but the matrix entry (block) itself like so:

imager-x-generate.php

return [
    'elements' => [
        [
            'elementType' =>  Entry::class,
            'criteria' => [
                'type' => 'entryTypeHandle',
            ],
            'fields' => ['imageFieldHandle'],
            'transforms' => ['imageTransform']
        ],
];

Is this the correct way to do this now? If so, do you think the docs should be updated?

Additional info

aelvan commented 1 month ago

Hi,

Sorry, this was a stupid bug, I just pushed 5.0.2 where matrixFieldHandle:entryType.imageFieldHandle should work as expected (ie as in Craft 4).

Targeting the entry type directly is definitively going to be a useful way of doing auto generation i Craft 5, and I'll try to improve the docs on that. But I think going through the matrix field will be useful too, I imagine that in many cases the transforms will depend on the context of where a given entry type is used, in which case you'd need to do it the same way as you did in Craft 4.

ArtDepartmentMJ commented 1 month ago

Thanks for jumping on it so quick!

I imagine that in many cases the transforms will depend on the context of where a given entry type is used

Yeah I can definitley see that although in my experience so far with Craft 5 you end up with a load of entry types specifically for the matrix field itself.

aelvan commented 1 month ago

Yeah, I'm only a couple of projects into Craft 5 at the moment, so I feel like I haven't totally cracked what the best practices will be yet. Hopefully in time it will get clearer, and I can improve the documentation to reflect that. :)