-1

I want to change drupal 8 views field value before displaying. I am using below hook_views_pre_render

function MYMODULE_views_pre_render(ViewExecutable $view) {
  if ($view->id() == "demo1" && $view->current_display == 'page_demo1') {
     foreach($view->result as $value){
      $value->_entity->set('created', $value->_entity->get('created')->value . '-' .($value->_entity->get('changed')->value - $value->_entity->get('created')->value));      
     }
  }
}

I want to time difference between created and changed date time.

but its not working...

1 Answer 1

5

You can convert them to int and work with them as integers. For example if you want to set created to changed - created. Here is a working example:


use Drupal\views\ViewExecutable;

function MYMODULE_views_pre_render(ViewExecutable $view){
  if ($view->id() == "views_id" && $view->current_display == 'view_display') {
    foreach ($view->result as $value) {
      $created = (int) $value->_entity->get('created')->value;
      $changed = (int) $value->_entity->get('changed')->value;
      //- If you want for example to set created time with changed - 
      created.
      $value->_entity->set('created', $changed - $created);
    }
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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