简体   繁体   中英

Drupal 8 change image style in first row on views list

I have created view that displays on page 10 newest article. I have in row two fields: image and content. In settings of image field I chose image style (for example: medium). How can I change image style to another (example: large) only in first row?

I have tried it in preprocess but i don't know where is stored information about image style:

function theme_preprocess_views_view_unformatted__lista_depesz_default(&$variables) {
$view = $variables['view'];
$rows = $variables['rows'];
$style = $view->style_plugin;
$options = $style->options;

$variables['default_row_class'] = !empty($options['default_row_class']);
foreach ($rows as $id => $row) {     $variables['rows'][$id] = array();
    $variables['rows'][$id]['content'] = $row;
    $variables['rows'][$id]['attributes'] = new Attribute();
    if ($row_class = $view->style_plugin->getRowClass($id)) {
      $variables['rows'][$id]['attributes']->addClass($row_class);
    }

    if ($id == 0 && $row['content']['#row']->_entity->field_image[0] != NULL) {
       //some php code to change image style
    }
}
}

Regards

Regarding your code and your explanations, I'm not sure to understand what you are trying to achieve.

1/ you try to add a CSS class to the 1st image of your view. Why not using the following CSS path .my_view .first img {}

2/ if you try to call another image style, can you create a view with only the 1st item, or a promoted? Then a second view with the rest of the items ?

3/ if you try to call another image style, you can do it without any code. You install the module http://drupal.org/project/views_conditional then you add 2 images with 2 different image style, and you apply your condition inside the condition fieldset.

I really prefer the solution 3 because it's pure Drupal build.

I hope it helps.

Cheers

You can create view with original images, and set style inside your twig files, using for example twig tweak: https://www.drupal.org/project/twig_tweak

Inside twig file you can set any style with conditioning

{{ 'public://images/ocean.jpg' | image_style('thumbnail') }}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM