3

First I tried with views_conditional (which was already installed on the site) to check for 1st row and output the specific image but it does not seem to work as per this issue

Ideally I want to do it without installing an extra module.

I tried with THEMENAME_preprocess_views_view_field but the image was already rendered at this stage. Have used the somewhat hackish str_replace but it does not actually work.

function THEMENAME_preprocess_views_view_field(&$vars) {
  if($vars['view']->element['#name'] == "free_write" 
      && $vars['field']->field == "field_main_image" 
      && $vars['row']->index == 1 
      ){
    $html = str_replace("teaser_media_small","teaser_media_medium",$vars["output"]->__toString());
    $vars["output"] = Drupal\Core\Render\Markup::create($html);
  }
}

what is the proper way to do this in a preprocess function or perhaps in a twig template ?

1
  • 2
    I think the solution from @Adrian Cid Almaguer is the best for your field based view. You might consider for better performance and theming a view displaying content and then switch view modes in a row style plugin, see this module drupal.org/project/views_parity_row
    – 4uk4
    Mar 1, 2018 at 20:02

2 Answers 2

4

A solution can be Add an Attachment Display to the View, in the Attachment you can select to show only one element with a different style and you can give as position Before.

enter image description here

In the Page then you select to skip 1 element.

enter image description here

This will output something like this: enter image description here

3

Another option, is to simply add your image field again and give it the other image style and only print this field in the first row via preprocess function.

If you want to use twig, you can add "count" as a field and use that to know which row you're on in your conditional.

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.