Skip to content

Commit 683b4b9

Browse files
authored
[6.0] New Date and Datetime fields (joomla#37456)
1 parent 522827f commit 683b4b9

File tree

6 files changed

+644
-5
lines changed

6 files changed

+644
-5
lines changed

layouts/joomla/form/field/date.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
/**
4+
* @package Joomla.Site
5+
* @subpackage Layout
6+
*
7+
* @copyright (C) 2023 Open Source Matters, Inc. <https://www.joomla.org>
8+
* @license GNU General Public License version 2 or later; see LICENSE.txt
9+
*/
10+
11+
defined('_JEXEC') or die;
12+
13+
/**
14+
* Layout variables
15+
* -----------------
16+
* @var string $autocomplete Autocomplete attribute for the field.
17+
* @var boolean $autofocus Is autofocus enabled?
18+
* @var string $class Classes for the input.
19+
* @var string $description Description of the field.
20+
* @var boolean $disabled Is this field disabled?
21+
* @var string $group Group the field belongs to. <fields> section in form XML.
22+
* @var boolean $hidden Is this field hidden in the form?
23+
* @var string $hint Placeholder for the field.
24+
* @var string $id DOM id of the field.
25+
* @var string $label Label of the field.
26+
* @var string $labelclass Classes to apply to the label.
27+
* @var boolean $multiple Does this field support multiple values?
28+
* @var string $name Name of the input field.
29+
* @var string $onchange Onchange attribute for the field.
30+
* @var string $onclick Onclick attribute for the field.
31+
* @var string $pattern Pattern (Reg Ex) of value of the form field.
32+
* @var boolean $readonly Is this field read only?
33+
* @var boolean $required Is this field required?
34+
* @var integer $size Size attribute of the input.
35+
* @var string $value Value attribute of the field.
36+
* @var string $min
37+
* @var string $max
38+
*/
39+
extract($displayData);
40+
41+
$class = !empty($class) ? 'class="form-control ' . $class . '"' : 'class="form-control"';
42+
$readonly = $readonly ? ' readonly' : '';
43+
$disabled = $disabled ? ' disabled' : '';
44+
$required = $required ? ' required' : '';
45+
$hint = strlen($hint) ? ' placeholder="' . $hint . '"' : '';
46+
$autocomplete = !$autocomplete ? ' autocomplete="off"' : ' autocomplete="' . $autocomplete . '"';
47+
$autocomplete = $autocomplete == ' autocomplete="on"' ? '' : $autocomplete;
48+
$autofocus = $autofocus ? ' autofocus' : '';
49+
$pattern = !empty($pattern) ? ' pattern="' . $pattern . '"' : '';
50+
$onchange = !empty($onchange) ? ' onchange="' . $onchange . '"' : '';
51+
$minAttr = !empty($min) ? ' min="' . $this->escape($min) . '"' : '';
52+
$maxAttr = !empty($max) ? ' max="' . $this->escape($max) . '"' : '';
53+
54+
echo '<input type="date" name="' . $name . '" id="' . $id . '" value="' . $this->escape($value) . '"'
55+
. $class . $disabled . $readonly
56+
. $hint . $onchange . $required . $autocomplete . $autofocus . $pattern . $minAttr . $maxAttr . ' />';
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
/**
4+
* @package Joomla.Site
5+
* @subpackage Layout
6+
*
7+
* @copyright (C) 2023 Open Source Matters, Inc. <https://www.joomla.org>
8+
* @license GNU General Public License version 2 or later; see LICENSE.txt
9+
*/
10+
11+
defined('_JEXEC') or die;
12+
13+
/**
14+
* Layout variables
15+
* -----------------
16+
* @var string $autocomplete Autocomplete attribute for the field.
17+
* @var boolean $autofocus Is autofocus enabled?
18+
* @var string $class Classes for the input.
19+
* @var string $description Description of the field.
20+
* @var boolean $disabled Is this field disabled?
21+
* @var string $group Group the field belongs to. <fields> section in form XML.
22+
* @var boolean $hidden Is this field hidden in the form?
23+
* @var string $hint Placeholder for the field.
24+
* @var string $id DOM id of the field.
25+
* @var string $label Label of the field.
26+
* @var string $labelclass Classes to apply to the label.
27+
* @var boolean $multiple Does this field support multiple values?
28+
* @var string $name Name of the input field.
29+
* @var string $onchange Onchange attribute for the field.
30+
* @var string $onclick Onclick attribute for the field.
31+
* @var string $pattern Pattern (Reg Ex) of value of the form field.
32+
* @var boolean $readonly Is this field read only?
33+
* @var boolean $required Is this field required?
34+
* @var integer $size Size attribute of the input.
35+
* @var string $value Value attribute of the field.
36+
* @var string $min
37+
* @var string $max
38+
*/
39+
extract($displayData);
40+
41+
$class = !empty($class) ? 'class="form-control ' . $class . '"' : 'class="form-control"';
42+
$readonly = $readonly ? ' readonly' : '';
43+
$disabled = $disabled ? ' disabled' : '';
44+
$required = $required ? ' required' : '';
45+
$hint = strlen($hint) ? ' placeholder="' . $hint . '"' : '';
46+
$autocomplete = !$autocomplete ? ' autocomplete="off"' : ' autocomplete="' . $autocomplete . '"';
47+
$autocomplete = $autocomplete == ' autocomplete="on"' ? '' : $autocomplete;
48+
$autofocus = $autofocus ? ' autofocus' : '';
49+
$pattern = !empty($pattern) ? ' pattern="' . $pattern . '"' : '';
50+
$onchange = !empty($onchange) ? ' onchange="' . $onchange . '"' : '';
51+
$minAttr = !empty($min) ? ' min="' . $this->escape($min) . '"' : '';
52+
$maxAttr = !empty($max) ? ' max="' . $this->escape($max) . '"' : '';
53+
54+
echo '<input type="datetime-local" name="' . $name . '" id="' . $id . '" value="' . $this->escape($value) . '"'
55+
. $class . $disabled . $readonly
56+
. $hint . $onchange . $required . $autocomplete . $autofocus . $pattern . $minAttr . $maxAttr . ' />';

libraries/src/Form/Field/CalendarField.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,14 @@ public function __get($name)
192192
public function __set($name, $value)
193193
{
194194
switch ($name) {
195+
case 'value':
196+
if ($value instanceof \DateTimeInterface) {
197+
$this->value = $value->format('Y-m-d H:i:s');
198+
} else {
199+
$this->value = (string) $value;
200+
}
201+
break;
202+
195203
case 'maxlength':
196204
case 'maxyear':
197205
case 'minyear':
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
<?php
2+
3+
/**
4+
* Joomla! Content Management System
5+
*
6+
* @copyright (C) 2023 Open Source Matters, Inc. <https://www.joomla.org>
7+
* @license GNU General Public License version 2 or later; see LICENSE.txt
8+
*/
9+
10+
namespace Joomla\CMS\Form\Field;
11+
12+
use Joomla\CMS\Form\FormField;
13+
14+
/**
15+
* Date field
16+
*
17+
* @since __DEPLOY_VERSION__
18+
*/
19+
class DateField extends FormField
20+
{
21+
/**
22+
* The form field type.
23+
*
24+
* @var string
25+
*
26+
* @since __DEPLOY_VERSION__
27+
*/
28+
protected $type = 'Date';
29+
30+
/**
31+
* Name of the layout being used to render the field
32+
*
33+
* @var string
34+
*
35+
* @since __DEPLOY_VERSION__
36+
*/
37+
protected $layout = 'joomla.form.field.date';
38+
39+
/**
40+
* Min value Y-m-d
41+
*
42+
* @var string
43+
*
44+
* @since __DEPLOY_VERSION__
45+
*/
46+
protected $dateMin = '';
47+
48+
/**
49+
* Max value Y-m-d
50+
*
51+
* @var string
52+
*
53+
* @since __DEPLOY_VERSION__
54+
*/
55+
protected $dateMax = '';
56+
57+
/**
58+
* Method to attach a Form object to the field.
59+
*
60+
* @param \SimpleXMLElement $element The SimpleXMLElement object representing the `<field>` tag for the form field object.
61+
* @param mixed $value The form field value to validate.
62+
* @param string $group The field name group control value. This acts as as an array container for the field.
63+
* For example if the field has name="foo" and the group value is set to "bar" then the
64+
* full field name would end up being "bar[foo]".
65+
*
66+
* @return boolean True on success.
67+
*
68+
* @since __DEPLOY_VERSION__
69+
*/
70+
public function setup(\SimpleXMLElement $element, $value, $group = null)
71+
{
72+
if (!parent::setup($element, $value, $group)) {
73+
return false;
74+
}
75+
76+
foreach (['min', 'max'] as $attr) {
77+
$this->__set($attr, (string) $element[$attr]);
78+
}
79+
80+
return true;
81+
}
82+
83+
/**
84+
* Method to get certain otherwise inaccessible properties from the form field object.
85+
*
86+
* @param string $name The property name for which to get the value.
87+
*
88+
* @return mixed The property value or null.
89+
*
90+
* @since __DEPLOY_VERSION__
91+
*/
92+
public function __get($name)
93+
{
94+
switch ($name) {
95+
case 'min':
96+
return $this->dateMin;
97+
case 'max':
98+
return $this->dateMax;
99+
default:
100+
return parent::__get($name);
101+
}
102+
}
103+
104+
/**
105+
* Method to set certain otherwise inaccessible properties of the form field object.
106+
*
107+
* @param string $name The property name for which to set the value.
108+
* @param mixed $value The value of the property.
109+
*
110+
* @return void
111+
*
112+
* @since __DEPLOY_VERSION__
113+
*/
114+
public function __set($name, $value)
115+
{
116+
switch ($name) {
117+
case 'value':
118+
if ($value instanceof \DateTimeInterface) {
119+
$this->value = $value->format('Y-m-d');
120+
} else {
121+
$this->value = (string) $value;
122+
}
123+
break;
124+
case 'min':
125+
$this->dateMin = (string) $value;
126+
break;
127+
case 'max':
128+
$this->dateMax = (string) $value;
129+
break;
130+
default:
131+
parent::__set($name, $value);
132+
}
133+
}
134+
135+
/**
136+
* Method to get the data to be passed to the layout for rendering.
137+
*
138+
* @return array
139+
*
140+
* @since __DEPLOY_VERSION__
141+
*/
142+
protected function getLayoutData()
143+
{
144+
$data = parent::getLayoutData();
145+
146+
$data['min'] = $this->dateMin;
147+
$data['max'] = $this->dateMax;
148+
149+
return $data;
150+
}
151+
}

0 commit comments

Comments
 (0)