Problem/Motivation

DateTimeComputed returns null if there are errors in the datetime object.

  if ($date instanceof DrupalDateTime && !$date->hasErrors()) {
        $this->date = $date;
  ...
  return $this->date;

Proposed resolution

We should either throw an exception or log if $date->hasErrors().

Remaining tasks

User interface changes

None.

API changes

None.

Data model changes

None.

Release notes snippet

Comments

jonathanshaw created an issue. See original summary.

rahul-kr-sh’s picture

Version: 9.2.x-dev » 9.3.x-dev
StatusFileSize
new512 bytes

Adding initial patch please review this.

jonathanshaw’s picture

Thanks for patch @rahul-kr-sh.

However your solution won't work, because an exception is not thrown if the date object has errors.

Handling the exception that is currently thrown (the @todo) is the focus of #3025642: Remove the try/catch block from DateTimeComputed::getValue().

The problem we're addressing in this issue is caused by this line:
if ($date instanceof DrupalDateTime && !$date->hasErrors()) {

Currently if is is true that $date->hasErrors() then we don't do anything, so the whole function returns NULL without warning.

Our code probably needs to become something like:

if ($date instanceof DrupalDateTime && !$date->hasErrors()) {
...
}
elseif($date instanceof DrupalDateTime && $date->hasErrors()) {
throw some exception
}

{

rahul-kr-sh’s picture

StatusFileSize
new1.26 KB

Updated the patch.

jonathanshaw’s picture

Status: Active » Needs work
  1. +++ b/core/modules/datetime/src/DateTimeComputed.php
    @@ -50,7 +50,10 @@ public function getValue() {
    +      if ($date->hasErrors()) {
    

    We need to check $date is an instance of DrupalDateTime before calling hasErrors

  2. +++ b/core/modules/datetime/src/DateTimeComputed.php
    @@ -50,7 +50,10 @@ public function getValue() {
    +        throw new \UnexpectedValueException('The date is not correct.');
    

    We need to call $date->getErrors() and use that to provide a more informative message in the exception.

  3. +++ b/core/modules/datetime/src/DateTimeComputed.php
    @@ -64,6 +67,9 @@ public function getValue() {
    +    catch (\UnexpectedValueException $e) {
    +      \Drupal::logger('datetime')->error($e->getMessage());
    +    }
    

    I don't think we need to add code to catch the exception. Logging is being handled in #3025642: Remove the try/catch block from DateTimeComputed::getValue(), and we can make sure that issue gets committed first.

vsujeetkumar’s picture

StatusFileSize
new1.04 KB
new1.3 KB

@jonathanshaw Fixed to failed tests(Due to syntax error.), Also worked on #5, Please have a look and advise.

vsujeetkumar’s picture

Status: Needs work » Needs review
jonathanshaw’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: +Needs tests

I think this is ready now. But it should probably have test coverage.

jonathanshaw’s picture

Status: Reviewed & tested by the community » Needs work

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.