Don’t forget to subscribe to our YouTube channel to stay up-to-date.
Infinite scrolling is a technique used to show more content as the user scrolls down a page eliminating the need for the user to click to go to the next page. This is commonly implemented in popular social media apps.
This tutorial will demonstrate how to use the Views Infinite Scroll module to achieve this and also show options that can be used to customize the user interaction with the infinite scrolling behavior.
We will show you how to:
- Install the module
- Set up a simple view
- Add the infinite scroll behavior
- Customize the infinite scrolling behavior.
Drupal Views Series
1. Getting Started with Views in Drupal
2. Create Infinite Scroll pages using Views Infinite Scroll in Drupal
3. Hide Block if no Results are Returned using Views in Drupal
4. Bulk Update Content using Views Bulk Operations in Drupal
5. Add Custom Tab to User Profile Page with Views in Drupal
6. Conditionally Display View Fields using Views Conditional in Drupal
We can easily install Views Infinite Scroll using Composer:
composer require drupal/views_infinite_scroll
To enable you can use Drush:
drush en views_infinite_scroll -y
Step 1: Set up your View
For the purposes of this tutorial, we are assuming you already have a content type set up and have already generated enough content for it.
We are going to create a simple View that lists Articles (Title and Body fields).
Go to Structure -> Views -> Add view
Here is a screenshot of our simple View. There is nothing fancy. We are only listing published Articles and have created a Page display for it.
If you’re new to Views then check out our “Getting Started with Views” tutorial.
Step 2: Adding the Pager
In the Pager settings of your View, change the type to “Infinite Scroll” (leaving all the settings as default for now).
Also, in the Advanced section of your View, set “Use AJAX” to “yes”.
Here is how our View settings looks now:
Save your View and go to the URL of the View page to test out your Infinite Scroll. You should now see a “Load More” button in the pager section of your View page that looks like this:
And that’s it! You now have infinite scrolling for your View. However, there are more options that can be configured for your Infinite Scroll. These are all optional as we shall demonstrate them now.
Step 3 (Optional): Replace the button text
You can add tokens to your Infinite Scroll button to enhance the user’s experience.
In the pager section of your Views configuration page, open the settings of your Infinite Scroll pager as shown in the screenshot below:
Set the button text to “Load @next_page_count more (Total @total)” as shown in the screenshot below:
Now your Views Infinite Scroll Pager button text will look like this:
@next_page_count” and “
@total” are called “tokens” in Drupal. The tokens are replaced with actual data automatically.
View Infinite Scroll supports the following tokens:
Step 4 (Optional): Automatically Load Content
Currently, users would have to click on the “Load More” button to get the next set of content. The Views Infinite Scroll page has an option to automatically load content (without the need to click on the button).
Go to the settings of your Views Infinite Scroll pager and check the box “Automatically Load Content” and click on Apply.
Save your View and then test it. As you scroll to the bottom of your View, another set of content should automatically be loaded.
Step 5 (Optional): Exposing Options to the User
Views Infinite Scroll allows you to expose settings to allow the user to control selected displays options. We will demonstrate by doing an example. In the Settings of your Views Infinite Scroll Pager, scroll to the “Expose Options” and check “Allow user to control the number of items displayed in this view”, then click on Apply and save your View.
This results in the following being added to the top of your View page:
This specific option will allow the user to select how many results are returned everytime they click on the “Load More” button.
Views Infinite Scroll is a neat module that allows you to add infinite scrolling behavior to your View. We have shown how you can use this module to achieve this infinite scrolling behavior and also demonstrated a few of its customizations along with one of it’s biggest pitfalls.