When trying to theme a form and using the HTML5 input types, D7 ignores the "type" attribute because it is hardcoded to "text". Attached patch will only hardcode to text if no alternative type is specified.

Example code:

$form["element"]["#attributes"]["type"]="number";

Comments

Sriparna Khatua’s picture

If user wants to add "text" field then $element['#attributes']['type'] = 'text'; should be text and it is working fine.

purushotam.rai’s picture

Rightly noticed, Drupal 7 does not regards 'type' attribute of 'textfield' element and hence there must be some solution for this. The above patch does not works for me.

purushotam.rai’s picture

Status: Active » Needs work
hgoto’s picture

The patch seems to work for me... @purushotam.rai, if possible, could you please provide more explanation or examle of "Drupal 7 does not regards 'type' attribute of 'textfield' element and hence there must be some solution for this. The above patch does not works for me."?

littletiger’s picture

@purushotam.rai : what's the code you used to test this out? could you share?

@hgoto : likewise, for you it works, can you share a code extract (or whole function) to demo in what use case it works for you?

Having the same need and would love to see progress on this and see the patch committed if validated.

purushotam.rai’s picture

StatusFileSize
new700 bytes

Hi @littletiger,

Here goes my patch to solve this issue. It basically respects the type attribute provided using form api for textfield.

Thanks

purushotam.rai’s picture

Status: Needs work » Needs review

The last submitted patch, drupal-textfield-allow-html5-types.diff, failed testing.

Status: Needs review » Needs work

The last submitted patch, 6: allow_custom_input-2545318-6.patch, failed testing.

purushotam.rai’s picture

Status: Needs work » Needs review
pashupathi nath gajawada’s picture

StatusFileSize
new694 bytes

Hi,
Please find the updated patch.

Status: Needs review » Needs work

The last submitted patch, 11: 2545318-11.patch, failed testing.

purushotam.rai’s picture

@pashupathi

Could you please provide the interdiff.

hgoto’s picture

The patch #11 uses bracket syntax for the array, and it seems to be the only difference from the patch #6. I don't understand why this is necessary. The patch #6 is proper one.

purushotam.rai’s picture

Status: Needs work » Needs review
michel.g’s picture

This will not work properly with text fields because #type is already set to textfield in $variables['element']. (Hence all the tests failing)
What we could do is define something like a #subtype in which we should allow people to add a few already not existing types.

  • number
  • email
  • password
  • color
  • url
  • tel
michel.g’s picture

StatusFileSize
new1.11 KB

Added a fresh patch which should not break the existing tests.

Status: Needs review » Needs work

The last submitted patch, 17: allow-custom-input-types-12773594-17.patch, failed testing. View results

michel.g’s picture

Seems there is 1 failure (still better than 973 failures) but it has to do with the max length of a "Menu link title" field.

To be investigated.

michel.g’s picture

Status: Needs work » Needs review
StatusFileSize
new1.14 KB

The failure was very silly. The test doesn't expect maxlength to the the last value in the HTML element. (It expects a space behind it)
I could fix it by just reordering some assignments.

This code will work by defining a #subtype when creating a form element:

$form['number_field'] = array(
'#type' => 'textfield',
'#subtype' => 'number',
'#title' => t('Number field'),
'#default_value' => 1,
'#size' => 5,
'#maxlength' => 5,
);

sokru’s picture

Status: Needs review » Reviewed & tested by the community

Tested & looks good to me.

AusJohn’s picture

#20 worked great for us.

We changed the quantity field in commerce to type=number.
It feels a lot better to use as a potential customer now.

With the patch in #20, created a custom module and put the following in the mymodule.module file:

function mymodule_form_alter(&$form, &$form_state, $form_id){
 if (isset($form['quantity'])){
    $form['quantity']['#subtype'] = 'number';
  }
}

Sets the quantity input type to number.
We did the condition on existence of field, not form-id as we have multiple add-to-cart forms. Trying to target by form id would be a nightmare.

sokru’s picture

Version: 7.38 » 7.x-dev
guypaddock’s picture

Note that the patch file name allow-custom-input-types-12773594-19.patch cites the wrong issue number; it should be 2545318 not 12773594.

adevms’s picture

Changed the patch name and also added min and max attributes options.

poker10’s picture

Title: Allow custom input types for textfields » Allow custom input types for textfields (HTML5 input types)
Component: field system » forms system
Category: Bug report » Feature request
Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs change record, +Needs tests

Thanks for the report and patches. I think this is not a bug, but a feature request - D7 currently does not support HTML5 input types. This functionality can be achieved by several ways:

- contrib module (for example this: https://www.drupal.org/project/elements)
- theme_textfield override in your theme

Anyway, if this issue should be considered, minimally two things needs to be added: tests, change record. Thus I am switching status back to Needs work.

Status: Needs work » Closed (outdated)

Automatically closed because Drupal 7 security and bugfix support has ended as of 5 January 2025. If the issue verifiably applies to later versions, please reopen with details and update the version.