Add custom class to views row depending on field value I'm using views to show different fields of an entity. If the uid of the entity is the current user I would like to add a custom class to the entire row (not only to the field).

I wrote the following preprocess function that works fine if I use the entity in views, but I don't know how to change it when I use fields instead:

function my_module_preprocess_views_view_unformatted(&amp;$variables) {

  $view = $variables['view'];

  if ($view-&gt;id() == 'my_view' &amp;&amp; $view-&gt;current_display == 'page_1') {

    $rows = $variables['rows'];
    foreach ($rows as $id =&gt; $row) {

      $uid = \Drupal::currentUser()-&gt;id();

      $owner = $row['content']['uid']['#object']-&gt;get('uid')-&gt;getString();

      $class = ($uid == $owner) ? 'own' : 'other';

      $custom_row_class = 'views-row-' . $class;
      $variables['rows'][$id]['attributes']-&gt;addClass($custom_row_class);
    }

  }
}
Can you please try the following code snippet. I swapped the line to retrieve the author's user ID.
/**
 * Implements hook_preprocess_HOOK().
 */
function MYMODULE_preprocess_views_view_unformatted(&amp;$variables) {

  $view = $variables['view'];
  if ($view-&gt;id() == 'MY_VIEW' &amp;&amp; $view-&gt;current_display == 'MY_DISPLAY') {

    $uid = \Drupal::currentUser()-&gt;id();

    $rows = $variables['rows'];
    foreach ($rows as $id =&gt; $row) {

      // Get user ID.
      $owner = $row['content']['#row']-&gt;_entity-&gt;get('uid')-&gt;target_id;

      $class = ($uid == $owner) ? 'own' : 'other';
      $custom_row_class = 'views-row-' . $class;

      $variables['rows'][$id]['attributes']-&gt;addClass($custom_row_class);
    }
  }
}