2

The Date module with Views has functionality to filter dates with a granularity by Day, Months, Year, etc. but not for weeks. So to do this with a Contextual Filter and PHP code to retrieve the current week, I tried this:enter image description here

This had an output that suggested the current week was the fifth of the year which would be in February as shown here: enter image description here

Yet the output is correct without any filter as shown here: enter image description here

With other PHP code to filter the View by days and months, it worked great. Yet this week filter doesn't seem to know the current week of the year. How can I either have the Date module filter with a granularity of weeks, or input the correct PHP code to have the list filter by the current week?

  • 1
    Do not use PHP inside of views... NEVEERR... Just remember one this quote next time you are using php inside of view... "Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live" – itsdarrylnorris Jan 11 '16 at 9:52
  • Ironically, inside the view would be the easiest place for the next guy to see what's going on. – Niall Murphy Jan 11 '16 at 16:34
  • @darol100 So PHP inside a View is a security risk? Not just a quality of life thing for maintainers? – KyleRiggen Jan 12 '16 at 1:44
  • You should never have any execute code in your database, that is huge security risk (including php and js). Also, php inside of views can have a a lot performance issue. Lastly, if the next developer have a syntax error inside of views the entire website is going to crash and you need to go directly to the database and fixed. – itsdarrylnorris Jan 12 '16 at 1:56
  • Using php views on a production site it just bad practice. – itsdarrylnorris Jan 12 '16 at 1:57
2

I was able to find a solution by using two separate filters.

The first filter will be a relative for the current day. It should have an operator that is greater than or equal to today.

The second filter will be a relative day as well. It will have an operator that is less than +7 days.

This will allow you to filter views by the current day plus seven days.

1

Try this:

same steps as Views Contextual Filter by current day

except for relative date: put week

  • Ok what should the granularity be for that? They all seem to output as blank. Imgur – KyleRiggen Jan 11 '16 at 9:45
  • humm ok, try putting this week or Monday this week or Monday next week – No Sssweat Jan 11 '16 at 9:59
  • try with day as granularity. – No Sssweat Jan 11 '16 at 9:59
  • this week allowed information to be displayed but it seemed to still only filter by which granularity was selected with the relative date of this week. Here is by Month that looks like this. Then your day suggestion does the same thing: these settings into this display. – KyleRiggen Jan 11 '16 at 10:06
  • I found a similar question over at Stack Overflow Any way to filter a view by week number? – No Sssweat Jan 11 '16 at 10:33

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.