Thanks again for this theme,

Is there any way theme-wise to create an outer element for fieldsets via a php snippet in the template.php? Fieldsets should be accompanied with outer element like ".fieldsets-outer" for creating boxes (rounded or otherwise) around them so that we're not forced to displaying the legend-sticking-up-halfway-above-the-border windows design - which I find most often looks ugly.

thanks,

Thomas

Comments

Anonymous’s picture

Category: feature » support

Don't take this the wrong way, but I find it funny that the Basic issue queue is become a 'Drupal theming support center' :) I don't mind it, but to be honest, this is not a feature request, but a support request, not about Basic, but really about drupal theming in general.

Feel free to ask for any support you need here, and let me also give you some more alternative link that are very useful when you're seeking help:

http://drupal.org/forum
Drupal is a very active community and you're pretty much sure to find a lot of help here.

http://drupal.org/support
The Drupal system is very well documented, and this page is the main starter for core research

http://api.drupal.org/
And this one is the one we are going to use for you. The Api website is a database of all the functions of the Drupal system, from version 4.7 to 7. When you are trying to find out how something is created in the drupal core, like fieldset for example, select the version of drupal you are using, and then, use the search input to find the function that creates the fieldset.

Remember that when some html is outputed, it is usually done using a 'theme' function. A theme function is a function that start with 'theme_' and is used to create the html that will be output when the function is called.

Make sure you read all this before jumping to the snippet!

So we are looking for a theme function that outputs fieldset, so I start to type 'fieldset' in the search of http://api.drupal.org/ and the you'll notice that all the functions that contains the word 'fieldset' are listed instantly while you are typing under the search box. Luckily, only one function uses fieldset, and it's... theme_fieldset !! So you can select this function and click on search, and you'll arrive on a page that show the function, how it works, where it is in drupal, etc... So this function is in the file form.inc in the folder 'includes', at the line 1503. It even shows you the function at the bottom of the page.

This is where this website is really useful, because what you really want to do, is not really to change this function, but OVERRIDE it in your theme, and that's what THEME functions are for, to be overridden in the theme (thus the name!). To do so, you just have to copy the function from http://api.drupal.org/ and paste it in your template.php file of your theme. But in order to make it work, you will have to replace the 'theme_' part of the function by the name of your theme. This is because you can't have two time the same function (with the same name) in your system or the system would be confused and crash. So for example, if your theme is called 'basic', rename the function 'basic_fieldset' instead of 'theme_fieldset'. Then Drupal knows that your theme is overriding the existing theme_fieldset function to replace it with your basic_fieldset one, only if the Basic theme is used of course.

So now you should have a copy of the fieldset function in your template.php, rename with your theme name, but this doesn't change anything because you are overriding the default theme_fieldset function with an exact copy of it. What you can do now, is ALTER the function of your theme in whatever way you want to, like adding div here and there, or whatever else you feel like.

Remember that whatever you change will aply to ALL fieldset, as this function is called everytime a fieldset is created.

So if you want to add a div around the fieldset tags, just find where the fieldset is created in the function, and add div around it, like this (for drupal 5):

<?php
function theme_fieldset($element) {
  if ($element['#collapsible']) {
    drupal_add_js('misc/collapse.js');

    if (!isset($element['#attributes']['class'])) {
      $element['#attributes']['class'] = '';
    }

    $element['#attributes']['class'] .= ' collapsible';
    if ($element['#collapsed']) {
     $element['#attributes']['class'] .= ' collapsed';
    }
  }

  return '<div class="fieldsets-outer"><fieldset' . drupal_attributes($element['#attributes']) .'>' . ($element['#title'] ? '<legend>'. $element['#title'] .'</legend>' : '') . ($element['#description'] ? '<div class="description">'. $element['#description'] .'</div>' : '') . $element['#children'] . $element['#value'] . "</fieldset></div>\n";
}
?>

Of course, notice that I also added the closing div, make sure you remember to close div ! Also, do not copy the php opening and closing tags.

So there you go, this snippet (that I tested) should work for drupal 5. Make sure you use the right version, because functions tend to change from 5 to 6 or 7, and this one might not work for 6. But just use the one for drupal 6 on http://api.drupal.org/ and it should work.

Hope all this make sense. Remember that this process is not only for this function, but for all theme function of drupal. So if you master this, you will gain some great theming power ! (but with great power, comes great responsibilities ;) )

If you'd like to override a SPECIFIC fieldset, (like only the second fieldset of the user edit page), the process is different, it's a form override, and you can read more about it here (great lullabot article) : http://www.lullabot.com/articles/modifying-forms-5-and-6

Cheers !

pribeh’s picture

Wow,

Thanks so much. I have both drupal theme books (5&6) and I didn't really get a good sense of how to override functions via themes very well. If you have a book for sale let me know. This is an amazing tutorial simply about using theme overrides and should be in the drupal handbook under theme fieldsets. Would you mind if I posted it in the drupal handbook?

thomas

Anonymous’s picture

Status: Active » Fixed

Sure, no problem

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.