theme.inc 9.52 KB
Newer Older
Dries's avatar
 
Dries committed
1
<?php
2
/**
Dries's avatar
 
Dries committed
3
  Theme System - controls the output of Drupal.
4

Dries's avatar
 
Dries committed
5 6
  The theme system allows for nearly all output of the Drupal system to be
  customized by user themes.
Dries's avatar
 
Dries committed
7

Dries's avatar
 
Dries committed
8
  @package theme_system
Dries's avatar
 
Dries committed
9

Dries's avatar
 
Dries committed
10 11 12
  @defgroup theme_system
  @{
**/
Dries's avatar
 
Dries committed
13

Dries's avatar
 
Dries committed
14
/* $Id$ */
Dries's avatar
 
Dries committed
15

Dries's avatar
 
Dries committed
16 17
/**
  Returns the theme header.
Dries's avatar
 
Dries committed
18

Dries's avatar
 
Dries committed
19
  @param $title (optional) override the page title.
Dries's avatar
 
Dries committed
20

Kjartan's avatar
Kjartan committed
21
  @return a string containing the \a header output.
Dries's avatar
 
Dries committed
22
**/
Dries's avatar
 
Dries committed
23 24
function theme_header($title = "") {
  global $base_url;
Dries's avatar
 
Dries committed
25

Dries's avatar
 
Dries committed
26 27
  $output = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n";
  $output .= "<html xmlns=\"http://www.w3.org/1999/xhtml\">";
Dries's avatar
 
Dries committed
28 29
  $output .= "<head>";
  $output .= " <title>". $title ? $title : variable_get(site_name, "drupal") ."</title>";
Dries's avatar
 
Dries committed
30
  $output .= theme_head($main);
Dries's avatar
 
Dries committed
31 32 33 34 35 36
  $output .= " <style type=\"text/css\" media=\"all\">";
  $output .= "  @import url(misc/drupal.css);";
  $output .= " </style>";

  $output .= " </head>";
  $output .= " <body style=\"background-color: #fff; color: #000;\"". theme("onload_attribute"). "\">";
Dries's avatar
 
Dries committed
37 38
  $output .= "<table border=\"0\" cellspacing=\"4\" cellpadding=\"4\"><tr><td style=\"vertical-align: top; width: 170px;\">";

Dries's avatar
 
Dries committed
39
  $output .= theme("box", t("Navigation"), @implode("<br />", link_page()));
40
  $output .= theme("blocks", "all");
Dries's avatar
 
Dries committed
41 42
  $output .= "</td><td style=\"vertical-align: top;\">";

Dries's avatar
 
Dries committed
43 44 45 46 47 48
  $output .= theme("breadcrumb", drupal_get_breadcrumb());
  $output .= "<h1>" . drupal_get_title() . "</h1>";
  if ($help = menu_get_active_help()) {
    $output .= "<small>$help</small><hr />";
  }

Dries's avatar
 
Dries committed
49
  return $output;
Dries's avatar
 
Dries committed
50
}
Dries's avatar
 
Dries committed
51

Dries's avatar
 
Dries committed
52 53 54 55
/**
  Returns themed set of links.

  @param $links an array of \a links to be themed.
Kjartan's avatar
Kjartan committed
56
  @param $delimiter (optional) \a delimiter used to separate the links.
Dries's avatar
 
Dries committed
57

Kjartan's avatar
Kjartan committed
58
  @return a string containing the \a links output.
Dries's avatar
 
Dries committed
59
**/
Dries's avatar
 
Dries committed
60 61 62
function theme_links($links, $delimiter = " | ") {
  return implode($delimiter, $links);
}
Dries's avatar
 
Dries committed
63

Dries's avatar
 
Dries committed
64 65 66 67 68
/**
  Returns themed image.

  @param $name the \a name of the image file.

Kjartan's avatar
Kjartan committed
69
  @return a string containing the \a image output.
Dries's avatar
 
Dries committed
70
**/
Dries's avatar
 
Dries committed
71 72 73
function theme_image($name) {
  return "misc/$name";
}
Dries's avatar
 
Dries committed
74

Dries's avatar
 
Dries committed
75 76 77 78 79
/**
  Returns a themed breadcrumb menu.

  @param $breadcrumb an array containing the breadcrumb links.

Kjartan's avatar
Kjartan committed
80
  @return a string containing the \a breadcrumb output.
Dries's avatar
 
Dries committed
81
**/
Dries's avatar
 
