Problem/Motivation

The date type element has #date_date_callback option which can be used to alter it.
But it doesn't work properly because of function_exists() function inside processDatetime method of Datetime element. It always returns FALSE for all callbacks added in the #date_date_callback option.

Proposed resolution

Repace function_exists() by call_user_func_array() to be able execute callback with context

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Arez created an issue. See original summary.

Brolad’s picture

Status: Active » Needs review
FileSize
1.29 KB
jhedstrom’s picture

Status: Needs review » Needs work

Thanks for this! I've noticed myself that callables cannot be used here, and only functions, which is really annoying.

+++ b/core/lib/Drupal/Core/Datetime/Element/Datetime.php
@@ -274,9 +274,7 @@ public static function processDatetime(&$element, FormStateInterface $form_state
+          call_user_func_array($callback, [&$element, $form_state, $date]);

@@ -305,10 +303,8 @@ public static function processDatetime(&$element, FormStateInterface $form_state
+          call_user_func_array($callback, [&$element, $form_state, $date]);

I think we still need to check if this is valid, so replacing function_exists with is_callable, and then using call_user_func_array should work.

andypost’s picture

Issue tags: +Needs tests

it needs tests and surely must use is_callable instead of function_exists

Vj’s picture

Status: Needs work » Needs review
FileSize
704 bytes

Tested below patch with following datetime field

    $test = [$this, 'testdatecallbackfromclass'];
    $form['datetime'] = [
      '#type' => 'datetime',
      '#title' => t('date'),
      '#description' => t('Description about the date.'),
      '#date_date_callbacks' => [$test, 'testdatecallbackfrommodule'],
    ];

@andypost : Can you give some idea about test to write, should i write tests in core/modules/datetime/tests/src/Functional/DateTimeFieldTest.php file ? and what can be added to compare.

andypost’s picture

@Vj I suppose there should be tests for datetime form element (very probably it does not exists for core/lib/Drupal/Core/Datetime/Element/Datetime.php)

Example is core/tests/Drupal/KernelTests/Core/Element/PathElementFormTest.php but I can't find similar one for this element

Vj’s picture

@andypost

Tried creating test for the first time :P
Please review and let me know if it works or any changes required.

andypost’s picture

Issue tags: -Needs tests

@Vj it looks great! Please attach patch with test only to show that without fix it will fail

Vj’s picture

The last submitted patch, 9: 2992580-09-testonly.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

jhedstrom’s picture

Status: Needs review » Reviewed & tested by the community

This looks great. Thanks all!

catch’s picture

Status: Reviewed & tested by the community » Fixed

Fixed these code style issues locally, and committed/pushed to 8.7.x, thanks!

FILE: ...sts/Drupal/KernelTests/Core/Datetime/DatetimeElementFormTest.php
----------------------------------------------------------------------
FOUND 3 ERRORS AND 2 WARNINGS AFFECTING 5 LINES
----------------------------------------------------------------------
  7 | WARNING | [x] Unused use statement
  9 | ERROR   | [x] There must be one blank line after the last USE
    |         |     statement; 3 found;
 12 | ERROR   | [ ] More than 2 empty lines are not allowed
 23 | ERROR   | [x] Additional blank lines found at end of doc
    |         |     comment
 68 | WARNING | [x] A comma should follow the last multiline array
    |         |     item. Found: ]
----------------------------------------------------------------------
PHPCBF CAN FIX THE 4 MARKED SNIFF VIOLATIONS AUTOMATICALLY
----------------------------------------------------------------------

  • catch committed 177ec26 on 8.7.x
    Issue #2992580 by Vj, Arez, andypost, jhedstrom: Custom callbacks doesn'...

Status: Fixed » Closed (fixed)

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

dww’s picture

Version: 8.7.x-dev » 8.6.x-dev
Status: Closed (fixed) » Needs review

Can we fix this in 8.6.x, too?
Patch applies cleanly.

dww’s picture

Status: Needs review » Reviewed & tested by the community

As expected, bot is happy on 8.6.x.

Cherry-picking this to 8.6.x would unblock #2672950: Notice: Undefined index: #default_value in Drupal\Core\Datetime\Element\Datetime::valueCallback() (line 103 which in turn unblocks / simplifies #2419131: [PP-1] #states attribute does not work on #type datetime.

This seems like an utterly non-disruptive bugfix with test coverage.

RTBC.

Thanks!
-Derek

dww’s picture

Sorry, in case that wasn't clear, I had re-triggered patch #9 to test against 8.6.x and it's all green...

Thanks,
-Derek

Wim Leers’s picture

This indeed looks very non-disruptive.

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

I agree we can backport this.

  • alexpott committed f9ec0de on 8.6.x authored by catch
    Issue #2992580 by Vj, Arez, andypost, jhedstrom: Custom callbacks doesn'...

Status: Fixed » Closed (fixed)

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