The 3 Most Common Drupal Theming Questions and Answers

12 Aug · by Tim Kamanin · 3 min read

Drupal themeing is very easy and logical, but there are still a lot of questions at drupal.org and stackoverflow.com and other resources about how to do this and how to output that. Today, I will review and answer three most common Drupal Theming Questions.

Here are the most common Drupal Theming Questions:

  1. How to output comments anywhere within a Drupal theme?
  2. How to get rid of (remove) 'Search this site' label from a search box
  3. How to output a view anywhere within a Drupal theme?

1. How to output comments anywhere within a Drupal theme?

This one was my first question when I was developing my first Drupal website. I wanted to put Drupal comments in a separate tab, but comments in Drupal are being outputted automatically in the bottom, so you can't change the place of its output until you use this code:

if (function_exists('comment_render') && $node->comment) {
  echo comment_render($node, $node->cid);
  $node->comment = NULL; 
}

A recommended place for putting this snippet is of course node template file which is node.tpl.php or node-type.tpl.php, but actually, you can put this snippet into any .php file and output comments for a passed through node. Just make sure that you have a proper $node loaded.

2. How to get rid of (remove) 'Search this site' label from a search box

In Drupal 6 default themes, we have annoying label above search-box which says: „ Search this site". It is absolutely useless label and I can only guess why did they include this one in Drupal 6, but we can easily get rid of it just following these steps:

a) To remove label from theme's main search form (which is called by _$search_box_ ):

In your selected theme's folder, edit or create (if it doesn't exist) file named: _search-theme-form.tpl.php_ and add the following code to it:

$search["search_theme_form"] = 
str_replace(t('Search this site:'), '', $search["search_theme_form"]); 
print $search["search_theme_form"];
print $search["submit"];
print $search["hidden"];

b) To remove label from _block 's search form_ (which is called in a search box block):

In your selected theme's folder, edit or create (if it doesn't exist) file named: _search-block-form.tpl.php_ and add the following code to it:

$search["search_block_form"] = str_replace(t('Search this site:'), '', $search["search_block_form"]);
print $search["search_block_form"];
print $search["submit"];
print $search["hidden"]; 
if (isset($search['extra_field'])) {
  print $search['extra_field'];
}

If nothing changed - flush theme cache after implementing these changes.

3) How to output a view anywhere within a Drupal theme?

Views are very popular nowadays in the Drupal world and nearly no site can be done without implementing Views more or less. One of the most common questions connected to views is how to output them on any desired Drupal page in any desired place? SIMPLE, just put this snippet where you want the View to output:

print views_embed_view('viewname', 'default', $arg);

Viewname can be easilly seen on a Views list page, just copy it there and paste instead of _' viewname'_, _' default'_ is a default mode of the _' viewname'_ view. _$arg_ can be empty or you can pass there _' viewname'_ arguments (depends on what arguments your View uses).

So here are three most common Drupal themeing questions and answers. If you have any more questions or answers about Drupal themeing, feel free to ask here in comments, I will answer and will try to compile them in a new Drupal Themeing Q&A post.

Comments

Required for comment verification



ashdrupalnew

how to add custom block and available block in node-type.tpl.php in drupal 6? Thanks in advance.

Reply · 7 years, 4 months ago
Ferran Gil

I've been looking to put the comments anywhere on my node-type.tpl.php and I finally found it, but there's still something wrong. Now, at the end of the page, the comments are being printed AGAIN. They are being called automatically and this is very annoying! Any help? Thanks!

Reply · 7 years, 4 months ago
Tim Wooten

You can also remove the search label with a quick hook_form_alter from within your own custom site module like so. function YOUR-MODULE_form_alter(&$form, $form_state, $form_id) { // Remove 'Search' label above search form if ($form_id == 'search_block_form') { $form['search_block_form']['#title'] = t(''); } }

Reply · 7 years, 4 months ago