Dries committed
82
function theme_breadcrumb($breadcrumb) {
Dries's avatar
 
Dries committed
83
  return "<div class=\"breadcrumb\">". implode($breadcrumb, " &raquo; ") ."</div>";
Dries's avatar
 
Dries committed
84
}
Dries's avatar
 
Dries committed
85

Dries's avatar
 
Dries committed
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
/**
  Returns themed node.

  The passed $node object provides a all relevant information for displaying a node:
  \li \c $node->nid
  \li \c $node->type i.e. story, blog, forum.
  \li \c $node->title
  \li \c $node->created a unix timestamp.
  \li \c $node->teaser
  \li \c $node->body
  \li \c $node->changed a unix timestamp.
  \li \c $node->uid the id of the poster.
  \li \c $node->username the username of the poster.

  @param $node the \a node to be themed.
  @param $main

Kjartan's avatar
Kjartan committed
103
  @return a string containing the \a node output.
Dries's avatar
 
Dries committed
104
**/
Dries's avatar
 
Dries committed
105 106 107
function theme_node($node, $main) {
  if (module_exist("taxonomy")) {
    $terms = taxonomy_link("taxonomy terms", $node);
Dries's avatar
 
Dries committed
108 109
  }

Dries's avatar
 
Dries committed
110 111 112
  $output = "<h2>$node->title</h2> by ". format_name($node);

  if (count($terms)) {
Dries's avatar
 
Dries committed
113
    $output .= " <small>(". print theme("links", $terms) .")</small><br />";
Dries's avatar
 
Dries committed
114 115
  }

Dries's avatar
 
Dries committed
116 117 118 119 120
  if ($main && $node->teaser) {
    $output .= $node->teaser;
  }
  else {
    $output .= $node->body;
Dries's avatar
 
Dries committed
121 122
  }

Dries's avatar
 
Dries committed
123
  if ($links = link_node($node, $main)) {
Dries's avatar
 
Dries committed
124
    $output .= "<br />[ ". print theme("links", $links) ." ]";
Dries's avatar
 
Dries committed
125
  }
Dries's avatar
 
Dries committed
126 127
  $output .= "<hr />";

Dries's avatar
 
Dries committed
128
  return $output;
Dries's avatar
 
Dries committed
129 130
}

Dries's avatar
 
Dries committed
131 132 133 134 135 136 137 138 139 140 141
/**
  Returns themed table.

  @param $title the form element's title
  @param $value the form element's data
  @param $description the form element's description or explanation

  @return a string contraining the \a node output.
**/

function theme_form_element($title, $value, $description = 0) {
142
  return "<div class=\"form-item\">". ($title ? "<label>$title:</label><br />" : "") . $value . ($description ? "<div class=\"description\">$description</div>" : "") ."</div>\n";
Dries's avatar
 
Dries committed
143
}
Dries's avatar
 
Dries committed
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197

/**
  Returns themed table.

  @param $header
  @param $rows

  @return a string contraining the \a node output.
**/

function theme_table($header, $rows) {

  $output = "<table>\n";

  /*
  ** Emit the table header:
  */

  if (is_array($header)) {
    $output .= " <tr>";
    foreach ($header as $cell) {
      if (is_array($cell) && $cell["field"]) {
        $cell = tablesort($cell, $header);
      }
      $output .= _theme_table_cell($cell, 1);
    }
    $output .= " </tr>\n";
  }

  /*
  ** Emit the table rows:
  */

  if (is_array($rows)) {
    foreach ($rows as $number => $row) {
      if ($number % 2 == 1) {
        $output .= " <tr class=\"light\">";
      }
      else {
        $output .= " <tr class=\"dark\">";
      }

      foreach ($row as $cell) {
        $output .= _theme_table_cell($cell, 0);
      }
      $output .= " </tr>\n";
    }
  }

  $output .= "</table>\n";

  return $output;
}

Dries's avatar
 
Dries committed
198 199 200 201 202
/**
  Returns themed box.

  @param $subject the \a subject of the box.
  @param $content the \a content of the box.
Kjartan's avatar
Kjartan committed
203
  @param $region the \a region of the box.
Dries's avatar
 
Dries committed
204

Kjartan's avatar
Kjartan committed
205
  @return a string containing the \a box output.
Dries's avatar
 
Dries committed
206
**/
Dries's avatar
 
Dries committed
207 208
function theme_box($subject, $content, $region = "main") {
  $output = "<h2>$subject</h2><p>$content</p>";
Dries's avatar
 
Dries committed
209
  return $output;
Dries's avatar
 
Dries committed
210 211 212
}

