Problem/Motivation

Sometimes a date range spans an entire month, from the first day to the last. "1 - 31 October 2021" is one such example. This can be shortened to "October 2021".

The same applies to ranges spanning multiple complete months, and even complete years, e.g.:

"1 January 2021 - 31 January 2021" -> "January 2021"
"1 January 2021 - 31 March 2021" -> "January - March 2021"
"1 January 2021 - 31 December 2021" -> "2021"
"1 January 2021 - 31 December 2022" -> "2021 - 2022"

Currently it's only possible to do this by omitting the day/day+month from the format patterns altogether, losing precision.

This issue proposes a way to do this only when a range spans complete months and/or years. So the same format entity that would normally show "1 - 15 October 2021" would also be able to show "October 2021" where appropriate.

Proposed resolution

There is a patch in this issue that adds this functionality.

Remaining tasks

Review the approach and patch.

User interface changes

TBC.

API changes

TBC.

Data model changes

TBC.

Comments

matsbla created an issue. See original summary.

matsbla’s picture

Title: New Date only formats for whole years » New Date only formats for multiple years

Would be great to get a feedback about this issue. I can also help provide a patch for it.

matsbla’s picture

Title: New Date only formats for multiple years » Provide opion to remove redundant date granuality
Issue summary: View changes
sergei_semipiadniy’s picture

Status: Active » Needs review
StatusFileSize
new7.29 KB

Hi,

For now there are options All month and All year in config form.

Please, test the patch.

sergei_semipiadniy’s picture

sergei_semipiadniy’s picture

Also reduced unnecessary string translations.

sergei_semipiadniy’s picture

matsbla’s picture

The patch looks great for me, however after I apply it I get some undefined indexes:

Notice: Undefined index: whole_month_pattern i Drupal\daterange_compact\Form\DateRangeFormatForm->form() (linje 150 av /srv/bindings/46aba7e3706b42c893272ab8cdfe17af/code/modules/contrib/daterange_compact/src/Form/DateRangeFormatForm.php) #0 /srv/bindings/46aba7e3706b42c893272ab8cdfe17af/code/core/includes/bootstrap.inc(584): _drupal_error_handler_real(8, 'Undefined index...', '/srv/bindings/4...', 150, Array) #1 /srv/bindings/46aba7e3706b42c893272ab8cdfe17af/code/modules/contrib/daterange_compact/src/Form/DateRangeFormatForm.php(150):

Notice: Undefined index: whole_year_pattern i Drupal\daterange_compact\Form\DateRangeFormatForm->form() (linje 160 av /srv/bindings/46aba7e3706b42c893272ab8cdfe17af/code/modules/contrib/daterange_compact/src/Form/DateRangeFormatForm.php) #0 /srv/bindings/46aba7e3706b42c893272ab8cdfe17af/code/core/includes/bootstrap.inc(584): _drupal_error_handler_real(8, 'Undefined index...', '/srv/bindings/4...', 160, Array) #1 /srv/bindings/46aba7e3706b42c893272ab8cdfe17af/code/modules/contrib/daterange_compact/src/Form/DateRangeFormatForm.php(160)

sergei_semipiadniy’s picture

@matsbla Have you run drush updb command to perform DB update?

ajay_reddy’s picture

Error while applying the patch:

Checking patch config/install/daterange_compact.date_range_format.medium.yml...
Checking patch config/schema/daterange_compact.schema.yml...
Checking patch daterange_compact.install...
error: daterange_compact.install: No such file or directory
Checking patch src/DateRangeFormatter.php...
Checking patch src/Form/DateRangeFormatForm.php...

Followed below steps:

  1. git clone --branch 8.x-1.x https://git.drupal.org/project/daterange_compact.git
  2. cd daterange_compact
  3. wget https://www.drupal.org/files/issues/2018-09-15/daterange_compact-provide...
  4. git apply -v daterange_compact-provide_opion_to_remove_redundant_date-2919426-6-D8.patch
sergei_semipiadniy’s picture

Hi,

@ajay_reddy Please try alternative command to apply patch:
patch -p1 < daterange_compact-provide_opion_to_remove_redundant_date-2919426-6-D8.patch

Because patch contains file (daterange_compact.install) that is not presented in git index.

Regards!

sergei_semipiadniy’s picture

Add checkbox to Enable/Disable short month(s)/year(s) patterns.

matsbla’s picture

#12 looks good to me!

sergei_semipiadniy’s picture

