0

I'm deleting items by means of an unset in hook_views_post_execute, as far as the view I have it configured by 10 items per page.

The problem is that it shows me sometimes 6 or 8, I have searched for options but to reduce the size as when inserting programmed nodes and not when they are missing

/**
 * Implements hooks_views_post_execute().
 */
function module_views_post_execute(ViewExecutable $view) {

    // Remove by region user origin the rows.
    if ($view->id() == 'taxonomy_term' && $view->current_display == 'page_1') {
        $language = \Drupal::languageManager()->getCurrentLanguage()->getId();
        foreach ($view->result as $key => $row) {
            $node = Node::load($row->_entity->id());
            $region_user = $node->getOwner()->field_region->value;

            if ($language != $region_user) {
                unset ($view->result[$key]); // delete a row
            }
        }
    }
}

Code according to references to update the page:

/**
 * Implements hooks_views_pre_render().
 */
function module_views_pre_render(ViewExecutable $view) {

  if ($view->id() == 'taxonomy_term' && $view->current_display == 'page_1') {

    // Rebuild number of items per page.
    $view->pager->total_items = $view->total_rows;
    $view->pager->updatePageInfo();
  }
}

Reference pages consulted: Change views pager programmatically

https://www.drupal.org/forum/support/module-development-and-code-questions/2017-10-10/update-views-result-and-pager-in-hook

https://www.drupal.org/project/views/issues/953072

Debug of $view->result after $view->pager->updatePageInfo();, can see that only 6 items and not 10

enter image description here

  • 3
    Why are you unsetting results? Filters exist for a reason... – No Sssweat Apr 27 at 12:07
  • I have to compare a field of the user that created the node, since the nodes do not have a selected language, with the current language that is in the url to be able to filter the results. – yorchperaza Apr 28 at 22:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.