1

Can I strip my form of these classes and DIVs as shown in the code below?

<form action="/example/unsubscribe" method="post" id="exampleform-unsubscribe" accept-charset="UTF-8">
  <div>/** this div **/
    <div class="form-item form-type-textfield form-item-email">/** strip these class and add my own class? **/
      <label for="edit-email">E-mail address <span class="form-required" title="This field is required.">*</span></label>/** strip classes form-required from span **/
      <input type="text" id="edit-email" name="email" value="" size="60" maxlength="128" class="form-text required" />/** strip form-text and required class **/
    </div>
    <input type="submit" id="edit-submit" name="op" value="Remove me!" class="form-submit" />/** remove form-submit class **/ <input type="hidden" name="form_build_id" value="formjKkl1KLWJLnv0hM4DSVd8-40boTgBQAzWWhUn44c15Q" />
    <input type="hidden" name="form_token" value="LB07DqsDXK9idWdOHLxUen7jKxm52JqTyHiR7-pNumA" />
    <input type="hidden" name="form_id" value="exampleform_unsubscribe" />
  </div>/** this div **/
</form>

Would it affect any form or ajax form functions of the form?

  • How do you plan to do it? And why would you want to do it? – Mołot Jan 24 '16 at 11:21
  • Via template.php code. I want my code to be as clean as possible, but i'm pretty sure that I can't change or remove IDs. – user504944 Jan 24 '16 at 17:10
1

There are 2 Drupal theme functions which you need to override in your theme's template.php

You can paste this code in your theme's template.php and change "themename" to the name of your theme (Drupal 7 code):

It shouldn't affect any ajax form functions if you have a custom theme, however if you are using a contributed theme you should check the javascript files to see if there are any functions using the classes / selectors you have stripped out.

  1. Override theme_form() to remove the div

    
    function themename_form($variables) {
      $element = $variables['element'];
      if (isset($element['#action'])) {
        $element['#attributes']['action'] = drupal_strip_dangerous_protocols($element['#action']);
    }
    element_set_attributes($element, array('method', 'id'));
    if (empty($element['#attributes']['accept-charset'])) {
     $element['#attributes']['accept-charset'] = "UTF-8";
    }
    return '' . $element['#children'] . '';
    }

  2. Override theme_form_element() function to strip out the classes

    function themename_form_element($variables) { $element = &$variables['element']; $element += array( '#title_display' => 'before', ); if (isset($element['#markup']) && !empty($element['#id'])) { $attributes['id'] = $element['#id']; } // Remove line below if you don't want a custom class $attributes['class'] = array('your-custom-class');

    // Add a class for disabled elements to facilitate cross-browser styling. if (!empty($element['#attributes']['disabled'])) { $attributes['class'][] = 'form-disabled'; } $output = '' . "\n"; // If #title is not set, we don't display any label or required marker. if (!isset($element['#title'])) { $element['#title_display'] = 'none'; }

    $prefix = isset($element['#field_prefix']) ? '' . $element['#field_prefix'] . ' ' : '';

    $suffix = isset($element['#field_suffix']) ? ' ' . $element['#field_suffix'] . '' : '';

    switch ($element['#title_display']) { case 'before': case 'invisible': $output .= ' ' . theme('form_element_label', $variables); $output .= ' ' . $prefix . $element['#children'] . $suffix . "\n"; break;

    case 'after':
      $output .= ' ' . $prefix . $element['#children'] . $suffix;
      $output .= ' ' . theme('form_element_label', $variables) . "\n";
      break;
    
    case 'none':
    case 'attribute':
      // Output no label and no required marker, only the children.
      $output .= ' ' . $prefix . $element['#children'] . $suffix . "\n";
      break;
    

    }

    if (!empty($element['#description'])) { $output .= '' . $element['#description'] . "\n"; }

    $output .= "\n";

    return $output; }

|improve this answer|||||

Not the answer you're looking for? Browse other questions tagged or ask your own question.