1

Having an issue with HOOK_views_pre_render and HOOK_views_post_render, trying to alter the rendered output of the view. There are similar questions like: Hide view results with hook but I am not seeing the results as I am expecting under $view['result']

There is what looks like a full entity and not the markup that I'm expecting.

enter image description here

  • What are you trying to modify? – Adrian Cid Almaguer Sep 14 '17 at 19:16
  • Sorry, I wanted to modify the views result row. Specifically a field and was exploring the different views hooks as I've used them many times in D7 and expected to see an array of rows in markup. – Blaine Lang Sep 15 '17 at 13:20
  • In my case, the solution that worked was to use the HOOK_preprocess_views_view_field, but during my research, I found links like the one I posted in the question that don't seem to work any longer. I just was not able to see how we would change the views markup in pre or post render hooks other then delete a result row. How do you interact to set/change values in a views ResultRow object? – Blaine Lang Sep 15 '17 at 13:29
0

You can use HOOK_views_post_execute() and cycle through the results rows, updating the row with something like:

        foreach ($viewExecutable->result as $key => $rr) {
          $title = $rr->_entity->getTitle();
          // update $title value...
          $viewExecutable->result[$key]->_entity->set('title', [['value' => $title]]);
        }
  • Thanks very much @jons for the answer. I was getting a PHP error that was causing a white screen of death - Call to undefined method Drupal\\taxonomy\\Entity\\Term::getTitle() which is odd since the title is on a node and not a taxonomy. It was not always throwing this error though. – Blaine Lang Sep 21 '17 at 15:25
1

Thanks @jons, your answer allowed me to move further and I was able to set other fields in the view result now:

// Testing how we can update view result title value
$title = 'This is a custom title';
$newurl = 'http://www.nextide.ca';
$view->result[$key]->_entity->set('title', $title);
$view->result[$key]->_entity->set('body', 'This is a custom content');
$view->result[$key]->_entity->set('field_coned_site_course_url', $newurl);

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.