phpdrupaldrupal-viewsdrupal-themingdrupal-8

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


Solution

  • 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