Alter value of view field using hook in Drupal 8

author details
AdiPie
30th Jul 2020
1 min read
Image
view-field-alter

Instructions to make custom code for alter the field value of a perticular view utilizing hooks in your custom module. 

Like we need to alter view field for username.

First we can put below code in header of the custom module.

 use Drupal\views\ViewExecutable;

At that point, use alter hooks in you custom.module page

function custom_views_pre_render(ViewExecutable $view) {
   
    $usernamename =  \Drupal::currentUser()->getUsername();

    $viewresult = $view->result;


    if ($viewresult == "your view id") 
      foreach($viewresult as $row){
        $row->_entity->set('name', $usernamename);
      }
}

 

With the assistance of above sample code you can resolve your issue for altering the value of the field which is fetch from the view.