5

I'm trying to figure out how to render a view's pager programmatically. I'm renderering my view programmatically as follows:

$viewname = 'example_view';
$viewdisplay = 'block';

// get the view machine id
$view = Views::getView($viewname);
// set the display machine id
$view->setDisplay($viewdisplay);
$view->render();

$renderer = $view->rowPlugin;
foreach ($view->result as $index => $row) {
  $view->row_index = $index;
  $vars['content']['view_results'][$index] = $renderer->render($row);
}

So now I need to render the pager, but not having any luck. I've tried something like:

$pagerrenderer = $view->pager;
$vars['content']['view_pager'] = $pagerrenderer->render($view->pager);

but then end up with the following error:

Recoverable fatal error: Argument 1 passed to pager_query_add_page() must be of the type array, object given, called in /www/projectname/docroot/core/includes/pager.inc on line 246 and defined in pager_query_add_page() (line 311 of core/includes/pager.inc).

Can anyone point me in the right direction?

1
  • Add per the documentation api.drupal.org/api/drupal/… you should pass extra GET information to the render method and not the pager itself.
    – Eyal
    Jun 17, 2016 at 11:46

1 Answer 1

7

The object returned from $views->pager() is an object implementing ViewsPluginInterface and extending PagerPluginBase. If you look at the parameters PagerPluginBase::render() accepts, you will see they are:

$input: Any extra GET parameters that should be retained, such as exposed input.

If you look at Full::render(), you have an idea of how that parameter is used.

  $tags = array(
    0 => $this->options['tags']['first'],
    1 => $this->options['tags']['previous'],
    3 => $this->options['tags']['next'],
    4 => $this->options['tags']['last'],
  );
  return array(
    '#theme' => $this->themeFunctions(),
    '#tags' => $tags,
    '#element' => $this->options['id'],
    '#parameters' => $input,
    '#quantity' => $this->options['quantity'],
    '#route_name' => !empty($this->view->live_preview) ? '<current>' : '<none>',
  );

In any case, you don't pass to the method of that object the object itself.

1
  • 1
    Ah, Thanks for this - it definitely helped me understand my problem. I managed to get my pager using $input = array(); $vars['content']['view_pager'] = $view->pager->render($input);
    – Janey
    Jun 17, 2016 at 12:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.