Steps to reproduce

  1. create a new view called "test"
  2. create a new page display
  3. add a "contextual filter" of type "Created month"
  4. save view
  5. go to /test/all

you will get a 500 error

Attached is a patch that checks if a valid date is returned from strtotime() before trying to format the date and return a empty string if an strtotime cannot parse the date.

Comments

L-four created an issue. See original summary.

L-four’s picture

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

matiasmiranda’s picture

Status: Active » Needs review
StatusFileSize
new66 KB
new1.38 KB

I think that in case of an invalid value, the default value should be taken from the parent class, instead of forcing an empty string, for example when a default value for "All" is provided:
argument default value

I attach a patch

daffie’s picture

Issue summary: View changes
Status: Needs review » Needs work
Issue tags: +Needs tests

The basic solution look good. For both code changes I would like to improve the code a little:

+++ b/core/modules/views/src/Plugin/views/argument/MonthDate.php
@@ -24,7 +24,13 @@ class MonthDate extends Date {
+    if($timestamp){
+      $summary_name = format_date($timestamp, 'custom', $this->format, 'UTC');
+    } else {
+      $summary_name = parent::summaryName($data);
+    }
+    return $summary_name;

Can we change this to:

    if($timestamp){
      return format_date($timestamp, 'custom', $this->format, 'UTC');
    }
    return parent::summaryName($data);

Now we need automated tests so that this fix will not be undone in the future.

Version: 8.6.x-dev » 8.8.x-dev

Drupal 8.6.x will not receive any further development aside from security fixes. Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

kunalgautam’s picture

Assigned: Unassigned » kunalgautam
kunalgautam’s picture

@daffie I updated the patch

kunalgautam’s picture

Assigned: kunalgautam » Unassigned
Status: Needs work » Needs review
daffie’s picture

Status: Needs review » Needs work

@kkalashnikov: Thank you. Could you also create the necessary tests?

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

raman.b’s picture

Version: 8.9.x-dev » 9.2.x-dev
Status: Needs work » Needs review
Issue tags: -Needs tests
StatusFileSize
new7.71 KB
new9.09 KB
new8.4 KB

Adding some test coverage for this

The last submitted patch, 12: 2969107-12-test-only.patch, failed testing. View results

lendude’s picture

Status: Needs review » Needs work

Nice test coverage!

  1. +++ b/core/modules/views/src/Plugin/views/argument/MonthDate.php
    @@ -24,7 +24,11 @@ class MonthDate extends Date {
    +    $timestamp = strtotime("2005" . $month . "15" . " 00:00:00 UTC");
    
    @@ -32,7 +36,11 @@ public function summaryName($data) {
    +    $timestamp = strtotime("2005" . $month . "15" . " 00:00:00 UTC");
    

    Wouldn't it be better to catch the thrown exception instead of making an assumption about what the date formatter service does?

    So just wrap the original code in a try/catch and return the parent call in the catch?

  2. +++ b/core/modules/views/src/Plugin/views/argument/MonthDate.php
    @@ -24,7 +24,11 @@ class MonthDate extends Date {
    +      return \Drupal::service('date.formatter')->format($timestamp, 'custom', $this->format, 'UTC');
    
    @@ -32,7 +36,11 @@ public function summaryName($data) {
    +      return \Drupal::service('date.formatter')->format($timestamp, 'custom', $this->format, 'UTC');
    

    If we are not using a try/catch at least it seems like the date formatter is injected, so lets use that and not \Drupal:: (like in the original code)

raman.b’s picture

Status: Needs work » Needs review
StatusFileSize
new9.09 KB
new1.48 KB

Thanks for the review @Lendude!

I like the idea of catching the exception and returning the parent call. Uploading a patch implementing the same.

lendude’s picture

Status: Needs review » Reviewed & tested by the community

Looks great, nice work!

catch’s picture

Version: 9.2.x-dev » 8.9.x-dev
Status: Reviewed & tested by the community » Patch (to be ported)

Committed/pushed to 9.2.x and cherry-picked to 9.1.x

This should go into 8.9.x too, but leaving at 'to be ported' for a little while since we just did the patch release yesterday evening so still thawing.

  • catch committed 37ecba1 on 9.2.x
    Issue #2969107 by raman.b, matiasmiranda, kkalashnikov, L-four, daffie,...

  • catch committed 4c1b7b8 on 9.1.x
    Issue #2969107 by raman.b, matiasmiranda, kkalashnikov, L-four, daffie,...

  • catch committed a9c69a3 on 8.9.x
    Issue #2969107 by raman.b, matiasmiranda, kkalashnikov, L-four, daffie,...
catch’s picture

Status: Patch (to be ported) » Fixed

Cherry-picked to 8.9.x

  • xjm committed 6837846 on 8.9.x
    Revert "Issue #2969107 by raman.b, matiasmiranda, kkalashnikov, L-four,...
xjm’s picture

Status: Fixed » Needs work

The cherry-pick broke 8.9.x HEAD on all environments: https://www.drupal.org/pift-ci-job/1965195

alexpott’s picture

+++ b/core/modules/views/tests/src/Functional/Plugin/MonthDatePluginTest.php
@@ -0,0 +1,101 @@
+  /**
+   * Modules to enable.
+   *
+   * @var array
+   */
+  protected static $modules = ['node'];

ON Drupal 8.9 this needs to be public.

alexpott’s picture

StatusFileSize
new554 bytes
new9.09 KB

Here's a fix.

catch’s picture

Title: 500 error on passing invalid month to MonthDate view argument handler » [backport] 500 error on passing invalid month to MonthDate view argument handler
Status: Needs work » Reviewed & tested by the community

  • xjm committed 817b13d on 8.9.x
    Issue #2969107 by raman.b, alexpott, matiasmiranda, L-four, kkalashnikov...
xjm’s picture

Title: [backport] 500 error on passing invalid month to MonthDate view argument handler » 500 error on passing invalid month to MonthDate view argument handler
Status: Reviewed & tested by the community » Fixed

That'd do it.

Committed the backport to 8.9.x. Thanks!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.