Allow "current year"

Created on 6 February 2023, almost 2 years ago
Updated 4 May 2024, 7 months ago

Problem/Motivation

When setting a default year this must me an explicit year. You can't set it as the current year.

Steps to reproduce

Enter blank year in the year filter.

Proposed resolution

Either add a "current year" to the filter or allow some sort of php or mysql date format in the year field.

I've added a patch which will allow you to enter year(curdate()) in the year field.

Remaining tasks

User interface changes

A new radio button such as: The current year.
Or, help text saying to enter "now()" or something else to that effect.
Hacking the validation in ViewsYearFilterDatetime::opSimple and allowing "year(curdate())" currently works.
I will attach a patch to that effect.

API changes

None

Data model changes

None.

✨ Feature request
Status

Needs work

Version

2.1

Component

Code

Created by

πŸ‡¬πŸ‡§United Kingdom oliverpolden

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @oliverpolden
  • πŸ‡¬πŸ‡§United Kingdom oliverpolden

    This is a patch to simply allow year(curdate()) in the year field. This would need a bit more work to at a minimum add some help text to that effect but at least this works.

  • Status changed to Needs work 9 months ago
  • πŸ‡§πŸ‡·Brazil igorgoncalves

    @oliverpolden i got the same issue as you but end up working without no patch.
    you can just use the regular option "A date in any machine readable format. CCYY-MM-DD HH:MM:SS is preferred."

    and set the field value as: Y

  • πŸ‡ͺπŸ‡ΈSpain ady1503

    @oliverpolden i got the same issue but your patch not working VersiΓ³n de Drupal 10.2.6.

    It would be correct for this module to have the option to activate the current year automatically if none is indicated or it is left empty.

    Thank you.

  • πŸ‡ͺπŸ‡ΈSpain ady1503

    @oliverpolden here is working example for year filter node

    function yourmodule_views_query_alter(ViewExecutable $view, QueryPluginBase $query) {
      // make sure we're altering the correct view
      if ($view->storage->get('id') === 'your_view' && $view->current_display === 'your_display') {
        $currentYear = date('Y');
        $query->addWhereExpression(0, "EXTRACT(YEAR FROM FROM_UNIXTIME(node_field_data.created)) = :year", [':year' => $currentYear]);
        }
    }

    node_field_data.created is for created year

Production build 0.71.5 2024