Programmatically setting a link I am trying to programmatically set a link in a Drupal 8 view. The client wants an external link used if a certain link field is set, otherwise default the link to the node.

I am trying to figure out how to set this, and despite looking at hook_views_pre_render() and hook_views_post_render(), I'm not seeing any method to make this happen. I'm trying to go into result -

foreach ($view-&gt;result as $key =&gt; $value) {

}


However Kint is giving me data like this:

public _relationship_entities -&gt; array(0)
public index -&gt; integer7
public node_field_data_langcode -&gt; string(2) "en"
public nid -&gt; string(2) "99"


for each ResultRow.

I don't remember this being particularly difficult in Drupal 7 - is there something I'm missing here? How do I programmatically set a link in Drupal 8 dependent on certain conditions for a view row/field in a row?
The hook you're looking for is hook_views_view_field().

This hook allows you to alter the output of the field, and still is early enough to have the entity data for for that field.

In the example below, I'm checking if the node has a link field, field_my_link set, and if it's set, I alter the title output (which is normally linked to the node) to output to the link field's uri, but only if it's an external link.

function mymodule_preprocess_views_view_field(&amp;$vars) {
  if ($vars['view']-&gt;id() != 'testview') {
    return;
  }
  $node = $vars['row']-&gt;_entity;
  if ($vars['field']-&gt;getField() == '.title' &amp;&amp; $node-&gt;field_my_link-&gt;isEmpty() === FALSE) {
    if ($node-&gt;field_my_link[0]-&gt;isExternal()) {
      $link = $node-&gt;field_my_link[0]-&gt;view();
      $link['#title'] = $node-&gt;label();
      $vars['output'] = $link;
    }
  }
}


If you want to alter the output of one views field based on the output of another, use hook_views_view_fields():

function mymodule_preprocess_views_view_fields(&amp;$vars) {
  if ($vars['view']-&gt;id() != 'testview') {
    return;
  }

  // Cast Markup object to string.
  $field_one_content = (string) $vars['fields']['field_1']-&gt;content;

  // Check if first field contains a substring, if so, update second field.
  if (strpos($field_one_content, 'my search string') &gt; -1) {
    $vars['fields']['field_2']-&gt;content = \Drupal\Core\Render\Markup::create('hello world');
  }

}