I want to use hook_views_pre_render() to read in a view and change a field output to a clickable link.

Here is code:

function sbn_views_pre_render(ViewExecutable $view) {
    // https://drupal.stackexchange.com/questions/272495/how-to-get-views-field-names-values-for-hook-views-pre-render
    if ($view->id() == 'find_paragraphs') {
      foreach ($view->result as $key => $value) {
        $parent_id_value =  $value->_entity->parent_id->getValue()[0]['value'];
        $parent_type_value =  $value->_entity->parent_type->getValue()[0]['value'];
        $private_files = $value->_entity->field_private_files->getValue()[0]['value'];
        if ($parent_type_value == 'node') {
          $link = '<a href="/node/'.$parent_id_value.'">'.$parent_id_value.'</a>';
          // $value->_entity->set('parent_id', $link);        
        if ($parent_type_value == 'paragraph') {

Formatting the $link value using standard html coding does not work. Any ideas on how to format the link from within this hook so that the rendered field is a clickable link?

| improve this question | | | | |
  • Change the $view->result[$key] instead of $value like $view->result[$key]->_entity->set('parent_id', $link); and see! – berramou Jan 12 at 20:57
  • It still results in field display looking like this: <a href="/node/4">4</a> Instead of an actual link. – SomebodySysop Jan 12 at 23:02
  • 1
    Don't set entity values if you don't indent to save the values to the database. In Views field values are rendered by a field handler and for entity fields this handler contains an advanced renderer which allows you to rewrite fields to anything you want, see for example drupal.stackexchange.com/questions/187843/… – 4k4 Jan 13 at 8:07
  • But, I DO want to save the values to the database. And, I understand, as per drupal.stackexchange.com/questions/187843/… that I can use the Views UI to rewrite the field as a link. The problem is that the UI doesn't allow, as far as I know, conditional rewriting of fields. I only wish to rewrite the field if a certain condition exists (the parent type = node). This is the only reason I am attempting to use the hook. – SomebodySysop Jan 13 at 21:31
  • You don't have to use the output as link option, you can put the HTML of the link and Twig code with conditions in the rewrite field. If you want to do this in PHP then add a custom field handler to Views. – 4k4 Jan 15 at 10:18

I have not been able to figure out how to conditionally change field to link using hook_views_pre_render(), but I did figure out how to do it within the Views UI:

Fields->Parent ID->Rewrite results->Override the output of this field with custom text:

{% if parent_type == 'paragraph' %}
<a href="/find-paragraphs?id={{ parent_id }}">{{ parent_id }}</a>
{% else %}
<a href="/node/{{ parent_id }}">{{ parent_id }}</a>
{% endif %}
| improve this answer | | | | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.