0

Im trying to build a complicated grid ui with a different image size and aspect ratio for each consecutive item in the list. How can I select a different image style for each item?

1 Answer 1

0

First, you need to go one level up, don't think about image styles, think about enity view modes. Then you can control which image style is used for each view mode.

The reason for that is that entity output is cached by view mode. You could theoretically alter each entity and switch out the view mode, the data might actually even be available there, but it will fall apart when the same entity is suddenly in a different position but still cached.

You could try to implement preprocess or another views hook to change the view modes programatically in the render array. That will however not be configurable if that is a requirement.

What we build for our project is a views display plugin that allows to not select a single view mode but one for each entity that is being displayed.

2
  • I think the preprocess hook is what I had in mind, I dont need it to be configurable, Im hoping to find the simplest approach that allows me to restructure the size of the grid items. I was thinking some sort of sequential iteration. Even if I could just get the first one in the list to use a different image style. Since this will be for a contributed theme, less configuration is better and I want to avoid setting up a separate view mode for each style if possible. Can you give me an example of preprocess hook? i still havent figured out how preprocess hooks work, thank you! Oct 23, 2016 at 16:19
  • I found this answer using display suite to choose the view modes: drupal.stackexchange.com/questions/151026/… My only issue is that Im trying to set up this theme in a way that makes it easy to configure and preferrably would work without complicated setup. thats why i was hoping for just the image style but some things are just trickier to set up. Oct 23, 2016 at 16:33

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.