Custom Sorting in Drupal Tutorial
In a recent Drupal training, I got a question about a replacement for the Drupal 7 Nodequeue module for Drupal 8 and other future versions. What this module allowed you to do was sort your content in whichever order you preferred.
In Drupal, we make lists of content using Views and out of the box, and we have the ability to sort this content in different ways, such as:
- Date created
- Date updated
- Alphabetically
But what if I want a list of content sorted in whichever order that I want? This is what Nodequeue allowed me to do: let me sort my content arbitrarily.
Unfortunately, the Nodequeue module no longer exists for Drupal 8. But good news: there's a replacement called Entityqueue. This module does everything that Nodequeue lets us do in Drupal 7, and more.
But content creation in Drupal has changed since Drupal 7. Many people are now using the Paragraphs module to lay out their content, so in this video tutorial, I'll be showing you two different methods of custom sorting:
- Entityqueue in Views
- Paragraphs with Content Reference
Entityqueue in Views
After installing the module, we have a new option in Admin > Structure called Entityqueues. By default, there are no queues, so we create one:
- Name: Featured Content
- Queue Type: Simple queue
- Type of Items to Queue: Content
- Content Type: Article (or whichever content you'll be sorting)
After saving our queue, we have two different ways of adding content to the queue. We click 'Edit items' and type in the title of the content in the autocomplete field, click 'Add item', and click 'Save.'
Another way to add items to the queue is to go to the content and click 'Edit'. Now, a new tab appears at the top of the page that says 'Entityqueue.' This will show a list of the queues this could be added to (it could be more than one). Click 'Add to queue' to add it.
After adding it, we might want to rearrange the order of the items inside of that queue. Click the 'Edit subqueue items' and drag them around.
For this tutorial, I've assumed you've already created a View of your content that you'd like to sort. Now, how do we get what we see in our View to match our queue?
Add Relationship to Your View
The magic of Entityqueues comes from the relationship that we add in Views: whatever happens with our queue can be matched in our View. To add the relationship do the following:
- Open the 'Advanced' section of your View
- Click 'Add' on 'Relationships'
- Check the box next to 'Content queue' (found in the Entityqueue category)
- Click 'Add and configure relationships'
- Check the radio button for the queue that you made (if you've made more than one, they will all show up here)
- Check 'Require this relationship'
After doing this, the View will now only show content that has been added to the queue. Great!
However, the order of our content might be incorrect. How do we get the sorting to match what's in our queue?
Add Sorting Criteria
If you've made no changes to the sorting criteria on your View, your content is likely being sorted by 'Content: Authored on (desc).' Basically, it's being sorted by the date the content was created. We need to remove this sorting criteria:
- Click on 'Content: Authored on (desc)'
- Click 'Remove'
Now, we need to add a new sort criteria that matches what is in our queue:
- In 'Sort Criteria' click 'Add'
- Check the box next to 'Content Queue Position' (found in the Entityqueue category)
- Click 'Add and configure sort criteria'
- Click 'Apply' (you can leave the default settings)
And now, our View content and order matches what is in our Entityqueue!
Paragraph Type with Content Reference
Although Drupal 7 did have support for the Paragraphs module, this method of laying out content has really gained popularity since Drupal 8 was released in 2016. To keep this tutorial brief, I won't go into all the details of creating new Paragraph types, but here are the basics.
After downloading and installing the module, we have a new section in Admin > Structure called Paragraph types:
- Click 'Add paragraph type'
- Label: 'Featured Content'
- Description: 'Content that can be added and sorted arbitrarily'
- Click 'Save'
- Click 'Add field'
Add a new 'Content' (reference) field:
- Label: 'Featured Content'
- Allowed Number of Values: unlimited
- Restrict to Content: 'Article'
- Click 'Save'
On your Paragraphs-enabled content type, you now have the option to add a 'Featured Content' paragraph. This will display an auto-complete field (similar to Entityqueue) where you can type the Title of the content you want to feature. You can add multiple items, and rearrange them how you'd like.
By default, what's shown is a simple link to the content, but this can be modified at the theme layer to display however you'd like, such as a slideshow of the featured content (but that's beyond the scope of this tutorial).
Comparing the Two Methods
From an Editor's perspective, it seems like there's less steps involved going with the Paragraphs approach, so why use that over Entityqueue? There's advantages to both methods and you have to choose which is right for your scenario.
If you have a Paragraphs-enabled content type, going down the Paragraphs route may be the simplest option. Editors just add the Paragraph to the page and begin typing and re-ordering the content. However, this features content in only one location on the site.
With Entityqueue, though there's a few more steps from the Editor's perspective (and you may want to create a menu item so they can get to the queues quicker), if you need content to be sorted and featured in multiple places on your site, this is the route to go. In the video, I created a Views Page which has a relationship with my queue, but I quickly created a Block, which inherits all the settings I had from my page) and I can now place this in a region and have the two show the same content in the same order.
Wrapping Up
Nodequeue was a great Drupal 7 module, and luckily its spirit lives on (okay, that sentence might be a bit puffed up). But Entityqueue offers a great alternative and even includes some features that Nodequeue didn't, like being able to sort other entities like users and taxonomy terms.
And content entry in Drupal 8 has evolved with the Paragraphs module, which also offers us a nice, easy-to-use method of sorting content in whichever order we'd like. To get more in-depth web development training, check out our training page or sign-up for a tutorial.