0

I'm tasked with creating a Year filter for our press releases page. We're using the Article content type, to which we've added a field called Release Date.

We have the press releases as a block display of a view of articles. Using this question, I was able to add a contextual filter that would accept a four-digit year.

Now I need to make the UI widget for the user to specify the year to filter on. I want it to be populated by the Release Dates of the article nodes, so that we automatically have all the years appearing in the filter, and don't have to manually add a new one when a new year rolls around.

However I'm having difficulty finding how to get the distinct values of the year part of the Release Date date field. I've created a display of just the release date field, and set the "Date format" to HTML Year (2019). I added aggregation, and for the Aggregation settings of the field, the "Aggregation type" is Group results together, and the "Group Column" is Value. I've also set checked Distinct in query settings.

However, when I look at the preview, I see a number of repetitions of the year. When I add in the full Release Date field, I see that I am getting one year per distinct date in that year. It seems that the query is pulling each unique date (which is all Article nodes-- none of them share a date), and then doing the formatting (to show only the year) after the select has been performed.

I looked at the Query preview, and it seems that this is indeed what is happening:

GROUP BY node_field_data.nid, node__field_release_date_field_release_date_value, node_field_data_created

I don't understand why it's grouping first by nid, and then also the created date at the end.

FWIW here is the entire query:

SELECT DISTINCT node__field_release_date.field_release_date_value AS node__field_release_date_field_release_date_value, node_field_data.created AS node_field_data_created, node_field_data.nid AS nid, MIN(node_field_data.nid) AS nid_1
FROM
{node_field_data} node_field_data
LEFT JOIN {node__field_release_date} node__field_release_date ON node_field_data.nid = node__field_release_date.entity_id AND node__field_release_date.deleted = '0'
WHERE (node_field_data.status = '1') AND (node_field_data.type IN ('article'))
GROUP BY node_field_data.nid, node__field_release_date_field_release_date_value, node_field_data_created
ORDER BY node__field_release_date_field_release_date_value DESC, node_field_data_created ASC

Is it possible to get this type of result set with Drupal 8 views? If so, what settings do I need? Is there a module or extension I need to employ?

Edit User prkos suggested using an exposed filter, perhaps with the better_exposed_filter module. I've since gone down this alley, and it doesn't seem to meet the requirements. The basic problem is that, since views only looks at the entire date value, I can't specify a year to filter on at all (the interface and options for exposed filters is completely different from contextual filters). It also seems to run in to the problem that it can't auto-populate filter widgets with unique years, which is the issue I'm asking about in this question.

When I add an exposed filter, a "Single filter" type only allows entire dates, or relative dates:

enter image description here

When I hard-coded a value (2015) to test, as in the screenshot above, the view returned no results. This is because the filter is turning the year into a particular date in the query:

AND ((DATE_FORMAT(node__field_release_date.field_release_date_value, '%Y-%m-%d') = DATE_FORMAT('2019-05-07T20:15:00', '%Y-%m-%d')))

When I look at Group Filters, there seem to be two issues: 1. groupings must be manually specified, which is the sort of maintenance I'm looking to avoid, and 2. it still only accepts an entire date, and can't handle a year specification.

enter image description here

I also installed better_exposed_filters, but it doesn't seem to provide any of the features I'm looking for.


Edit 2 Following the instructions in the first half of prkos' answer, I was able to get a list of unique year values:

enter image description here

But, I don't see how to use it as a filtering widget for end-users. The results are links, but they each only link to the /press-releases URL. Because I'm getting the contextual filter value from a query parameter, I need the links to look like /press-release?year=2014. But, because they are summary results from the contextual filter, and not fields, I can't re-write them.

So I guess this technically answers my question of how to get a view of unique years, but it doesn't give me something that I can work into a usable UI widget. I'm going to accept this answer, and ask about how to create the useable widget in another question.


Edit 3

As per prkos' suggestion, I looked at the summary results on the page, instead of just the view preview (I was pretty sure I had done this earlier, but of course you should always double-check your work).

I found that the links were not automatically populated with the value of the filter.

Here my mouse is hovering over the '2014' in FF. The screenshot doesn't capture the mouse cursor icon, but you can see which link is being hovered over, because its styled as a darker magenta color than the others:

enter image description here

Another test in Chrome, hovering over '2017':

enter image description here

So in the tests above, I was testing the summary view in the header of the press releases block. To make sure this wasn't a problem with a view-within-view, or anything, I copied the summary block display as a page. The page showed the same behavior:

