How can I add custom classes to each view's generated row? Is it possible by using the theme preprocessor add some classes to views generated rows?

For example: based on Drupal site about default theme implementation to display a view of unformatted rows.views-view-unformatted.html.twig:


  Available variables:
  - title: The title of this group of rows. May be empty. 
  - rows: A list of the view's row items. 
  - attributes: The row's HTML attributes. 
  - content: The row's content.
  - view: The view object.
  - default_row_class: A flag indicating whether default classes
     should be used on rows.


I know that it's possible and we can add our desired classes to each row inside the views-view-unformatted.html.twig:

{% if title %}
  &lt;h3&gt;{{ title }}&lt;/h3&gt;
{% endif %}
{% for row in rows %}
  {%
    set row_classes = [
      default_row_class ? 'views-row',
    ]
  %}
  &lt;div{{ row.attributes.addClass(row_classes) }}&gt;
    {{ row.content }}
  &lt;/div&gt;
{% endfor %}


But I want to understand is it possible to do the same thing for custom views like front-page by use of:function template_preprocess_views_view_unformatted__frontpage in particularor in other template_preprocess_views_view generally?
If you look at /core/modules/views/views.theme.inc at template_preprocess_views_view_unformatted you'll see this code:

function template_preprocess_views_view_unformatted(&amp;$variables) {
  $view = $variables['view'];
  $rows = $variables['rows'];
  $style = $view-&gt;style_plugin;
  $options = $style-&gt;options;

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


This is how if you have set a row class in the view, it gets added.



And the resulting output:



You can copy this function to your custom module and customize it as you like.  For example, this will add the "views-row-" plus the name of your view cleaned up using the pathauto cleanstring utility:

function mymodule_preprocess_views_view_unformatted(&amp;$variables) {
  $view = $variables['view'];
  $rows = $variables['rows'];

  foreach ($rows as $id =&gt; $row) {
    // Add custom class to each row that encorporates the view id.
    $custom_row_class = \Drupal::service('pathauto.alias_cleaner')
      -&gt;cleanString($view-&gt;id());
    $custom_row_class = 'views-row-' . $custom_row_class;
    $variables['rows'][$id]['attributes']-&gt;addClass($custom_row_class);
  }
}


If you would like to add classes to a views rows from hook_preprocess_views_view, you can do it this way:

function mitsloan_global_preprocess_views_view(&amp;$variables) {
  $view = $variables['view'];

  if ($view-&gt;id() == 'testview') {

    $row_style_plugin = &amp;$variables['rows'][0]['#view']-&gt;style_plugin;

    $custom_row_class = 'views-row-' . \Drupal::service('pathauto.alias_cleaner')
        -&gt;cleanString($view-&gt;id());

    $row_style_plugin-&gt;options['row_class'] = $custom_row_class;
  }

}