Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Hi,
if I add a addWhere() to the query object in hook_views_query_alter(), my items get filtered like desired in the views pager pages. But the number of the items is broken then. There not shown 10 items anymore. But 10 items minus the number of filtered items. I have used the following code to filter the items:
/**
* Implements hook_views_query_alter().
*/
function permissions_by_term_views_query_alter(ViewExecutable $view, QueryPluginBase $query) {
$query = $view->getQuery();
$accessCheck = \Drupal::service('permissions_by_term.access_check');
if (!$accessCheck->canUserBypassAllAccessChecks()) {
$allowed_nids = $accessCheck->getAllowedNidsByCurrentUsersUid();
if (!empty($allowed_nids) && in_array('node_field_data', array_keys($query->relationships))) {
$view->query->addWhere('pbt', 'node_field_data.nid', $allowed_nids, 'IN');
}
}
return $view;
}
What am I doing wrong? How can I ensure to have always the pre-defined number of items on each pager page?
Comments
Comment #2
darrenwh CreditAttribution: darrenwh as a volunteer and at Investis Digital commentedHi, a few questions:
Comment #3
LendudeMoving to the right queue.
Comment #12
dimaboychev CreditAttribution: dimaboychev as a volunteer commentedI can confirm that after doing above in hook_views_query_alter() my page size sometimes is less than what it is set to (less than 10). I suppose in cases when the query alter reduces the results, the pager doesn't recognize that and shows whatever the limit is (10) minus the results that were removed by above query_alter.
EDIT: happens only when query setting distinct is checked.
Comment #17
LendudeIt sounds like #12 might be the thing, that distinct causes this. There are older indications that distinct can cause some problem when looking at the comment in \Drupal\views\Plugin\views\query\Sql::execute
So we might be doing something special for distinct in count queries?
Some confirmation that this is indeed the issue would be great, sorta feels like stabbing in the dark right now.