Skip to content

Commit

Permalink
[4.0] Implement time field (#26184)
Browse files Browse the repository at this point in the history
  • Loading branch information
VladikB authored and wilsonge committed Sep 13, 2019
1 parent 05a45ae commit ba5e292
Show file tree
Hide file tree
Showing 5 changed files with 442 additions and 0 deletions.
4 changes: 4 additions & 0 deletions administrator/language/en-GB/en-GB.lib_joomla.ini
Expand Up @@ -314,6 +314,10 @@ JLIB_FORM_CHANGE_USER="Select User"
JLIB_FORM_CONTAINS_INVALID_FIELDS="The form cannot be submitted as it's missing required data. <br> Please correct the marked fields and try again."
JLIB_FORM_ERROR_FIELDS_CATEGORY_ERROR_EXTENSION_EMPTY="Extension attribute is empty in the category field."
JLIB_FORM_ERROR_FIELDS_GROUPEDLIST_ELEMENT_NAME="Unknown element type: %s"
JLIB_FORM_FIELD_INVALID_MAX_TIME="The time you entered is after the maximum time."
JLIB_FORM_FIELD_INVALID_MIN_TIME="The time you entered is before the minimum time."
JLIB_FORM_FIELD_INVALID_TIME_INPUT="Invalid time format. Please use hh:mm."
JLIB_FORM_FIELD_INVALID_TIME_INPUT_SECONDS="Invalid time format. Please use hh:mm:ss."
JLIB_FORM_ERROR_NO_DATA="No data."
JLIB_FORM_ERROR_VALIDATE_FIELD="Invalid xml field."
JLIB_FORM_ERROR_XML_FILE_DID_NOT_LOAD="XML file did not load."
Expand Down
4 changes: 4 additions & 0 deletions language/en-GB/en-GB.lib_joomla.ini
Expand Up @@ -314,6 +314,10 @@ JLIB_FORM_ERROR_NO_DATA="No data."
JLIB_FORM_ERROR_VALIDATE_FIELD="Invalid xml field."
JLIB_FORM_ERROR_XML_FILE_DID_NOT_LOAD="XML file did not load."
JLIB_FORM_FIELD_INVALID="Invalid field:&#160"
JLIB_FORM_FIELD_INVALID_MAX_TIME="The time you entered is after the maximum time."
JLIB_FORM_FIELD_INVALID_MIN_TIME="The time you entered is before the minimum time."
JLIB_FORM_FIELD_INVALID_TIME_INPUT="Invalid time format. Please use hh:mm."
JLIB_FORM_FIELD_INVALID_TIME_INPUT_SECONDS="Invalid time format. Please use hh:mm:ss."
JLIB_FORM_FIELD_INVALID_VALUE="This value is not valid"
JLIB_FORM_FIELD_REQUIRED_CHECK="One of the options must be selected"
JLIB_FORM_FIELD_REQUIRED_VALUE="Please fill in this field"
Expand Down
70 changes: 70 additions & 0 deletions layouts/joomla/form/field/time.php
@@ -0,0 +1,70 @@
<?php
/**
* @package Joomla.Site
* @subpackage Layout
*
* @copyright Copyright (C) 2005 - 2019 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/

defined('JPATH_BASE') or die;

/**
* @var array $displayData Array with values.
*/
extract($displayData);

/**
* Layout variables
* -----------------
* @var boolean $autofocus Is autofocus enabled?
* @var string $class Classes for the input.
* @var string $description Description of the field.
* @var boolean $disabled Is this field disabled?
* @var string $group Group the field belongs to. <fields> section in form XML.
* @var boolean $hidden Is this field hidden in the form?
* @var string $hint Placeholder for the field.
* @var string $id DOM id of the field.
* @var string $label Label of the field.
* @var string $labelclass Classes to apply to the label.
* @var boolean $multiple Does this field support multiple values?
* @var string $name Name of the input field.
* @var string $onchange Onchange attribute for the field.
* @var string $onclick Onclick attribute for the field.
* @var string $pattern Pattern (Reg Ex) of value of the form field.
* @var boolean $readonly Is this field read only?
* @var boolean $repeat Allows extensions to duplicate elements.
* @var boolean $required Is this field required?
* @var boolean $spellcheck Spellcheck state for the form field.
* @var string $validate Validation rules to apply.
* @var string $value Value attribute of the field.
* @var array $checkedOptions Options that will be set as checked.
* @var boolean $hasValue Has this field a value assigned?
* @var array $options Options available for this field.
* @var array $inputType Options available for this field.
* @var array $spellcheck Options available for this field.
* @var string $accept File types that are accepted.
*/

$attributes = [
!empty($class) ? 'class="form-control ' . $class . '"' : 'class="form-control"',
!empty($description) ? 'aria-describedby="' . $name . '-desc"' : '',
$disabled ? 'disabled' : '',
$readonly ? 'readonly' : '',
strlen($hint) ? 'placeholder="' . htmlspecialchars($hint, ENT_COMPAT, 'UTF-8') . '"' : '',
!empty($onchange) ? 'onchange="' . $onchange . '"' : '',
isset($max) ? 'max="' . $max . '"' : '',
isset($step) ? 'step="' . $step . '"' : '',
isset($min) ? 'min="' . $min . '"' : '',
$required ? 'required' : '',
$autofocus ? 'autofocus' : ''
];

?>
<input
type="time"
name="<?php echo $name; ?>"
id="<?php echo $id; ?>"
value="<?php echo $value ?>"
<?php echo implode(' ', $attributes); ?>>

181 changes: 181 additions & 0 deletions libraries/src/Form/Field/TimeField.php
@@ -0,0 +1,181 @@
<?php
/**
* Joomla! Content Management System
*
* @copyright Copyright (C) 2005 - 2019 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/

namespace Joomla\CMS\Form\Field;

defined('JPATH_PLATFORM') or die;

use Joomla\CMS\Component\ComponentHelper;
use Joomla\CMS\Factory;
use Joomla\CMS\Form\FormField;
use Joomla\CMS\HTML\HTMLHelper;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Uri\Uri;

/**
* Form Field class for the Joomla Platform.
* Provides a select list of integers with specified first, last and step values.
*
* @since 4.0.0
*/
class TimeField extends FormField
{
/**
* The form field type.
*
* @var string
* @since 4.0.0
*/
protected $type = 'Time';

/**
* The allowable minimal value of the field.
*
* @var integer
* @since 4.0.0
*/
protected $min;

/**
* The allowable maximal value of the field.
*
* @var integer
* @since 4.0.0
*/
protected $max;

/**
* Steps between different values
*
* @var integer
* @since 4.0.0
*/
protected $step;

/**
* Name of the layout being used to render the field
*
* @var string
* @since 4.0.0
*/
protected $layout = 'joomla.form.field.time';

/**
* Method to set certain otherwise inaccessible properties of the form field object.
*
* @param string $name The property name for which to set the value.
* @param mixed $value The value of the property.
*
* @return void
*
* @since 4.0.0
*/
public function __set($name, $value)
{
switch ($name)
{
case 'min':
$this->min = (string) $value;
break;

case 'max':
$this->max = (string) $value;
break;

case 'step':
$this->step = (string) $value;
break;

default:
parent::__set($name, $value);
}
}

/**
* Method to attach a Form object to the field.
*
* @param \SimpleXMLElement $element The SimpleXMLElement object representing the `<field>` tag for the form field object.
* @param mixed $value The form field value to validate.
* @param string $group The field name group control value. This acts as an array container for the field.
* For example if the field has name="foo" and the group value is set to "bar" then the
* full field name would end up being "bar[foo]".
*
* @see FormField::setup()
* @return boolean True on success.
*
* @since 4.0.0
*/
public function setup(\SimpleXMLElement $element, $value, $group = null)
{
$return = parent::setup($element, $value, $group);

if ($return)
{
// It is better not to force any default limits if none is specified
$this->max = isset($this->element['max']) ? (string) $this->element['max'] : null;
$this->min = isset($this->element['min']) ? (string) $this->element['min'] : null;
$this->step = isset($this->element['step']) ? (float) $this->element['step'] : null;
}

return $return;
}

/**
* Method to get certain otherwise inaccessible properties from the form field object.
*
* @param string $name The property name for which to get the value.
*
* @return mixed The property value or null.
*
* @since 4.0.0
*/
public function __get($name)
{
switch ($name)
{
case 'min':
case 'max':
case 'step':
return $this->$name;
}

return parent::__get($name);
}

/**
* Method to get the field input markup.
*
* @return string The field input markup.
*
* @since 4.0.0
*/
protected function getInput()
{
return $this->getRenderer($this->layout)->render($this->getLayoutData());
}

/**
* Method to get the data to be passed to the layout for rendering.
*
* @return array
*
* @since 4.0.0
*/
protected function getLayoutData()
{
$data = parent::getLayoutData();

$extraData = [
'min' => $this->min,
'max' => $this->max,
'step' => $this->step,
];

return array_merge($data, $extraData);
}
}

0 comments on commit ba5e292

Please sign in to comment.