I have a grouped view that displaces content containing multiple entity references. I have a field that generates multiple times, because of those entity references. I've looked into modifying views-view-group.html.twig with no luck. Now I'm trying to figure out how to use hook_views_pre_render to accomplish this.

(If it comes up... no, using 'Views Field View' didn't work)

I currently have results like this:

Group Title
 - Item one
   -- entity-ref1
   -- Field-x
   -- entity-ref2
   -- Field-x

I want this:

Group Title 
 - Item one 
  -- entity-ref-item1
  -- entity-ref-item2
  -- Field x

I've already got the hook set up in a module. I'm just trying to figure out how to use it to rearrange fields and output Field x - After the rest of each entry has output. I haven't found anything.

UPDATE: I tried this solution for removing duplicates - BUT.. it's removing the multiple entity references along with the multiple Field X. I just one Field X pared down to one:

    function webinar_rearrange_views_pre_render(\Drupal\views\ViewExecutable $view) { 
      if ($view->id() == 'webinars') {
        $filtered_nids = array();

        foreach ($view->result as $key => $row) {
          if (in_array($row->nid, $filtered_nids)) {
            unset($view->result[$key]); // remove the duplicate
          } else {
            $filtered_nids[] = $row->nid;

This isn't the answer I was looking for, but it solves the issue until a better method is found.

Basically, the problem is that a specific field - a custom text 'find out more' field was showing three times (for example) when a row had three authors (entity ref. fields)

Since the 'find out more' field was the last item in each row, I overrode the "views-view-unformatted" twig template and, inside of the 'for' loop I added an extra bit, to change the class of the last item. Then, with CSS, I could hide the fields with 'views-row' class, and display 'views-row-last'

Here's what I added to that twig file:

    {% if loop.last %}
        set row_classes = [
          default_row_class ? 'views-row-last',

    {% endif %}

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.