Drupal 8: views_query_alter - setLimit()

Submitted by Calvin on Fri, 04/19/2019 - 12:16

If you need change the limit of a views query dynamically it would seem obvious to use:

$query->setOffset($offset);
$query->setLimit($limit);

However, the pager overrides this.

To make this work use the following:

$view->setOffset($offset);
$view->getPager()->setItemsPerPage($limit);

Note these methods are on the $view object and not the $query object.