I have a paragraph which shows a view. The editor may set some options as argument filters, sort and show/hide pager. "Normal" fields are used to collect these options and template_preprocess_paragraph__PARAGRAPH_TYPE
actually builds the view. The filtering works well, but I am not able to get rid of the pager if the editor desires to disable it while limiting the amount of items shown at the same time ($view->setItemsPerPage(0);
is not an option here). Here is some simplified code from mytheme.theme:
function mytheme_preprocess_paragraph__content_list(&$variables) {
$view = Views::getView('content_list');
if (!$view) {
return;
}
// Simulate user input:
$order = 'DESC';
$disable_pager = TRUE;
$items_per_page = 12;
$filter_args = [1,2,3,4];
$display_id = 'embed_content_list_title';
$view->setDisplay($display_id);
$view->setHandlerOption($display_id, 'sort', 'title', 'order', $order);
if ($disable_pager) {
$display = $view->getDisplay();
$pager = [
'type' => 'none',
'options' => [
'offset' => 0,
'items_per_page' => 50,
],
];
// This doesn't work:
$display->setOption('pager', $pager);
$display->setOverride('pager', $pager);
// This neither works:
$view->display_handler->setOption('pager', $pager);
$view->display_handler->setOverride('pager', $pager);
// I also had no luck with:
$view->pager = NULL;
$view->setItemsPerPage($items_per_page);
}
$view->execute();
$variables['content']['content_list'] = $view->buildRenderable($display_id, [$filter_args]);
}
How can the pager be disabled in above code? A workaround might be using different displays, one with pager enabled and another with pager disabled. But I would prefer to solve it with just one display (as I already have multiple displays and would have to build both versions for each display).