enter image description here

Each of the above views have the same Base Path value, /press-releases. When I tried removing the Base Path value, then there were no results, summarized or other, as prkos warned.

I don't know why the summary links on the page display were going to /year-filter-test-- that's the path of that page display.

  • If you were able to get the Contextual filter work there is no reason why the regular Filter wouldn't work. Why not just add the regular Filter and expose it? You can use Better exposed filter module to change the widget for the exposed filter. Or even a better option would be to use the Summary option on your Contextual filter When the filter value is NOT available. This will give you a list of links of all the years for your users to click through (don't forget to enter your path into those options). – prkos May 6 at 21:56
  • @prkos I've tested an exposed filter and also better_exposed_filters. They seem to have the same issues: that entire dates must be specified, year values are expanded to a particular date; and groupings must be manually specified. – user1359 May 7 at 13:41
0

I may be misunderstanding what you're trying to achieve but looking at your screenshots I don't see why you wouldn't be able to achieve what you're after.

Use the Summary option on the Contextual Filter

You mentioned you got the Contextual filter to take the year as an argument (in another question). All you have to do is use the Summary option of that filter to do it all for you.

It will "see" what years are present in your results for that particular field, list those years as links, you can even opt to show how many results there are under each year, and when you click through one of those links you'll see the results filtered for that year alone.

Here is what adding the Contextual may look like:

add Contextual Date Year

Here's how you configure it:

Contextual Date Year Summary configuration

You must include the Base path if the Page display you're using isn't the first one in your Views, otherwise it won't work.

You can also set the Specify validation criteria further below to Date format, and some other options depending on your needs.

Here is what the results may look like:

years Date Summary

How to group Date ranges Filter

This isn't what you're after because it requires adding each year manually, but since you posted the screenshot I'll post what the correct configuration for that should look like:

grouped Exposed Date range filter

You have to use Is between Operator, not the Is equal to one. The date you enter is in the CCYY-MM-DD HH:MM:SS format, so when you enter 2017 you're actually telling to show only nodes with that field set to 2017-01-01 00:00:00.

You can omit the time if you set the correct dates for the In between, as I did, because of the assumed values.

Here is what the Exposed Grouped Date filter can look like:

Exposed Grouped Date Filter

The Better Exposed Filters was suggested to have more control over some Exposed filters, to show radio buttons/checkboxes instead of the Autocomplete field in some cases, since you wanted to be presented with options, instead of users typing them out on their own. You don't need it though when Contextual Summary is doing it for you.

  • Hey prkos, I don't want to manually update the view every January 1st: "...so that we automatically have all the years appearing in the filter, and don't have to manually add a new one when a new year rolls around." If I define the filters up through 2019, what happens when January 1st, 2020 rolls around? Will I get a bug report about the Press Releases page being broken (i.e. missing the option of '2020' as an option for the year filter)? – user1359 May 7 at 19:33
  • You don't have to do anything if you're using the Summary Option of the Contextual filter. Why do you think it's not automatic? I thought I explained it: "It will "see" what years are present in your results for that particular field, list those years as links" but maybe it wasn't clear enough. The Contextual filter will automatically list all the years present in the results. I also explained why I added the Exposed filter explanation, maybe you missed that? – prkos May 7 at 20:24
  • Hey prkos, sorry for the delay. I finally got some time to look at your answer again. I was able to create a summary that returned unique years, but since they only link to the same page, I don't see how I can use them in a user-facing widget. I've updated my answer to include the results of this answer. – user1359 May 14 at 20:29
  • Also, are you aware that the second half of this answer is devoted to a solution that requires hard-coding and manual updates, which I covered initially in my answer? It was confusing to me as to what exactly you were suggesting, without really digging into ti. – user1359 May 14 at 20:30
  • Summary links do provide arguments in each link! Maybe you don't see them in the Views Preview, but visit your actual Page and you'll see they all have the appropriate year /press-releases/2014 ;) – prkos May 14 at 21:45
0

I am going to go ahead and say No, this feature is not available in Drupal 8 at at the time of this writing.

Presently Drupal 8 views is not able to return unique year values of a date field. This is because the view selects the entire date of the date field, and then can only operate on those.

Also Exposed filters can only filter on a specific day date; it can't use an entire month or year (even though contextual filters has this ability, exposed filters does not seem to).

If this answer becomes outdated or is incorrect, please add the answer.

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.