[英]how to remove step attribute of number field from webform component in drupal 7
我设计了一个Drupal 7网络表单,其中包含字段(用户名,电话号码,电子邮件ID)。 请在下面查看源代码:
电话号码字段具有step属性,这会导致可访问性错误。 我要删除此内容以确保可访问性。 我该怎么办?
您可以使用表单alter来执行此操作:
function MYMODULE_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'your_form_id') {
$fieldname = 'my_field_name_to_edit';
if(isset($form[$fieldname]['#attributes']['step'])) unset($form[$fieldname]['#attributes']['step']);
}
}
在此处查找文档: https : //api.drupal.org/api/drupal/modules%21system%21system.api.php/function/hook_form_alter/7.x
和
https://api.drupal.org/api/drupal/developer%21topics%21forms_api_reference.html/7.x#attributes
对于Webfor Number字段,将step属性强行添加到theme_webform_number()
,因此唯一的方法是在您的主题中覆盖该函数并删除该代码位:
/**
* Theme function to render a number component.
*/
function YOURTHEME_webform_number($variables) {
$element = $variables['element'];
// This IF statement is mostly in place to allow our tests to set type="text"
// because SimpleTest does not support type="number".
if (!isset($element['#attributes']['type'])) {
// HTML5 number fields are no long used pending better browser support.
// See issues #2290029, #2202905.
// $element['#attributes']['type'] = 'number';
$element['#attributes']['type'] = 'text';
}
// Step property *must* be a full number with 0 prefix if a decimal.
if (!empty($element['#step']) && !is_int($element['#step'] * 1)) {
$decimals = strlen($element['#step']) - strrpos($element['#step'], '.') - 1;
$element['#step'] = sprintf('%1.' . $decimals . 'F', $element['#step']);
}
// If the number is not an integer and step is undefined/empty, set the "any"
// value to allow any decimal.
if (empty($element['#integer']) && empty($element['#step'])) {
$element['#step'] = 'any';
}
elseif ($element['#integer'] && empty($element['#step'])) {
$element['#step'] = 1;
}
unset($element['#step']);
// Convert properties to attributes on the element if set.
// removed step attribute.
foreach (array('id', 'name', 'value', 'size', 'min', 'max') as $property) {
if (isset($element['#' . $property]) && $element['#' . $property] !== '') {
$element['#attributes'][$property] = $element['#' . $property];
}
}
_form_set_class($element, array('form-text', 'form-number'));
return '<input' . drupal_attributes($element['#attributes']) . ' />';
}
希望这对您有帮助...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.