Skip to content

Commit 70c74db

Browse files
Nathaniel Catchpolealexpott
authored andcommitted
Issue #2992580 by Vj, Arez, andypost, jhedstrom: Custom callbacks doesn't work
(cherry picked from commit 177ec26)
1 parent 8bd590f commit 70c74db

File tree

2 files changed

+101
-1
lines changed

2 files changed

+101
-1
lines changed

lib/Drupal/Core/Datetime/Element/Datetime.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ public static function processDatetime(&$element, FormStateInterface $form_state
274274
// Allows custom callbacks to alter the element.
275275
if (!empty($element['#date_date_callbacks'])) {
276276
foreach ($element['#date_date_callbacks'] as $callback) {
277-
if (function_exists($callback)) {
277+
if (is_callable($callback)) {
278278
$callback($element, $form_state, $date);
279279
}
280280
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
<?php
2+
3+
namespace Drupal\KernelTests\Core\Datetime;
4+
5+
use Drupal\Core\Datetime\DrupalDateTime;
6+
use Drupal\Core\Form\FormInterface;
7+
use Drupal\Core\Form\FormStateInterface;
8+
use Drupal\KernelTests\KernelTestBase;
9+
10+
/**
11+
* Tests DatetimeElement functionality.
12+
*
13+
* @group Form
14+
*/
15+
class DatetimeElementFormTest extends KernelTestBase implements FormInterface {
16+
17+
/**
18+
* The variable under test.
19+
*/
20+
protected $flag;
21+
22+
/**
23+
* Modules to enable.
24+
*
25+
* @var array
26+
*/
27+
public static $modules = ['datetime', 'system'];
28+
29+
/**
30+
* Sets up the test.
31+
*/
32+
protected function setUp() {
33+
parent::setUp();
34+
}
35+
36+
/**
37+
* {@inheritdoc}
38+
*/
39+
public function getFormId() {
40+
return 'test_datetime_element';
41+
}
42+
43+
/**
44+
* {@inheritdoc}
45+
*/
46+
public function datetimecallback($date) {
47+
$this->flag = 'Date time callback called.';
48+
}
49+
50+
/**
51+
* {@inheritdoc}
52+
*/
53+
public function buildForm(array $form, FormStateInterface $form_state) {
54+
55+
$form['datetime_element'] = [
56+
'#title' => 'datelist test',
57+
'#type' => 'datetime',
58+
'#default_value' => new DrupalDateTime('2000-01-01 00:00:00'),
59+
'#date_date_format' => ['Y-m-d'],
60+
'#date_time_format' => ['H:i:s'],
61+
'#date_date_element' => 'HTML Date',
62+
'#date_time_element' => 'HTML Time',
63+
'#date_increment' => 1,
64+
'#date_date_callbacks' => [[$this, 'datetimecallback']],
65+
];
66+
67+
$form['submit'] = [
68+
'#type' => 'submit',
69+
'#value' => t('Submit'),
70+
];
71+
72+
return $form;
73+
}
74+
75+
/**
76+
* {@inheritdoc}
77+
*/
78+
public function submitForm(array &$form, FormStateInterface $form_state) {}
79+
80+
/**
81+
* Form validation handler.
82+
*
83+
* @param array $form
84+
* An associative array containing the structure of the form.
85+
* @param \Drupal\Core\Form\FormStateInterface $form_state
86+
* The current state of the form.
87+
*/
88+
public function validateForm(array &$form, FormStateInterface $form_state) {}
89+
90+
/**
91+
* Tests that default handlers are added even if custom are specified.
92+
*/
93+
public function testDatetimeElement() {
94+
$form = \Drupal::formBuilder()->getForm($this);
95+
$this->render($form);
96+
97+
$this->assertEqual(t('Date time callback called.'), $this->flag);
98+
}
99+
100+
}

0 commit comments

Comments
 (0)