Change field value with hook_views_pre_render() I'm using Views in 8.4.0 and attempting to change the output of a field value with  hook_views_pre_render(). The default value of the field is a node ID. The intent is to replaced the value with a static string depending on a whether or not it's contained in a predefined array using in_array().

The aforementioned is relatively straightforward.

I begin by looping through the results:

  for ($i = 0; $i &lt; count($view-&gt;result); $i++) {
    if (isset($view-&gt;result[$i]-&gt;node_field_data_node__field_lesson_nid)) {
      $viewed = in_array($view-&gt;result[$i]-&gt;node_field_data_node__field_lesson_nid, $viewed) ? 'yes' : 'no';
      // Replace the default Node ID value with $viewed.
      ...
    }
  }


The array $viewed contains a list of predefined node IDs. If the node ID exists in the array "yes" is output otherwise "no".

The problem is how to output the value for each row of results within the View object.

I tried the following at the above ellipsis:

      $view-&gt;field['nid_2']-&gt;options['alter']['text'] = $viewed;
      $view-&gt;field['nid_2']-&gt;options['alter']['alter_text'] = TRUE;


"nid_2" is the token for node_field_data_node__field_lesson_nid.

The problem is this changes the field value for every row. The last row in the results determines the value. (The View also has multiple joins (relationships) which further compounds the complexity.)

So my dilemma is where to replace the value within each row of the $view->result.
I was trying to do something similar as well. I had an integer field and I wanted to replace the integer column with a string value. That was not working. So I had to add a separate string field, maintained the integer field as a hidden field and then overwrote the string field. The code that worked is given below.

function module_views_pre_render(ViewExecutable $view) {
  // Change the comments column in the list of filings table in the view case
  if ($view-&gt;id() == 'my_view') {
    foreach ($view-&gt;result as $key =&gt; $value) {
      $custom_id = $value-&gt;_entity-&gt;get('custom_id')-&gt;value;
      $string = convert_id_to_string_get_user_name($custom_id);
      $value-&gt;_entity-&gt;set('string_field', $string);
    }
  }
}


There is documentation and sample snippets here but looks like it needs more documentation.

https://api.drupal.org/api/drupal/core!modules!views!views.api.php/function/hook_views_pre_render/8.2.x