1

I'm trying to modify the basic search block form that comes with Drupal 7 core. I have a custom theme and inside my templates folder I copied the search-block-form.tpl.php and modified it like so

<form action="/fi" method="post" accept-charset="UTF-8" class="ng-pristine ng-valid form-inline" novalidate role="form">    
<div class="form-group">            
    <div class="input-group">           
        <input 
            title="<?php print t('Type in your search terms'); ?>" 
            type="text"                 
            name="search_block_form" 
            value=""                
            maxlength="128" 
            class="form-text" />
    </div>      
    <?php print $search['hidden']; ?>
</div>  
<button 
    type="submit"               
    name="op"               
    class="btn btn-default">
    <span class="glyphicon glyphicon-search"></span>
</button>

But when I look my rendered markup on page it is totally different.

<form action="/fi" method="post" id="search-block-form" accept-charset="UTF-8" class="ng-pristine ng-valid">
<div>
    <div class="form-group">
        <div class="input-group">           
            <input title="Type in your search terms" type="text" name="search_block_form" value="" maxlength="128" class="form-text">
    </div>      
    <input type="hidden" name="form_build_id" value="">
    <input type="hidden" name="form_id" value="search_block_form">
</div>  
<button type="submit" name="op" class="btn btn-default">
    <span class="glyphicon glyphicon-search"></span>
</button>
</div>
</form>

Why is there an extra div inside the form and where is my form-inline class and who put that id on my form? Why isn't my markup rendered as I wrote it? I also tried to remove all unnecessary preprocessing by overriding xxx_preprocess_search_block_form and xxx_form_alter but nothing seems to work.

And yes I have tried to clear theme cache many times.

This is driving me insane about Drupal. All this "magical" stuff that keeps happening when I'm trying to accomplish even the most simplest templating tasks. Could someone please explain what is going on? Thanks!

3

First of all nothing is "magical" and if you are new to drupal then you have not looked into the Form API or ignored it.

What you are trying to do resides on hooking into the form api using the hook_form_alter or hook_form_FORM_ID_alter functions. The "magical" inline elements on your form are being handled by the form API I spoke of. You are trying to enter form elements and a button into a template file which is not only a security issue but is not the way that drupal handles forms.

You will need to create a custom module and add your hook_form into there and use the form api I spoke of to handle what you are trying to do.

  • Awesome thanks! Have to look into that API. Though I don't really like the idea that I have to create own module to get this done. It kind of conflicts with this guide. – suomiton Dec 8 '14 at 21:10
  • You are correct in saying it seems to conflict but you are adding elements to the form and also wanting to modify the form. That would be handled in those form hooks. What exactly are you trying to do with the form? – chadpeppers Dec 8 '14 at 21:29
  • Basically I'm just trying to take control of the markup rendered by the search form block. Add some bootstrap classes, angular attributes and change the structure of the html but still keeping the form functionality as it is including the security aspects. – suomiton Dec 9 '14 at 15:43
  • Is this for the form element itself or an element that wraps the item? – chadpeppers Dec 9 '14 at 21:21

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.