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

jepSter created an issue. See original summary.

darrenwh’s picture

Hi, a few questions:

  1. Why are you returning the $view this is not required?
  2. At the end of your query after doing your alter what is the value of $query->getLimit() is it still set to 10?
Lendude’s picture

Project: Views (for Drupal 7) » Drupal core
Version: 8.x-3.x-dev » 8.3.x-dev
Component: Miscellaneous » views.module

Moving to the right queue.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

dimaboychev’s picture

  if ($view->id() === 'my_view_id') {
    $definition = [
      'table' => 'my_custom_table',
      'field' => 'my_custom_field',
      'left_table' => 'my_custom_table_2',
      'left_field' => 'my_custom_field_2',
      'type' => 'INNER'
    ];

    $join = Drupal::service('plugin.manager.views.join')->createInstance('standard', $definition);
    $query->addRelationship('my_custom_table', $join, 'my_custom_table');
    $query->addWhere('', 'my_custom_table.uid', \Drupal::currentUser()->id(), '=');
    $query->addOrderBy('my_custom_table', 'submit_timestamp', 'DESC');
  }

I 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.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Lendude’s picture

Status: Active » Postponed (maintainer needs more info)

It 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

// Count queries must be run through the preExecute() method.
      // If not, then hook_query_node_access_alter() may munge the count by
      // adding a distinct against an empty query string
      // (e.g. COUNT DISTINCT(1) ...) and no pager will return.
      // See \Drupal\Core\Database\Query\PagerSelectExtender::execute()
      // See https://www.drupal.org/node/1046170.

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.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.