0

I am trying to set an offset for some filtered view results, and the offset should only apply when the filters are not set. Here's my attempt:

/**
 * Implements hook_views_pre_render().
 */
function mysite_views_pre_render(\Drupal\views\ViewExecutable $view) {

  if ('related_content' == $view->id()) {
    if ('All' == $view->exposed_raw_input['field_term_region_target_id'] &&
        'All' == $view->exposed_raw_input['field_term_center_target_id']) {
      \Drupal::logger('hook_views_pre_render')->notice('The hook executed.');
      $view->result = array_slice($view->result, 4);
    }
  }
}

The above code creates an offset of 8, not 4. Whatever number I use in array_slice, Drupal renders the output as if that number is doubled.

As you can see, I added a watchdog to the code to see if the hook actually fires twice, and it does not. It only fires once, which is good. But somehow the output is rendered with double the offset it should get.

Because I want the offset to be 4, I could use 2 as the argument in array_slice and be done with it, but I want to understand what's happening.

(I can't set the offset in the view config, because there should only be an offset when the filters are not set.)

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.