簡體   English   中英

如何從Drupal 7中的Webform組件中刪除數字字段的Step屬性

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM