This is a simple example of the use of Views hooks in the template.php file of a theme. It is written for use with Drupal 7 and Views 7.x-3.0-beta3.

In this project, a page contains a View which must display one, two or three results/rows. Each row contains an image and and body text. The final output display must to stay roughly the same size regardless of the amount of content. To make it all fit and allow for custom theming/CSS, I decided to change the Image Style and Row Class depending on the number of results.

I’ll use the Views hook function hook_views_pre_render() which runs after the View query has run, but before the View is rendered. This allows me to check the number of results and make adjustments to the theming settings as required.

Put the following in your theme’s template.php file, then adjust as needed:

/*
 * Implements hook_views_pre_render()
 */
function [theme_name]_views_pre_render(&$view) {
  if ($view->name=='[view_name]' && $view->current_display=='[view_display_name]') {
    //Change the Photo style and row class depending on the number of results
    switch (count($view->result)) {
      case 1:
        //Change the Image Style to the large format when the space is available.
        $view->field['field_photo']->options['settings']['image_style'] = 'photo_large';
        //Changing the row class in two areas. This appears to be enough to create the change.
        $view->style_options['row_class'] = $view->style_plugin->options['row_class'] = 'display-1';
        break;
      case 2:
        $view->field['field_photo']->options['settings']['image_style'] = 'photo_med';
        $view->style_options['row_class'] = $view->style_plugin->options['row_class'] = 'display-2';
        break;
      case 3:
        $view->field['field_photo']->options['settings']['image_style'] = 'photo_small';
        $view->style_options['row_class'] = $view->style_plugin->options['row_class'] = 'display-3';
        break;
    }
  }
}

The devel module supplied dpm() function will display the data within the View object. Usage: dpm($view);

Comments

(Statically copied from previous site)

Jean replied on January 5, 2012 - 6:42pm

Hi,

I’m trying your code and for some reason even if using dpm I can see that the image_style value has correctly been set the image itself still uses the old image style.

Any clue why?

brad replied on January 5, 2012 - 6:55pm

I wouldn’t be surprised if there has been a change to Views since Views 7.x-3.0-beta3. Try putting a dpm() in the image theming functions themselves. You want to check the value of the data before it is rendered. The newer version of Views might have changed the data source. Hmm.. I just checked the site I originally built this functionality for it, and it is working correctly.

Jean replied on January 10, 2012 - 5:33pm

I tried doing this to make sure it wasn’t a logic issue or something :

function mytheme_views_pre_render(&$view) {
    if ($view->name == "myviewname") {
        $view->field['field_logo']->options['settings']['image_style'] = 'large';
    }
}

Cleared Drupal cache and browser cache. No luck. When looking at the folder the images are not even getting created either.

brad replied on January 10, 2012 - 9:14pm

My example code assumes the images already have styles set. The code you’ve posted wouldn’t do anything, unless you already have the image style set to medium, or something else. Do you already have image styles setup in the view?

fabio replied on March 21, 2012 - 6:34am

Hi, I tried the piece of code you posted to change the image style in the first result of a view I created, cleared the Drupal and browser cache, and already have an image style set for the field in the view administration panel. If I dpm the current value of the image_style of the first result, it says me that it has been changed, but the image has not been formatted with the format I set. Am I missing something?

brad replied on March 23, 2012 - 3:28pm

Check the output of dpm to make sure the image_style hasn’t been set elsewhere. I wrote this nearly a year ago, so Views may have slightly changed since then.

Tom Cant replied on July 10, 2012 - 3:55am

At the time of this writing Views responds to the above code if you place it in the pre_build hook instead. I have used the following code with success:

function [module-name]_views_pre_build(&$view) {
  if ($view->name == '[view-name]') {
    $view->field['[image-field-name]']->options['settings']['image_style'] = '[preset- name]';
  }
}

I hope that helps someone.

baddlan replied on August 27, 2012 - 1:06pm

Using hook_views_pre_render The following snippet demonstrates how to apply a different image style for each result produced by the View. In this case, styles are simply picked randomly.

function <MODULE_NAME>_views_pre_render(&$view)
{
  if($view->name == 'SOME VIEW')
  {
    $styles = array('thumbnail', 'medium', 'large');

    foreach($view->resultas &$result)
    {
      $result->field_field_image[0]['rendered']['#image_style'] = $styles[array_rand($styles)];
    }
  }
}

Do not forget to replace the name of the image field field_field_image with your own. If you use the Devel module, you may want to inspect the $result object using kpr($result).