欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

drupal 字段 form textfield remove form-item div 移除 文本框外围的form-item的div Removing field wrapper in a form 有大用

I would like to remove div element on webform textfield. Instead of :
<div class="form-item webform-component webform-component-textfield" id="webform-component-vous-etes--nom">
  <input type="text" id="edit-submitted-vous-etes-nom" name="submitted[vous_etes][nom]" value="" size="60" maxlength="128" class="form-text">
</div>
I would like to have this :
<input type="text" id="edit-submitted-vous-etes-nom" name="submitted[vous_etes][nom]" value="" size="60" maxlength="128" class="form-text">



正确答案 
1down voteaccepted

You would do this by overriding theme_form_element() in your theme, eg, something along the lines of:

function YOURTHEME_form_element($variables) {

  $element=&$variables['element'];

  if ( /* this is a webform textfield */ ) {

    // create the html without the div wrapper

  } else {

    // do it the standard way

  }

  return /* the html */;

}

A look into includes/form.inc will show you the full theme_form_element() code that you could copy into your function above where necessary.

shareimprove this answer
 
   
Thank you. That works for me. – Mamadou Apr 7 '14 at 17:55
   
Can we custumise the output of the form tag ? : <form id="contact_form" action="" ..." – Mamadou Apr 7 '14 at 17:56
   
most definitely, see theme_form() in includes/form.inc and override it similarly as YOURTHEME_form() in your theme's template.php file – Jimajamma Apr 7 '14 at 17:59
   
If you're trying to target only one element and have all others flow as normal, return your html for the element you want and have all others hit return theme_form_element($variables); – Scott Joudry Feb 28 '15 at 17:44
   
I try this but when there is textarea field it yet return it with div wrapper – zhilevan Sep 24 '15 at 6:15
function YOURTHEME_form_element($variables) {
  $element = &$variables['element'];
  if ( isset($element['#remove-div-wrap']) && $element['#remove-div-wrap'] === true ) {
    return preg_replace('/<div[^>]*>|<\/div>/is', '', theme_form_element($variables));
  } else {
    return theme_form_element($variables);
  }
}

Use hook_form_alter() to specify the affected fields:

function YOURTHEME_form_FORMID_alter(&$form, &$form_state, $form_id) {
  $form['mail']['#remove-div-wrap'] = true;
}
shareimprove this answer

 
普通分类: