I'm trying to figure out how to modify a view's results with hook_views_pre_render.
How do I get the available values to work with?
I've turned on Devel and Kint, but I don't see any array on the page that includes views stuff.

I've tried blindly finding fields using machine names, but it just breaks the page. I'm sure I'm doing the right thing, I have one field field_byline that I'm able to get just fine with a simple set() to see if I'm able to do anything.

Here's what I've got:

function webinar_rearrange_views_pre_render(ViewExecutable $view) {
  if ($view->id() == 'viewname' && $view->current_display == 'block_1') {
    foreach ($view->result as $key => $value) {
      $value->_entity->set('title', 'test title'); // worked
      $value->_entity->set('body', 'test body'); // worked
      $value->_entity->set('field_byline', 'test byline');
      $value->_entity->set('field_view_node', 'http://test.com'); // DOESN'T WORK - maybe it's an array? How do I do those?
  • which display format fields or content ? – berramou Nov 14 '18 at 22:48
  • I believe fields, if I'm understanding right? – turpentyne Nov 14 '18 at 22:54

How do I get the available values to work with?

To get values from result you can do something like: $value->_entity->title->getValue()[0]['value'];
Here is example to get body and title you can do the same with other fields.

 * Implements hook_views_pre_render().
function mymodule_views_pre_render(ViewExecutable $view) {
  if ($view->id() == 'viewname' && $view->current_display == 'page_1') {
    foreach ($view->result as $key => $value) {
      //- Get title value 
      $title_value =  $value->_entity->title->getValue()[0]['value'];
      //- Get body value
      $body_value =  $value->_entity->body->getValue()[0]['value'];
     //- Get Other field values ...
| 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.