/**
Dries's avatar
 
Dries committed
213 214 215 216 217 218 219 220 221 222
  Returns a themed block.

  You can style your blocks by defining .block (all blocks),
  .block-<i>module</i> (all blocks of module <i>module</i>),
  and \#block-<i>module</i>-<i>delta</i> (specific block of
  module <i>module</i> with delta <i>delta</i>) in your
  theme's CSS.

  @param $block object "indexed with" fields from database table 'blocks' ($block->module, $block->delta, $block->region, ...) and fields returned by <i>module</i>_block("view") ($block->subject, $block->content, ...).

Kjartan's avatar
Kjartan committed
223
  @return a string containing the \a box output.
Dries's avatar
 
Dries committed
224
**/
Dries's avatar
 
Dries committed
225 226
function theme_block($block) {
  $output  = "<div class=\"block block-$block->module\" id=\"block-$block->module-$block->delta\">";
Dries's avatar
 
Dries committed
227
  $output .= " <h2>$block->subject</h2>";
Dries's avatar
 
Dries committed
228 229
  $output .= " <div class=\"content\">$block->content</div>";
  $output .= "</div>";
Dries's avatar
 
Dries committed
230
  return $output;
Dries's avatar
 
Dries committed
231
}
Dries's avatar
 
Dries committed
232

Dries's avatar
 
Dries committed
233 234 235
/**
  Returns themed page footer.

Kjartan's avatar
Kjartan committed
236
  @return a string containing the \a footer output.
Dries's avatar
 
Dries committed
237
**/
Dries's avatar
 
Dries committed
238 239
function theme_footer() {
  $output = "</td></tr></table>";
Dries's avatar
 
Dries committed
240
  $output .= theme_closure();
Dries's avatar
 
Dries committed
241
  $output .= "</body></html>";
Dries's avatar
 
Dries committed
242
  return $output;
Dries's avatar
 
Dries committed
243
}
Kjartan's avatar
Changes  
Kjartan committed
244

Dries's avatar
 
Dries committed
245
/**
Dries's avatar
 
Dries committed
246 247
  Returns themed marker, useful for marking new comments or required form elements.

Kjartan's avatar
Kjartan committed
248
  @return a string containing the \a mark output.
Dries's avatar
 
Dries committed
249
**/
Dries's avatar
 
Dries committed
250
function theme_mark() {
Dries's avatar
 
Dries committed
251
  return "<span class=\"marker\">*</span>";
Dries's avatar
 
Dries committed
252 253
}

Dries's avatar
 
Dries committed
254
/**
Dries's avatar
 
Dries committed
255 256 257 258 259
  Returns themed list of items.

  @param $items (optional) an array of the items to be displayed in a list.
  @param $title (optional) the title of the list.

Kjartan's avatar
Kjartan committed
260
  @return a string containing the \a list output.
Dries's avatar
 
Dries committed
261
**/
Dries's avatar
 
Dries committed
262
function theme_item_list($items = array(), $title = NULL) {
Dries's avatar
 
Dries committed
263
  $output .= "<div class=\"item-list\">";
Dries's avatar
 
Dries committed
264
  if (isset($title)) {
Dries's avatar
 
Dries committed
265
    $output .= "<h3>$title</h3>";
Dries's avatar
 
Dries committed
266 267
  }

Dries's avatar
 
Dries committed
268
  if (isset($items)) {
Dries's avatar
 
Dries committed
269
    $output .= "<ul>";
Dries's avatar
 
Dries committed
270
    foreach ($items as $item) {
Dries's avatar
 
Dries committed
271
      $output .= "<li>$item</li>";
Dries's avatar
 
Dries committed
272
    }
Dries's avatar
 
Dries committed
273
    $output .= "</ul>";
Dries's avatar
 
Dries committed
274
  }
Dries's avatar
 
Dries committed
275
  $output .= "</div>";
Dries's avatar
 
Dries committed
276 277 278
  return $output;
}

Dries's avatar
 
Dries committed
279
/**
Dries's avatar
 
Dries committed
280 281 282 283
  Returns themed error message.

  @param $message the error message to be themed.

Kjartan's avatar
Kjartan committed
284
  @return a string containing the \a error output.
Dries's avatar
 
Dries committed
285
**/
Dries's avatar
 
Dries committed
286
function theme_error($message) {
Dries's avatar
 
Dries committed
287
  return "<div class=\"error\">$message</div>";
Dries's avatar
 
Dries committed
288 289
}

Dries's avatar
 
