Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 KB | None | 0 0
  1. // Assigns a random style name to each image
  2. function <MODULE_NAME>_views_pre_render(&$view)
  3. {
  4. if($view->name == 'SOME VIEW')
  5. {
  6. $styles = array('thumbnail', 'medium', 'large');
  7. foreach($view->result as &$result)
  8. {
  9. $result->field_field_image[0]['rendered']['#image_style'] = $styles[array_rand($styles)];
  10. }
  11. }
  12. }
  13.  
  14. function <MODULE_NAME>_views_pre_render(ViewExecutable $view)
  15. {
  16. if($view->id() == 'SOME VIEW')
  17. {
  18. $styles = array('thumbnail', 'medium', 'large');
  19. foreach($view->result as &$result)
  20. {
  21. $image = $result->_entity->get("field_media_image");
  22. $image->SOME_FUNCTION('#style_name') = $styles[array_rand($styles)];
  23. }
  24. }
  25. }
  26.  
  27. function <TEMPLATE_NAME>_preprocess_image_formatter(&$variables) {
  28. $styles = array('thumbnail', 'medium', 'large');
  29. if ($variables['image']['#style_name'] === 'medium') {
  30. $variables['image']['#style_name'] = $styles[array_rand($styles)];
  31. }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement