1

How to let Drupal print only necessary HTML in forms. I want to override fields input.

This is the current code for node add form:

<form ...>
...
    <div class="field-type-text field-name-field-price-ad field-widget-text-textfield form-wrapper" id="edit-field-price-ad">             
      <div id="field-price-ad-add-more-wrapper">                 
        <div class="form-item form-type-textfield form-item-field-price-ad-und-0-value">                        
          <label for="edit-field-price-ad-und-0-value">Price</label>                       
          <input class="text-full form-text required" type="text" id="edit-field-price-ad-und-0-value" name="field_price_ad[und][0][value]" value="" size="60" maxlength="55" />                     
          <div class="description">Text</div>                 
        </div>             
      </div>         
    </div>    
    <div class="field-type-youtube field-name-field-video-ad field-widget-youtube form-wrapper" id="edit-field-video-ad">      
      <div id="field-video-ad-add-more-wrapper">        
        <div class="form-item form-type-textfield form-item-field-video-ad-und-0-input">             
          <label for="edit-field-video-ad-und-0-input">Video</label>            
          <input type="text" id="edit-field-video-ad-und-0-input" name="field_video_ad[und][0][input]" value="" size="60" maxlength="1024" class="form-text" />          
          <div class="description">Text</div>        
        </div>      
      </div>    
    </div>
...
</form>

I want something like this:

<form ...>
...
    <div id="edit-field-price-ad">                                    
          <label for="edit-field-price-ad-und-0-value">Price</label>                       
          <input class="text-full form-text required" type="text" id="edit-field-price-ad-und-0-value" name="field_price_ad[und][0][value]" value="" size="60" maxlength="55" />                     
          <div class="description">Text</div>        
    </div>    
    <div id="edit-field-video-ad">         
          <label for="edit-field-video-ad-und-0-input">Video</label>            
          <input type="text" id="edit-field-video-ad-und-0-input" name="field_video_ad[und][0][input]" value="" size="60" maxlength="1024" class="form-text" />          
          <div class="description">Text</div>          
    </div>
...
</form>

Using theme_form_element I was able to remove third div (form-item). I cannot figure out how to edit first 2 div wraps.

1

With Display Suite and sub module Display Suite Extras you can remove unnecessary wrappers.

  • Thank you, but I prefer theme_hook or my_custom_module_hook solution, no more modules. This will be just some small snipped I cannot find. – Jasom Dotnet Sep 24 '15 at 8:29
0

Try Fences module - you can override any templates with your own.

  • You are 3th person which suggest me something for override fields output. It means I'm asking wrong :-) I want to override fields input (Drupal forms). – Jasom Dotnet Sep 24 '15 at 8:56
  • 1
    I see. Check out Theming Node Forms and How to Override Forms. But you will probably need to do something similar for each of the fields individually. – David Lukac Sep 24 '15 at 10:05
  • 1
    Yea, as suspected this is probably going to be a pain. I think your best approach will be to use hook_field_widget_form_alter(), and change the wrapping element type from container to item. That might stop the div coming out, assuming item types don't also have a wrapping div. If that doesn't work, you're probably going to need to do some more serious surgery on the render arrays. Keep in mind that there's at least a small possibility some of those field types will rely on the container existing for javascript/form submission reasons – Clive Sep 24 '15 at 10:12
  • 1
    You can see what render arrays are responsible for the markup you're seeing here: api.drupal.org/api/drupal/modules!field!field.form.inc/function/… – Clive Sep 24 '15 at 10:13
  • 1
    If you want really architecturally clean solution, without hacking around Field API (because IMO the solution will be fragile and will keep breaking up), you could generate your own form (e.g. using Backbone), where you can control the markup fully, and submit it to a REST service, which will populate fields (see example). – David Lukac Sep 24 '15 at 10:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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