Dries committed
290 291
/**
  Execute hook _head which is run at the start of the page, and output should be in the head tags.
Dries's avatar
 
Dries committed
292

Dries's avatar
 
Dries committed
293
  @param $main (optional)
294

Kjartan's avatar
Kjartan committed
295
  @return a string containing the \a error output.
Dries's avatar
 
Dries committed
296
**/
Dries's avatar
 
Dries committed
297
function theme_head($main = 0) {
Dries's avatar
 
Dries committed
298
  global $base_url;
Dries's avatar
 
Dries committed
299
  $output .= "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />";
Dries's avatar
 
Dries committed
300 301 302 303
  $output .= "<base href=\"$base_url/\" />\n";
  $output .= "<style type=\"text/css\">\n";
  $output .= "@import url(misc/drupal.css);\n";
  $output .= "</style>\n";
Dries's avatar
 
Dries committed
304
  $head = module_invoke_all("head", $main);
Dries's avatar
 
Dries committed
305 306
  $output .= implode($head, "\n");
  return $output;
Dries's avatar
 
Dries committed
307 308
}

Dries's avatar
 
Dries committed
309
/**
Dries's avatar
 
Dries committed
310
  Execute hook _footer() which is run at the end of the page right
311
  before the \</body> tag.
Dries's avatar
 
Dries committed
312 313 314

  @param $main (optional)

Kjartan's avatar
Kjartan committed
315
  @return a string containing the \a closure output.
Dries's avatar
 
Dries committed
316
**/
Dries's avatar
 
Dries committed
317
function theme_closure($main = 0) {
Dries's avatar
 
Dries committed
318 319 320 321
  $footer = module_invoke_all("footer", $main);
  return implode($footer, "\n");
}

Dries's avatar
 
Dries committed
322 323 324
/**
  Call _onload hook in all modules to enable modules to insert javascript
  that will get run once the page has been loaded by the browser.
325

Dries's avatar
 
Dries committed
326
  @param $theme_onloads (optional) addition onload directives.
Dries's avatar
 
Dries committed
327

Kjartan's avatar
Kjartan committed
328
  @return a string containing the \a onload output.
Dries's avatar
 
Dries committed
329 330 331 332
**/
function theme_onload_attribute($theme_onloads = array()) {
  if (!is_array($theme_onloads)) {
    $theme_onloads = array($theme_onloads);
Dries's avatar
 
Dries committed
333
  }
Dries's avatar
 
Dries committed
334 335 336 337 338
  // Merge theme onloads (javascript rollovers, image preloads, etc.)
  // with module onloads (htmlarea, etc.)
  $onloads = array_merge(module_invoke_all("onload"), $theme_onloads);
  if (count($onloads)) {
    return " onload=\"" . implode("; ", $onloads) . "\"";
Dries's avatar
 
Dries committed
339
  }
Dries's avatar
 
Dries committed
340
  return "";
Dries's avatar
 
Dries committed
341 342
}

Dries's avatar
 
Dries committed
343
/**
344 345 346 347 348 349 350
 * Returns themed blocks available for current $user at $region.
 *
 * @param       $region   main|left|right
 *
 * @return      a string containing the \a blocks output.
 */
function theme_blocks($region) {
Dries's avatar
 
Dries committed
351
  $output = "";
352 353 354 355 356

  if ($list = module_invoke('block', 'list', $region)) {
    foreach ($list as $key => $block) {
      // $key == <i>module</i>_<i>delta</i>
      $output .= theme('block', $block);
Dries's avatar
 
Dries committed
357 358
    }
  }
Dries's avatar
 
Dries committed
359
  return $output;
Dries's avatar
 
Dries committed
360
}
361
/**  @} End of defgroup themeable **/
362

363 364 365 366 367 368
function _theme_table_cell($cell, $header = 0) {
  if (is_array($cell)) {
    $data = $cell["data"];
    foreach ($cell as $key => $value) {
      if ($key != "data")  {
        $attributes .= " $key=\"$value\"";
Dries's avatar
 
Dries committed
369 370
      }
    }
Dries's avatar
 
Dries committed
371
  }
372 373
  else {
    $data = $cell;
Dries's avatar
 
Dries committed
374
  }
Dries's avatar
 
Dries committed
375

376 377
  if ($header) {
    $output = "<th$attributes>$data</th>";
Dries's avatar
 
Dries committed
378
  }
379 380
  else {
    $output = "<td$attributes>$data</td>";
Dries's avatar
 
Dries committed
381 382
  }

383 384
  return $output;
}
Dries's avatar
 
Dries committed
385
?>