From b6caffebd89fe236be291b7634017e63a5b958cc Mon Sep 17 00:00:00 2001
From: Chris Ruppel <chris.ruppel@gmail.com>
Date: Mon, 22 Aug 2011 18:05:55 -0500
Subject: [PATCH] Issue 1174634: adding tel FAPI element; abstracting autocomplete for additional text inputs;

---
 includes/common.inc                         |    3 ++
 includes/form.inc                           |   45 ++++++++++++++++++++++++--
 modules/simpletest/drupal_web_test_case.php |    1 +
 modules/simpletest/tests/form.test          |    5 ++-
 modules/simpletest/tests/form_test.module   |    2 +-
 modules/system/system.module                |    9 +++++
 6 files changed, 59 insertions(+), 6 deletions(-)

diff --git a/includes/common.inc b/includes/common.inc
index 1e287f8..6606152 100644
--- a/includes/common.inc
+++ b/includes/common.inc
@@ -6562,6 +6562,9 @@ function drupal_common_theme() {
     'textfield' => array(
       'render element' => 'element',
     ),
+    'tel' => array(
+      'render element' => 'element',
+    ),
     'form' => array(
       'render element' => 'element',
     ),
diff --git a/includes/form.inc b/includes/form.inc
index 40363d6..371eb37 100644
--- a/includes/form.inc
+++ b/includes/form.inc
@@ -3660,8 +3660,47 @@ function theme_textfield($variables) {
   element_set_attributes($element, array('id', 'name', 'value', 'size', 'maxlength', 'placeholder'));
   _form_set_class($element, array('form-text'));
 
+  $extra = form_add_autocomplete($element);
+  $output = '<input' . drupal_attributes($element['#attributes']) . ' />';
+
+  return $output . $extra;
+}
+
+/**
+ * Returns HTML for a tel form element.
+ *
+ * @param $variables
+ *   An associative array containing:
+ *   - element: An associative array containing the properties of the element.
+ *     Properties used: #title, #value, #description, #size, #maxlength,
+ *     #placeholder, #required, #attributes, #autocomplete_path.
+ *
+ * @ingroup themeable
+ */
+function theme_tel($variables) {
+  $element = $variables['element'];
+  element_set_attributes($element, array('type', 'id', 'name', 'value', 'size', 'maxlength', 'placeholder'));
+  _form_set_class($element, array('form-text', 'form-tel'));
+
+  $extra = form_add_autocomplete($element);
+  $output = '<input' . drupal_attributes($element['#attributes']) . ' />';
+
+  return $output . $extra;
+}
+
+/**
+ * Return the autocompletion HTML for a form element.
+ *
+ * @param $element
+ *   The renderable element to process for autocompletion.
+ *
+ * @return
+ *   The rendered autocompletion element HTML, or an empty string if the field
+ *   has no autocompletion enabled.
+ */
+function form_add_autocomplete(&$element) {
   $extra = '';
-  if ($element['#autocomplete_path'] && drupal_valid_path($element['#autocomplete_path'])) {
+  if (!empty($element['#autocomplete_path']) && drupal_valid_path($element['#autocomplete_path'])) {
     drupal_add_library('system', 'drupal.autocomplete');
     $element['#attributes']['class'][] = 'form-autocomplete';
 
@@ -3674,9 +3713,7 @@ function theme_textfield($variables) {
     $extra = '<input' . drupal_attributes($attributes) . ' />';
   }
 
-  $output = '<input' . drupal_attributes($element['#attributes']) . ' />';
-
-  return $output . $extra;
+  return $extra;
 }
 
 /**
diff --git a/modules/simpletest/drupal_web_test_case.php b/modules/simpletest/drupal_web_test_case.php
index 5c39cfc..a4e72ad 100644
--- a/modules/simpletest/drupal_web_test_case.php
+++ b/modules/simpletest/drupal_web_test_case.php
@@ -2150,6 +2150,7 @@ class DrupalWebTestCase extends DrupalTestCase {
           case 'textarea':
           case 'hidden':
           case 'password':
+          case 'tel':
             $post[$name] = $edit[$name];
             unset($edit[$name]);
             break;
diff --git a/modules/simpletest/tests/form.test b/modules/simpletest/tests/form.test
index 71187b5..f36f28b 100644
--- a/modules/simpletest/tests/form.test
+++ b/modules/simpletest/tests/form.test
@@ -37,6 +37,9 @@ class FormsTestCase extends DrupalWebTestCase {
     $elements['textfield']['element'] = array('#title' => $this->randomName(), '#type' => 'textfield');
     $elements['textfield']['empty_values'] = $empty_strings;
 
+    $elements['telephone']['element'] = array('#title' => $this->randomName(), '#type' => 'tel');
+    $elements['telephone']['empty_values'] = $empty_strings;
+
     $elements['password']['element'] = array('#title' => $this->randomName(), '#type' => 'password');
     $elements['password']['empty_values'] = $empty_strings;
 
@@ -258,7 +261,7 @@ class FormsTestCase extends DrupalWebTestCase {
 
     // All the elements should be marked as disabled, including the ones below
     // the disabled container.
-    $this->assertEqual(count($disabled_elements), 32, t('The correct elements have the disabled property in the HTML code.'));
+    $this->assertEqual(count($disabled_elements), 33, t('The correct elements have the disabled property in the HTML code.'));
 
     $this->drupalPost(NULL, $edit, t('Submit'));
     $returned_values['hijacked'] = drupal_json_decode($this->content);
diff --git a/modules/simpletest/tests/form_test.module b/modules/simpletest/tests/form_test.module
index a934816..0e63fa4 100644
--- a/modules/simpletest/tests/form_test.module
+++ b/modules/simpletest/tests/form_test.module
@@ -1065,7 +1065,7 @@ function form_test_checkboxes_radios($form, &$form_state, $customize = FALSE) {
  */
 function _form_test_disabled_elements($form, &$form_state) {
   // Elements that take a simple default value.
-  foreach (array('textfield', 'textarea', 'hidden') as $type) {
+  foreach (array('textfield', 'textarea', 'tel', 'hidden') as $type) {
     $form[$type] = array(
       '#type' => $type,
       '#title' => $type,
diff --git a/modules/system/system.module b/modules/system/system.module
index 946ef0c..4faef3d 100644
--- a/modules/system/system.module
+++ b/modules/system/system.module
@@ -355,6 +355,15 @@ function system_element_info() {
     '#theme' => 'textfield',
     '#theme_wrappers' => array('form_element'),
   );
+  $types['tel'] = array(
+    '#input' => TRUE,
+    '#size' => 60,
+    '#maxlength' => 128,
+    '#autocomplete_path' => FALSE,
+    '#process' => array('ajax_process_form'),
+    '#theme' => 'tel',
+    '#theme_wrappers' => array('form_element'),
+  );
   $types['machine_name'] = array(
     '#input' => TRUE,
     '#default_value' => NULL,
-- 
1.7.3.4

