By joekers


2016-11-18 17:46:41 8 Comments

In Drupal 7 I used hook_views_pre_render() to update the image style for specific results. I'm trying to do the same for a view in Drupal 8 but I don't have the same options available in the field.

An example of the code I used before: $view->result[4]->field_field_image[0]['rendered']['#image_style'] = 'image_style_name';

There's no longer a 'rendered' array, just an ImageItem object with the target_id, title, width and height.

1 comments

@tim.plunkett 2016-12-06 00:00:43

The bad news is that you can't use that approach anymore. Due to the performance improvements like this one, fields are all rendered via #pre_render callbacks, and run through the same build process as normal entity rendering.

The good news is that hook_entity_display_build_alter() will run, the very same hook that runs for displaying an entity.

Views doesn't pass any additional context to this hook. Depending on how you were deciding which one to alter, you may still be able to accomplish this.

@joekers 2017-01-19 11:41:48

Unfortunately I wasn't able to accomplish the thing I wanted because I needed more context from views but thanks for the detailed explanation :)

Related Questions

Sponsored Content

2 Answered Questions

[SOLVED] How can I get a display name from the view ID and display ID?

  • 2016-04-09 05:18:47
  • Webdrips
  • 12570 View
  • 9 Score
  • 2 Answer
  • Tags:   views 8

1 Answered Questions

[SOLVED] How to change image style using hook_views_pre_render()

  • 2019-04-23 19:51:28
  • Mike Godin
  • 46 View
  • 1 Score
  • 1 Answer
  • Tags:   views 8 hooks media

1 Answered Questions

[SOLVED] Getting a custom block image field URL in the block Twig template

  • 2016-01-22 16:03:53
  • cheesetriple
  • 4461 View
  • 3 Score
  • 1 Answer
  • Tags:   theming 8 media

1 Answered Questions

[SOLVED] How to remove width and height from image field images in Views?

  • 2014-08-25 23:15:33
  • user56reinstatemonica8
  • 1294 View
  • 3 Score
  • 1 Answer
  • Tags:   views entities

1 Answered Questions

Switch style plugin of a view based on results

  • 2015-10-08 10:26:02
  • MiloŇ° Kroulík
  • 64 View
  • 1 Score
  • 1 Answer
  • Tags:   views

2 Answered Questions

Render a Views table row ID using a field as the ID value

  • 2015-02-12 16:42:26
  • Scott859
  • 195 View
  • 1 Score
  • 2 Answer
  • Tags:   views

3 Answered Questions

[SOLVED] How do I maintain aspect ratio of scaled image with max-width?

  • 2012-05-09 16:21:55
  • iainH
  • 54066 View
  • 5 Score
  • 3 Answer
  • Tags:   media theming

1 Answered Questions

[SOLVED] View sorting programmatically

  • 2014-07-05 19:55:11
  • rreiss
  • 1122 View
  • 1 Score
  • 1 Answer
  • Tags:   7 views

1 Answered Questions

[SOLVED] which hook to change the node order in a view?

  • 2013-04-18 15:09:53
  • FLY
  • 1474 View
  • 2 Score
  • 1 Answer
  • Tags:   hooks views

Sponsored Content