In the ancient times when things were easy, you just had to code a form in html and then your php would typically have a if($_REQUEST['some_value']) to handle the request. But now with Drupal, nothing is easy... Here is once more my latest battle to understand how Drupal thinks.

Context: I have a list of news (content type "news") shown on a page with paging system. Each news title links to the news details page. This works fine. There is a new requirement that each news must have one or more "tags". The goal is to show a list of checkboxes on top of the news list to allow the user to filter in/out news with the selected tags.

EDIT: After being defeated by Drupal's form I decided to completely bypass the Form API and generate my own fully flexible form.

There is only one thing remaining, Drupal is forcing some div wrappers around each of my input fields. I want to get rid of them (and stop Drupal to think at my place).

In my_news_list.tpl.php I coded:

<form action="" method="post"> <!-- recall same page on submit -->
foreach($tags as $tag){
    ?><input type="checkbox" name="tags[]" 
        value="<?php echo $tag['id'] ?>"
        <?php if(isset($tag['checked'])){?>checked="checked"<?php }?> 
      /><?php echo $tag['value'] ?><?php


<form action="" method="post">
    <div class=" ui-checkbox"> <!-- Why those div wrappers?! -->
        <input type="checkbox" name="tags[]" value="15" onchange="form.submit();">
    <div class=" ui-checkbox">
        <input type="checkbox" name="tags[]" value="14" onchange="form.submit();">
    <div class=" ui-checkbox">
        <input type="checkbox" name="tags[]" value="18" onchange="form.submit();">
    Investor News
    <div class=" ui-checkbox">
        <input type="checkbox" name="tags[]" value="16" onchange="form.submit();">
    Products and Services   
  • Creat News listing page with views.For taxanomy filter you can use 'Better Exposed Filter' drupal.org/project/better_exposed_filters. – deepa Apr 17 '15 at 7:40
  • The news list page exists and like I said in my post, works perfectly. I don't need views and I don't want to add any other module. – Guillaume Bois Apr 17 '15 at 11:41
  • Those div wrappers are normally generated by the Form API. Are you sure that you are using the template file with your custom code, and not the old template which you tried with the Form API? – ouni Apr 17 '15 at 15:06
  • Yes I am sure. Sometimes when my server is slow and I get a lag, I can see my form printing correctly without the divs. I was fast enough to stop the browser and was able to inspect the code to realize that the divs were not (yet) injected. It seems that Drupal injects the divs after the page load, probably in an ajax call of some kind. What JS plugin/library could do that? – Guillaume Bois Apr 20 '15 at 13:14
  • After typing my previous comment, I started to search for a js that would inject divs and found that I was using jQuery mobile which by default did that. I had to set $.mobile.page.prototype.options.keepNative = "select, input, textarea"; in a mobileinit event to fix the problem. – Guillaume Bois Apr 20 '15 at 13:30

Drupal's Form API is complex but gives you lot of things: is secure, is extensible, is themeable. Indeed is more difficult than coding a simple HTML form, but with a simpel HTML form you have a simple functionality: just that simple HTML form.

Form API generates the form HTML element and handles the POST requests, and validate and submithandlers, you don't and you shouldn't code your own HTML form.

I recommend you to read the Drupal 6 Form API Quickstart Guide. Drupal 7 form handling is pretty the same, but check the Drupal 7 Form API Reference and Form API Internal Workflow Illustration.

In the other hand you may prefer to use a module that address what you need without coding, that's a faceted search: Facet API. From its documentation page:

Faceted search allows our search results to be filtered by defined criteria like category, date, author, location, or anything else that can come out of a field. We call these criteria facets. With facets, you can narrow down your search more and more until you get to the desired results.

  • I don't want a lot of things. I don't need secure, extensible nor themable. I just need simple functionality. Drupal form API should be flexible enough to allow trully simple forms! Or how can I bypass Form API? Remember KISS (Keep It Simple Stupid!) – Guillaume Bois Apr 17 '15 at 12:29
  • Read the documentation and you'll discover a simple Form is simple with Form API. A faceted form is not a trully simple form, though. – sanzante Apr 17 '15 at 12:57
  • The documentation given is for Drupal 6. I'm on Drupal 7. Things are complicated enough, I don't want to extrapolate for Drupal 7. All Drupal 7 form examples starts with a hook_menu function which created a url to call the form. I don't want a new page, I already have a working page generated from a .tpl.php file. I just want to add a few checkboxes to this existing page. ATM, in my code, I completely bypassed Drupal form functions and still get div wrapper around my input field! Its driving me nuts! – Guillaume Bois Apr 17 '15 at 13:17
  • I know you are in D7, that's why I added "Drupal 7 form handling is pretty the same". IMHO, you are doing it wrong so I can't help you that way. – sanzante Apr 17 '15 at 13:23
  • So you say there is no way to remove the div wrappers around my input fields? Maybe that's impossible in Drupal? – Guillaume Bois Apr 17 '15 at 13:34

The issue here is theme_wrappers around your form elements.

// @see: https://drupal.stackexchange.com/a/193587/58635

  • Yes, the code in the OP is totally not the way to go about it. – Kevin Mar 4 '16 at 18:05

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.