Nested views ajax pagination

Created on 12 December 2021, almost 3 years ago
Updated 23 August 2022, about 2 years ago

How can I use ajax pagers with nested views_field_view fields?

I am using Drupal 9.2 and views_field_view to embed a "child" view within a "parent" view. Because there are multiple rows in the parent view, I end up with multiple of the same "child" view being rendered. This prevents the "pagination" from rendering because the "pager id" is the same for the multiple rendered child views.

I am able to circumvent that render issue by programmatically setting the pager id. See code below.

use Drupal\views\ViewExecutable;

function hook_views_pre_view(ViewExecutable $view, $display_id, array &$args) {
  
  if ($view->id() == '<view_id>') {
    $unique_id = $view->args[0]; // argument that is a unique ID
    $view->pager->options['id'] = $unique_id;
  }
}

By using the above code in a module, the child view's pager now appears. However, clicking the pager numbers or buttons results in the parent view updating instead of the child view.

How can I change the pager for the "child" view so that it updates the child view instead of the parent? How can I use ajax pagers with nested views_field_view fields?

Steps to reproduce

  1. Create at least a few "parent" nodes
  2. Create view of "parent" nodes with an ajax pager
  3. Create at least a few "child" nodes referencing parent node (use ref as contextual filter for child view)
  4. Create view of "child" nodes with an ajax pager
  5. Attach child view to parent view via views_field_view
  6. Witness failure of child pager to render
  7. Use hook_views_pre_view to alter pager id making it unique per view
  8. Attempt to use child pager that now renders. See that it doesn't affect correct view, only causing parent view to reload

Also here are a some things I've been looking into, but so far have not helped:

  1. Drupal 7 views fix - not applicable since views moved into core?
  2. Tried a couple other modules which all had their own issues about this or something similar: Views load more issue , Views show more module , Views infinite scroll issue
  3. Within the views_field_view module it seems that it attempts to set a unique id for the pager before using the following $view->initPager(); to initialize the pager. However debugging shows that the ID that is set does not get applied on to the $view. Did something maybe change with initPager? (though the API doesn't make any indication that it has)
🐛 Bug report
Status

Active

Version

1.0

Component

Miscellaneous

Created by

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024