3

I have a large number of views that show member profiles. Right now, I only show 8 members per page, because on a mobile device it takes a very long time to scroll.

However, for the desktop, I could easily show 20 members per page.

Since there are a large number of views, I don't want to do this if I have to duplicate all of them and change the pager settings. Is there another way to return a different number of results per page if the user is on a mobile device?

  • I just realised I asked a very similar question a while back. If I remember rightly you're on Pantheon? I haven't found a sustainable way to do this yet with their enterprise package (to be fair to them I haven't gone down every possible avenue so it may well be possible) – Clive Oct 6 '13 at 14:40
  • Yes, good point-- I'm on Pantheon, so the solution has to work on Varnish too. – Patrick Kenny Oct 7 '13 at 2:07
3

Personally, I would use something like Views Infinite Scroll instead of messing with the pager. This would be better for most users in all cases.

However, you can use the Mobile Detect module inside a hook_views_pre_build(). Something like this (untested)

function mymodule_views_pre_build (&$view) {
  $detect = mobile_detect_get_object();
  $is_mobile = $detect->isMobile();
  $is_tablet = $detect->isTablet();

  if ($is_mobile && !$is_tablet) {
    $view->pager["items_per_page"] = 8;
  }
  else {
    $view->pager["items_per_page"] = 20;
  }
}

This would affect all views. dpm() the $view object to see how you would use it to add logic to limit to particular views and/or displays.

However, adding logic like this really complicates the cachability of pages using the normal methods. The Mobile Detect module has a submodule that adds some ctools selection and access rules. Using this, you could have view panes for device types, and then leverage Page Manager for caching.

Disclosure: I am the author of the Mobile Detect module.

| improve this answer | |
  • I had originally avoided Views Infinite Scroll because it does not work with the iPhone and there is no consistently working patch (drupal.org/node/1180962). However, I was able to get Views Load More (drupal.org/project/views_load_more) with Waypoints to provide infinite scroll instead. – Patrick Kenny Oct 8 '13 at 12:11
  • @PatrickKenny The patched version of the jQuery plugin works for me. Oddly enough, I have never gotten Views Load More + Waypoints to do infinite scrolling. – mpdonadio Oct 10 '13 at 19:20
  • The current release of Views Load More requires a patch (drupal.org/node/1889300) for infinite scrolling to work. Also, you have to turn on both "Use AJAX" (general views setting) and "Use infinite scrolling" (pager setting) in the options; this is not documented (I added a request for at least putting that info in the README, but who knows when it will be acted upon). – Patrick Kenny Oct 11 '13 at 2:53
  • Would this work with boost enabled? – Nathan Bomshteyn Nov 13 '14 at 22:55
  • 1
    @nathan6137 Doubtful. The module has some caching issues right now, similar to anything that modified behavior based on UA. – mpdonadio Nov 15 '14 at 22:14
3

You could use Browscap module along with Browscap Block which would add the visibility of blocks depending on device. Next you could add two blocks one for Normal screen and one for mobile devices.

Browscap Block

Browscap Block adds visibility options to block configuration settings to allow you hide or show blocks in mobile devices.

If you do not want to create multiple views then you could use the browscap function in your template file to detect the browser and then restrict the number of items displayed. The function would be browscap_get_browser()

This could either be done in hook_views_pre_build function or with in the Style output template file where it loops through the $rows array. you could restrict it the number to 8 depending on the device by exiting the loop once you reach the required count.

| improve this answer | |
  • 2
    Without resorting to AJAX/ESI this is about the best you can get, although basing content on the UA string can be a bit hit and miss. You also need to give careful consideration to how you manage this, so you don't end up penalised in SERPs for showing different content to different users (your intentions are obviously harmless but there's no intuitive way for Google et al to know that), and so that all your content actually gets indexed. If your site's behind a reverse proxy it'll probably require some extra configuration on that server too – Clive Oct 6 '13 at 14:33
  • I am pretty sure it is too late in the template logic to alter view paging. – mpdonadio Oct 6 '13 at 17:01

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.