From d8f1758d57b2d5183949730195ab5912410d514f Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Tue, 22 Dec 2015 23:50:17 -0500 Subject: [PATCH] CRM_Core_Form - Support html5 field types --- CRM/Core/Form.php | 34 +++++++++++++++++----------------- CRM/Core/Form/Renderer.php | 13 ++++++++++--- 2 files changed, 27 insertions(+), 20 deletions(-) diff --git a/CRM/Core/Form.php b/CRM/Core/Form.php index 82e4e0afef..c4a4d65b90 100644 --- a/CRM/Core/Form.php +++ b/CRM/Core/Form.php @@ -201,6 +201,16 @@ class CRM_Core_Form extends HTML_QuickForm_Page { */ private $_chainSelectFields = array(); + /** + * Extra input types we support via the "add" method + * @var array + */ + public static $html5Types = array( + 'number', + 'url', + 'email', + ); + /** * Constructor for the basic form page. * @@ -327,7 +337,7 @@ class CRM_Core_Form extends HTML_QuickForm_Page { $attributes = '', $required = FALSE, $extra = NULL ) { // Fudge some extra types that quickform doesn't support - if ($type == 'wysiwyg' || $type == 'number') { + if ($type == 'wysiwyg' || in_array($type, self::$html5Types)) { $attributes = ($attributes ? $attributes : array()) + array('class' => ''); $attributes['class'] = ltrim($attributes['class'] . " crm-form-$type"); $type = $type == 'wysiwyg' ? 'textarea' : 'text'; @@ -1359,10 +1369,6 @@ class CRM_Core_Form extends HTML_QuickForm_Page { $isSelect = (in_array($widget, array( 'Select', 'Multi-Select', - 'Select State/Province', - 'Multi-Select State/Province', - 'Select Country', - 'Multi-Select Country', 'AdvMulti-Select', 'CheckBoxGroup', 'RadioGroup', @@ -1391,16 +1397,8 @@ class CRM_Core_Form extends HTML_QuickForm_Page { $props['data-api-field'] = $props['name']; } } - //Use select2 library for following widgets. - $isSelect2 = (in_array($widget, array( - 'Select', - 'Multi-Select', - 'Select State/Province', - 'Multi-Select State/Province', - 'Select Country', - 'Multi-Select Country', - ))); - if ($isSelect2) { + // Use select2 library for following widgets. + if (in_array($widget, array('Select', 'Multi-Select'))) { $props['class'] = (!empty($props['class']) ? $props['class'] . ' ' : '') . "crm-select2"; if ($props['context'] == 'search' || strpos($widget, 'Multi') !== FALSE) { $props['class'] .= ' huge'; @@ -1417,10 +1415,12 @@ class CRM_Core_Form extends HTML_QuickForm_Page { // TODO: refactor switch statement, to separate methods. switch ($widget) { case 'Text': - case 'Link': + case 'Url': + case 'Number': + case 'Email': //TODO: Autodetect ranges $props['size'] = isset($props['size']) ? $props['size'] : 60; - return $this->add('text', $name, $label, $props, $required); + return $this->add(strtolower($widget), $name, $label, $props, $required); case 'hidden': return $this->add('hidden', $name, NULL, $props, $required); diff --git a/CRM/Core/Form/Renderer.php b/CRM/Core/Form/Renderer.php index 86a39b8b47..60e997eeee 100644 --- a/CRM/Core/Form/Renderer.php +++ b/CRM/Core/Form/Renderer.php @@ -192,9 +192,16 @@ class CRM_Core_Form_Renderer extends HTML_QuickForm_Renderer_ArraySmarty { elseif (strpos($class, 'crm-form-contact-reference') !== FALSE) { self::preprocessContactReference($element); } - // Hack to support number fields - elseif (strpos($class, 'crm-form-number') !== FALSE) { - $element->setAttribute('type', 'number'); + // Hack to support html5 fields (number, url, etc) + else { + foreach (CRM_Core_Form::$html5Types as $type) { + if (strpos($class, "crm-form-$type") !== FALSE) { + $element->setAttribute('type', $type); + // Also add the "base" class for consistent styling + $class .= ' crm-form-text'; + break; + } + } } if ($required) { -- 2.25.1