Fix error: daterange_compact.install: No such file or directory during patch applying.

sergei_semipiadniy’s picture

sergei_semipiadniy’s picture

Mats, I added small improvements and #15 is the best for now.

matsbla’s picture

StatusFileSize
new7.05 KB

Updated patch

matsbla’s picture

Version: 8.x-1.x-dev » 2.0.0
matsbla’s picture

StatusFileSize
new8.69 KB
matsbla’s picture

StatusFileSize
new8.01 KB
erik.erskine’s picture

Status: Needs review » Postponed (maintainer needs more info)

Thank you @sergei_semipiadniy and @matsbla for your contributions and helping move this forward.

I'm trying to clarify the use case and see what is and isn't available already.

I tried to recreate the first example described in the issue summary:

  • create a date range field with start date "2021-01-01" and end date "2021-12-31".
  • create a format with a basic pattern of Y and no special same day/month/year handling.

The output is "2021", which is what I expected.

Then I tried to recreate the second example:

  • create a date range field with start date "2021-12-01" and end date "2021-12-31".
  • create a format with:
    • a basic pattern of F Y
    • a same year start pattern of F
    • a same year end pattern of F Y
    • no same month start pattern
    • a same month end pattern of F Y

The output is "December 2021", which is also what I expected.

That mimics the test coverage that exists for those two use cases.

Have I understood the issue summary correctly? Or is there something else I've missed that the patch provides? In which case, let's clarify that so we can add a test for it.

matsbla’s picture

Issue summary: View changes

That is correct!

In patch from #15 there is a config option enable_whole_year_month but I think it is redundant, I think if you leave pattern empty it should just fallback to default option.

a basic pattern of F Y

I think the year pattern is appended to the month pattern, so you just need to write F.
+ $extra_description = $this->t('The year pattern will be appended to the month pattern.');
However I think it makes more sense to just write out the fulle pattern 'F Y'

matsbla’s picture

Issue summary: View changes
u_tiwari’s picture

StatusFileSize
new8.45 KB

Updated patch to make values persistent

matsbla’s picture

Status: Postponed (maintainer needs more info) » Needs review
StatusFileSize
new12.87 KB

@u_tiwari
Thank you!

I worked a little more on this patch, I made separate sections for whole month and whole year pattern. They now also have start pattern and end pattern just like the other formats. However for whole month I think we need two different start pattern depending on if it is the same year or different years, e.g:
01.01.2000-31.2.2000 = Jan - Feb 2000
01.10.2000-31.01.2001 = Oct 2000 - Jan 2001

matsbla’s picture

StatusFileSize
new12.87 KB

Just a small typo with the whole_year_separator

matsbla’s picture

StatusFileSize
new12.86 KB
erik.erskine’s picture

Status: Needs review » Needs work
Issue tags: +Needs tests

01.01.2000-31.2.2000 = Jan - Feb 2000
01.10.2000-31.01.2001 = Oct 2000 - Jan 2001

You can already do that much without the patch, using the patterns in #21. So I've misunderstood something.

Let's clarify the requirements a bit more and get some concrete examples. Then we can turn those into tests.

A quick look through the patch suggests you want to use a different pattern when the range spans a whole month (or a whole year). In other words, one format that can do the following:

  • for a range from 1 Jan 2021 to 15 Jan 2021, display "1 - 15 January 2021"
  • for a range from 1 Jan 2021 to 31 Jan 2021, display only "January 2021"

This would consider "1 - 31" redundant (in the context of January 2021) and remove it, but leave in "1 - 15".

Have I understood this correctly?

matsbla’s picture

Yes!

Any date that has start date first day of the month and end date last day of the month, should use "whole month(s)" pattern.
Any date that has start date first day of the year an d end date last day of the year, should use "whole year(s)" pattern.

"1 January 2021 - 31 January 2021" -> "January 2021"
"1 January 2021 - 31 March 2021" -> "January - March 2021"
"1 January 2021 - 31 December 2021" -> "2021"
"1 January 2021 - 31 December 2022" -> "2021 - 2022"

matsbla’s picture

StatusFileSize
new1.4 KB
matsbla’s picture

StatusFileSize
new14.27 KB
erik.erskine’s picture

Title: Provide opion to remove redundant date granuality » Remove redundant granularity when displaying whole months and/or years
Issue summary: View changes

Got it! I've updated the issue summary with this clarification.

Thanks for adding the tests too, that's a big help.