Hand-picked content at the top of a chronologically sorted view: Option 1, make it Sticky!

<p><span class="drop-cap">E</span>very time that I have to implement a <strong><em>list of articles, sort them by created date and also allow our content editors to override that order by selecting which articles go at the top of the list</em></strong> I need to sit back and think for a minute about the best implementation for that particular case. I realized today, discussing this with my coworker and friend Uriel, that <em>I’ve done this differently many times</em>. That doesn’t mean <em>all but one</em> implementations were wrong. Some implementations had advantages over the others for each special case. </p> <p>Without further ado, we came up with this <a href="https://www.youtube.com/watch?v=1VFuHj9_Tgw"><em>one way or another</em></a> series (I hope the song gets stuck into your heads as well), to approach common issues and solve them in different ways, weighing their pros and cons, and showing quick tutorials on each approach. </p> <p>Today is the turn of the checkbox<strong><em> Sticky</em></strong>! </p> <ul></ul><p>"Sticky at top of lists" is a checkbox on the promotion options of your content edit page. It's easy to set up and It is a good option, when for example a content manager wants to display one article (or any content type of their choice) in a special page and have the option to override it and showcase one hand-picked node at times.</p> <p>Here's how it's done: I’ll start by creating a view that displays <strong>one</strong> article:</p> <p><img alt="Create a view of type Page, that displays only one Article tagged with the term Animals." data-entity-type="file" data-entity-uuid="a578aa67-f949-47e7-a4d7-4bce94a66302" src="/sites/default/files/inline-images/01%20-%20create%20view.png" /></p> <p>You'll notice that I also added a filter by term "animals". This is because I created simple articles to test the view with that term. Nothing special.</p> <p>Back to the view: we want to prioritize the contents marked as <em>sticky</em>, so we'll add a <strong>"Sticky at top of lists" sort criteria</strong> to our view, and set it to <strong>descending</strong> order:</p> <p><img alt="Add a sort criteria on the view edit page." data-entity-type="file" data-entity-uuid="0bb22b19-6d80-4398-80d9-2da62d15ed1e" src="/sites/default/files/inline-images/04%20-%20add%20sort%20criteria.png" /></p> <p><img alt="Select &quot;Sticky at top of lists&quot;" data-entity-type="file" data-entity-uuid="df164340-ba49-4793-91df-c9e623fbd6eb" src="/sites/default/files/inline-images/05%20-%20add%20sort%20criteria.png" /></p> <p><img alt="Set the order to &quot;descending&quot;" data-entity-type="file" data-entity-uuid="379adfcc-5bda-4b4a-9685-82aa7273b67c" src="/sites/default/files/inline-images/06%20-%20add%20sort%20criteria.png" /></p> <p>After that, we need to rearrange the sort criteria. Do not forget this step! It's crucial to this feature, that the Sticky sort criteria gets applied <em>before</em> the rest of the criteria. This is done by clicking on the little arrow next to “Add” and clicking on “Rearrange”.</p> <p>What this does, is that the view will first sort by Sticky (has sticky first, doesn't have sticky next), and if there is a tie (more than one article has the Sticky checkbox on), it will sort those tied articles by "Authored on" date.</p> <p><img alt="Rearrange the sort options, so that the sticky sort option gets applied first." data-entity-type="file" data-entity-uuid="fff6b0f9-89d8-456e-883b-d4d7e7b06022" src="/sites/default/files/inline-images/08%20-%20sort%20criteria.png" /></p> <p>Now the view is finished. To test this, I have two Articles with pictures of animals in them (and tagged them with the Animals term). The first (and therefore oldest) article has a bunny, and the newest has an elephant. None of the articles is marked <em>sticky</em> yet.</p> <p>The first result of the page is the elephant, since it's the newest content.</p> <p><img alt="The view displays an article with an Elefant." data-entity-type="file" data-entity-uuid="a8a51945-c823-4603-ac83-7c97cc7e21fd" src="/sites/default/files/inline-images/02%20-%20favorite%20page%20.png" /></p> <p>Then I edited the rabbit article and marked it as sticky on the promotion options. Notice that, to be able to access the promotion options, an admin has to allow it on the content type first at <strong>Structure</strong> &gt; <strong>Content types</strong> &gt; <strong>Article</strong> (or your content type) &gt; <strong>Edit</strong>.</p> <p><img alt="The article with the bunny is now marked as sticky." data-entity-type="file" data-entity-uuid="d7521215-6cbf-48ab-9914-ae7b5b71be72" src="/sites/default/files/inline-images/03%20-%20sticky%20rabbit%202%20.png" /></p> <p>The view gets updated, and displays the rabbit instead of the elephant, because the rabbit is marked sticky!</p> <p><img alt="The view now displays the rabbit article." data-entity-type="file" data-entity-uuid="0d2b7821-7178-48b2-8b2e-56bbced77584" src="/sites/default/files/inline-images/09%20-%20ta%20da.png" /></p> <p> </p> <p>Summing up, sticky is a great feature, because:</p> <ul><li>it <strong>comes along with the Drupal core</strong> (there’s no need to install anything new). It’s a nice checkbox in the sidebar of your content edition page. That’s it.</li> <li>It’s also very <strong>easy to integrate with views</strong> (I’ll show you later).</li> </ul><p>But nothing is perfect, and this also has some cons:</p> <ul><li><strong>Using a checkbox doesn’t provide any way for the user to specify the order of the elements</strong>, and also,</li> <li>once you mark many contents as sticky, It gets harder to remember or even figure out which ones you marked: <strong>it's messy</strong>. In the worst case scenario, your content editor might end up having to edit them all again in the quest of <em>“the lost content with the sticky checkbox”</em>. One way to help your content editors out is to create an administration view where you can show them which contents are sticky at one glance. They will appreciate it very much.</li> </ul><p>Hope this is useful to you all. Next, we'll implement this feature with entity queues! Stay tuned!</p>

Related blog posts

The 2021 virtual DrupalCon sessions I enjoyed the most

Once again this year, Drupalcon went virtual. If you didn't have a chance to go and care for a suggestion, I list here a few of the sessions I enjoyed the most.

Ajax view scroll to top

Sometimes as developers we encounter situations that seem pretty straightforward, but as we try to implement them, we find weird nonsensical bugs that completely ruin the experience for our users.

Drupal + GraphQL Cheatsheet

Here's a cheat sheet to show you a couple of examples of GraphQL in Drupal 8.