2

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

1

Piggybacking off How to set views pager item size programmatically? and looking at PagerPluginBase.

To change values

if ($disable_pager == FALSE) {
  $view->setOffset(0);
  $view->setItemsPerPage($items_per_page);
}

Now to answer your question

How can the pager be disabled in above code?

You can disable it by setting items per page to zero.

$view->setItemsPerPage(0);

If you want to limit the amount of items without pager, all you have to do is unset it.

   $view = Views::getView('content_list');
   $display_id = 'embed_content_list';
   $view->setDisplay($display_id);
   $view->setItemsPerPage(12);
   $view->execute();
   unset($view->pager); // must put this unset after the $view->execute();
   $variables['content']['content_list'] = $view->buildRenderable($display_id, [$filter_args]);
  • Thanks @no-sssweat for your answer. if ($view->pager) {$view->pager->setItemsPerPage(0);} didn't work. $view->setItemsPerPage(); works to disable the pager but shows all items, what is not desired, the editor still should be able to limit the amount of items to show but disable the pager. It was my fault, the question was vague about that, so I modified it. – LarS Apr 23 at 13:41
  • @LarS try $view->pager->$total_items = 12; – No Sssweat Apr 23 at 23:29
  • $view->pager == null do I need to call something else before? I did: $view = Views::getView('content_list'); $display_id = 'embed_content_list'; $view->setDisplay($display_id); if ($view->pager) { $view->pager->setItemsPerPage(0); $view->pager->total_items = 50; } – LarS Apr 24 at 23:34
  • @LarS I finally tried this myself rather than best guess. Updated my answer. – No Sssweat Apr 25 at 9:17
  • Unsetting pager between execute and build works fine, thank you @no-sssweat, – LarS Apr 25 at 19:11

Your Answer

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

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