How to modify the html of a block?

shinta - May 10, 2006 - 19:31

I'm completely new to Drupal, so forgive me if this is a newbie question (and yes, I have searched the docs and forums). Is there any easy way to modify the output of a block? For example, to change the length of the user login form text fields, or the layout of the login forms?

In the phptemplate theme files, all I see is a <? echo $sidebar_left ?> which apparently dumps all the sidebar_left contents. How do you modify this string before it gets dumped? Modifying block.tpl won't do anything to help in this case. Is there any way to change the html code for the user login form without going into modules/user.module and digging through the 2100 lines of code there, hoping to find something that looks like it dumps the html for the login form?

Cheers,
Steve

css

theichurch - May 10, 2006 - 20:57

I think some of the modifications you are looking to do can be done with css. Find the fields you are looking to change and then find the classes and ids associated with them. Then, in the themes css file mark up the css so it looks the way you want it to be. For css info check out http://www.w3schools.com/css/

To override it, you'll need

styro - May 10, 2006 - 21:17

To override it, you'll need to understand the Forms API, which might be throwing you in the deep end too soon.

http://api.drupal.org/api/HEAD/function/user_block

Overriding the basic theme functions (eg http://drupal.org/node/11811) probably won't be enough, as there is only theme_block() and no theme_user_block() as far as I'm aware.

But...

The form is just HTML by the time it gets to your browser. I don't think there shouldn't be anything stopping you creating your own static HTML block containing a form using the same HTML. As long as it sends the same HTTP GET/POST that the standard form does, it should work (if a little inelegant from a coding perspective).

--
Anton
New to Drupal? | Forum posting tips | Troubleshooting FAQ
Example Knowledge Base built using Drupal

you might be better off...

theichurch - May 10, 2006 - 21:56

If you want to alter an existing form you want to check out form_alter. An example is here http://drupal.org/node/58154#comment-110182

also, check out.... http://api.drupal.org/api/HEAD/function/hook_form_alter

In the end the best way to change the look is to change the css for an id or class.

Thanks!

shinta - May 10, 2006 - 22:29

Thanks guys for your replies! The user_block function looks like it's exactly what I was looking for (must've missed it when I quickly scanned user.module earlier). CSS might work, but it won't for things like changing the wording of labels, links, etc. or rearranging the layout of certain items within a block (beyond simple "float" or ugly "position" manipulations).

I guess for other modules, it looks like any fine-tuning of the display will require modifying the module files directly and looking for a [module]_block function. I'm someone who's anal about pixel-perfect designs, wording, positioning, etc., so I'll most likely be doing a lot of tweaking in the /modules directory.

Thanks again to everyone who replied!

Cheers,
Steve

Some points

styro - May 10, 2006 - 22:59

I guess for other modules, it looks like any fine-tuning of the display will require modifying the module files directly and looking for a [module]_block function.

I wouldn't recommend that approach. There is usually always some way of customising stuff without altering core files. If you alter a core file, you've effectively 'forked' it, and will have to manually keep it up to date from now on when new Drupal versions (eg 4.7.1 etc) are released.

Look into those links for form_alter... that's if the static HTML approach won't work for you.

I'm someone who's anal about pixel-perfect designs...

Well there's your main problem right there... ;)

--
Anton
New to Drupal? | Forum posting tips | Troubleshooting FAQ
Example Knowledge Base built using Drupal

depending on what verbage

sepeck - May 10, 2006 - 23:38

or words you want to change, you can change them without modifying the modules themselves
HOWTO: Use a customized language set to change Drupal text and terminology

-Steven Peck
---------
Test site, always start with a test site.
Drupal Best Practices Guide -|- Black Mountain

In response to the latest

shinta - May 11, 2006 - 14:17

In response to the latest comments:

styro: I agree about effectively forking core files and the hassle it introduces when upgrading, but I'm used to doing that from past work with mambo and phpnuke. I've looked at hook_form_alter, and it's good for some things, but not for others. And yes, being anal about pixel perfect designs may be my problem, but it's also the only motivation for me to design my own themes rather than using the default or public ones out there. ;)

sepeck: That's a gread idea! Thanks for the link.

Drupal

styro - May 11, 2006 - 21:41

styro: I agree about effectively forking core files and the hassle it introduces when upgrading, but I'm used to doing that from past work with mambo and phpnuke.

Yep. I think you'll really start to appreciate Drupal's API - a lot of effort goes into it easy to override and customise most things. The benefit of this isn't usually apparent to most new users. It is mainly this aspect of Drupal that keeps me here :)

I've looked at hook_form_alter, and it's good for some things, but not for others.

I've used it in a module, but I haven't seen how it can work in themes yet - so I was a bit vague about how to use it.

And yes, being anal about pixel perfect designs may be my problem, but it's also the only motivation for me to design my own themes rather than using the default or public ones out there. ;)

I'm glad you took my comment in the spirit it was intended :)

Anyway I'm off to look at the rest of the info others have posted in this thread....

--
Anton
New to Drupal? | Forum posting tips | Troubleshooting FAQ
Example Knowledge Base built using Drupal

Theming a form in phptemplate style

Heine - May 11, 2006 - 15:04

Theming a form with phptemplate involves several steps

1. identify the form id (eg. user_login)
2. open/create template.php
3. enter a function:

<?php
function phptemplate_user_login($form) {
  return
_phptemplate_callback('user-login', array('form' => $form));
}
?>

(yourthemename_user_login also works)

4. create the file user-login.tpl.php
5. Indulge yourself with HTML and some php while filling user-login.tpl.php.

The minimum to render the form is

<?php
 
print form_render($form);
?>

You can do silly things as well; the following code will put the password box first.

<?php
 
print form_render($form['pass']);
  print
form_render($form);
?>

It's also possible to change the form, although I advise to keep modifications to a minimum and only touch on output. As an example: the following code shrinks the size of the password field to 10 cols.

<?php
$form
[pass]['#size'] = 10
print
form_render($form);
?>

--
When your problem is solved, please post a follow-up to the thread you started.

Thank you!!!

shinta - May 11, 2006 - 16:17

Brilliant! Thanks for laying it all out for me. This is exactly what I needed. With these instructions and the useful links posted previously, I can do what I want without modifying anything in the /modules directory. Whew! :)

Cheers,
Steve

 
 

Drupal is a registered trademark of Dries Buytaert.