Automatically Hide Content in Views Based on Date

By adam
Thu, 2015-05-21 08:02
1 comments

Share with Others

Normally, I would do this video style, but I'm a wildcard people and today we write! Thanks to one of our Code Karate supporters, Pieter, I am going to walk you through how to use Drupal views, date and content types to automatically hide/show nodes based on date field. Let's get started.

Make sure that you have Drupal Views installed. If you're new to Drupal, Views is like oxygen to humans, you REALLY need it. Also, you can learn more about Views by watching this Drupal views video. Also, I installed the Drupal Date module. I will use this module to set the date for the node (explained more below). Besides Views and Date, you shouldn't need any other modules besides what already comes with Drupal core.

For my example, I am going to display a list of training times people could sign up for and then once the training session has passed the view will no longer show that training. Make sense? Let's build!

A quick note before we get ahead of ourselves. When you enable the Date module, make sure that you enable Date, Date API, Date Popup, and Date Views. These additional modules will make your life a little easier. You're welcome.

I think we're ready. To start, I am going to create a training content type. For the fields, I am just going to keep the defaults Title and Body and add additional field training date. Naturally, you can add other fields that fit your unique situation, but for the purpose of this example we won't need any other fields.

With that content type, I am going to create five different nodes. Each of these nodes will have a different date. For the sake of showing how to hide content based on date, I am going to make one of the nodes have a date in past. This node will be used to prove that my view is only showing training dates in the future.

With the nodes created, we now need to create a view. Before we start excluding dates from showing in the view, let's make sure we get a view that displays all the content we want. To do this, make sure you add all the fields you want for the training content type. For me, that means I have added the Title, Body, and Training Date fields. Also, under the Filter Criteria section I have limited it to only showing training content types. Doing this will show that I can display all the training content.

Next, we need to add a filter that will only show the training dates that are in the future and exclude those that have already happened. Remember, in my example I have added a date in past for my 1st training node. If the view is built correctly it shouldn't exclude the 1st training node, but still show the remaining four training nodes.

The first step to do this is to add a filter to your view. For the field, you will want to select the Training Date field (or whatever field you are using for the date). For the settings, keep everything default except for the granularity. I change this to be minute. The reason for this is because I want the training to be available to be seen right up until they start. If I kept the granularity at a day, it would be hidden at the beginning of the day.

The filter criteria are where we do the filtering for the view. In other words, this is what is telling the view to show or not show. Again, the goal is to hide old training once the date and time have passed. To do this, select the Is greater than operator and change the drop down to Enter a relative date. Within the relative date text field, enter "now". Why now? Now is a PHP date format that specifies the current date and time. So "now" is an always changing value, which is exactly what we want. There are tons of other PHP date strings that you can use if "now" isn't exactly what you are looking for. To learn more about those formats just go here.

That is it! Once you save your filter go ahead and take a look at the view. Again, if done correctly, we shouldn't see the 1st training node as that contains a date field less than "now".

Hopefully, you were able to follow along and get your view and content displaying correctly. Remember, you can use other PHP date formats to show/hide content based on your unique situation. There are an almost unlimited amount of possibilities.

This was a very simple way to filter content. Do you use a different way? If so, share it in the comments below.

Until next time, Happy Coding!

Comments

High school, school, and graduate students are swinging to expert writing services to complete their essay writing exercises. Admission essay writing service is ending up being more predominant with students. It may flabbergast you to find that very nearly half of all students in the US case to have used online essay writers help at any rate once in the midst of their insightful work. Organization essay writers commonly have various years of association in your field of study. Most associations have writers that create on particular subjects and branches of learning.
An essay writer ordinarily has various years of experience writing essays so you can be overall ensured that any essay you purchase will get a not too bad assessment in an academic setting. If you slant toward doing your own work these associations generally give adjusting and fixing services that you can use to upgrade or ensure the general way of your essay. It is straightforward for students to wind up overwhelmed and to fight especially in the later years of school when there are battling requirements for your time and thought. Conceivably your assessments have been slipping and you are on academic probation or possibly you have to work and manage the kids while you are in like manner still in school. Essay writing associations can be a honest to goodness get you through to the accompanying term, semester or academic year.
Fundamental points of interest of using admission essay writing service is recorded as extra your time, you get specialists' proposal, sensibility and they serve as arrangements for future assignments. Working with a site that does not ensure strikingly made essays is a wagered; you could end up expelled from school or stuck in a sad circumstance in case you turn in an essay that your teacher can find when checking for scholarly robbery. In case you don't utilize the best writing site, you won't get the best things.

Post new comment