Work with Drupal long enough and you'll soon discover the bits of HTML/CSS markup which have been hardcoded into Drupal core and/or it's contributed modules. This markup which is ready to go 'out of the box' is a very large part of what makes it so easy to for people who use Drupal to launch a functional and reasonably appealing looking site, very quickly - without having to become and HTML/CSS gurus themselves. Which is, of course, awesome.
For something gained, something is given, however. In this case a bit of flexibility. That's right - now that you've worked up enough gumption to try and get your Drupal installation to do a backwards somersault with a twist (e.g., highly customized theming of Drupal's output) the same hardcoded markup that made your life easier is likely to make it a bit messier if not just outright harder. It is at this point, typically, that the different kinds of "overrides" are recommended. In this post we'll take a quick look at common override techniques, as well as a less common, but potentially useful and easier, third option:
A CSS override is done by placing an already existing style (e.g., one that existing in say, system.css, a Drupal core stylesheet) inside a stylesheet which is loaded later than the original one, which will cause the more recently loaded stylesheet to take precedence over the pre-existing values of the original stylesheet. The upside of this method is that is often the simplest overriding method, which makes it good for non-PHP comfortable people and/or for those times when a function override would just be overkill (more about those below). The weakness of this method, depending on exactly what you're trying to override and how, is that CSS overrides are inherently limited in their scope of influence. At some point it becomes true that one can only override what is there - they can't add or take away more than that.
Normally done in template.php for most themes, the idea is to create a PHP-based function override for an existing themeable function. For PHP masters or for people who aren't PHP masters but have enough patience, time, and knowledge to get through making a function override - it's perhaps the 'cleanest' and most accepted way to go for anything resembling heavy modifications to the output/styling.
A 'weakness' of both of the above methods is that they can take quite a bit of the previously mentioned time, patience, and knowledge.
...enter a simple PHP function called strip_tags. It works by simply wrapping a given statement with strip_tags. With strip_tags you can selectively zap any and/or all unwanted markup from the face of the earth. No more overriding, no more wrestling those hardcoded bits of markup and hog-tying them with sheer force of will and righteous indignation - they're simply not there anymore. With the offending auto-generated stuff gone you're free to add you own markup however you like and style it appropriately.
So this:
<div class="my-own-class"><?php echo strip_tags($primary_links, '<a>'); ?></div>
...would output $primary_links but without any HTML/CSS markup, except for the 'a href' statement (since we indicated in our PHP statement that we wanted to keep it, right), wrapped inside our class, "my-own-class".
If you've ever done the li-display-inline-float-left-clear-all dance before you probably have a very big smile on your face at this point. Likewise for many things it may be more economical/practical to do a simple strip_tags than to put together an whole themeable function.
Caleb G's blog
Comments
Thanks for this information,
Thanks for this information, that strip tags function is such a tiny piece of code and it did the job for me.
Post new comment