Commit 98063726 authored by Dries's avatar Dries

- Block and theme improvements. Patch by Ax.

   + block_list() (in block.module): returns an array of block objects for
     $region.
   + theme_blocks() (in theme.inc): uses block_list() and theme("block") to
     actually render them.

  Advantages:
   + Decouples blocks content and layout, allows block_list() to be used
     for non-output purposes (think "pull").
   + Unifies naming in theme.inc: render_blocks()) didn't really fit there.
   + Puts block_list() in blocks.module where it logically belongs.

- Removed some cruft from the Xtemplate theme.  Patch by Ax.
parent 3198d786
...@@ -37,7 +37,7 @@ function theme_header($title = "") { ...@@ -37,7 +37,7 @@ function theme_header($title = "") {
$output .= "<table border=\"0\" cellspacing=\"4\" cellpadding=\"4\"><tr><td style=\"vertical-align: top; width: 170px;\">"; $output .= "<table border=\"0\" cellspacing=\"4\" cellpadding=\"4\"><tr><td style=\"vertical-align: top; width: 170px;\">";
$output .= theme("box", t("Navigation"), @implode("<br />", link_page())); $output .= theme("box", t("Navigation"), @implode("<br />", link_page()));
$output .= render_blocks("all"); $output .= theme("blocks", "all");
$output .= "</td><td style=\"vertical-align: top;\">"; $output .= "</td><td style=\"vertical-align: top;\">";
return $output; return $output;
...@@ -358,24 +358,19 @@ function theme_onload_attribute($theme_onloads = array()) { ...@@ -358,24 +358,19 @@ function theme_onload_attribute($theme_onloads = array()) {
} }
/** /**
Render blocks available for (global) $user and $region calling $theme->block($block). * Returns themed blocks available for current $user at $region.
*
@param $region main|left|right * @param $region main|left|right
*
@return a string containing the \a blocks output. * @return a string containing the \a blocks output.
**/ */
function render_blocks($region) { function theme_blocks($region) {
global $user;
$result = db_query("SELECT * FROM {blocks} WHERE (status = '1' OR custom = '1') ". ($region != "all" ? "AND region = %d " : "") ."ORDER BY weight, module", $region == "left" ? 0 : 1);
$output = ""; $output = "";
while ($result && ($block = db_fetch_array($result))) {
if ((($block['status'] && (!$user->uid || !$block['custom'])) || ($block['custom'] && $user->block[$block['module']][$block['delta']])) && (!$block['path'] || preg_match($block['path'], str_replace("?q=", "", request_uri())))) { if ($list = module_invoke('block', 'list', $region)) {
$block = array_merge($block, module_invoke($block['module'], 'block', 'view', $block['delta'])); foreach ($list as $key => $block) {
if ($block['content']) { // $key == <i>module</i>_<i>delta</i>
$output .= theme('block', (object)$block); $output .= theme('block', $block);
}
} }
} }
return $output; return $output;
......
...@@ -346,4 +346,37 @@ function block_user($type, &$edit, &$user) { ...@@ -346,4 +346,37 @@ function block_user($type, &$edit, &$user) {
} }
} }
/**
* Return blocks available for current $user at $region.
*
* @param $region main|left|right
*
* @return array of block objects, indexed with <i>module</i>_<i>delta</i>
*
* @see <a href="http://drupal.org/node/view/1042" target="_top">[feature]
* Generic template design difficult w/o block region "look-ahead"</a>
* @todo add a proper primary key (bid) to the blocks table so we don't have
* to mess around with this <i>module</i>_<i>delta</i> construct. currently,
* "blocks" has no primary key defined (bad)!
*/
function block_list($region) {
global $user;
static $blocks = array();
if (!isset($blocks[$region])) {
$blocks[$region] = array();
$result = db_query("SELECT * FROM {blocks} WHERE (status = '1' OR custom = '1') ". ($region != "all" ? "AND region = %d " : "") ."ORDER BY weight, module", $region == "left" ? 0 : 1);
while ($result && ($block = db_fetch_array($result))) {
if ((($block['status'] && (!$user->uid || !$block['custom'])) || ($block['custom'] && $user->block[$block['module']][$block['delta']])) && (!$block['path'] || preg_match($block['path'], str_replace("?q=", "", request_uri())))) {
$block = array_merge($block, module_invoke($block['module'], 'block', 'view', $block['delta']));
if ($block['content']) {
$blocks[$region]["$block[module]_$block[delta]"] = (object) $block;
}
}
}
}
return $blocks[$region];
}
?> ?>
...@@ -346,4 +346,37 @@ function block_user($type, &$edit, &$user) { ...@@ -346,4 +346,37 @@ function block_user($type, &$edit, &$user) {
} }
} }
/**
* Return blocks available for current $user at $region.
*
* @param $region main|left|right
*
* @return array of block objects, indexed with <i>module</i>_<i>delta</i>
*
* @see <a href="http://drupal.org/node/view/1042" target="_top">[feature]
* Generic template design difficult w/o block region "look-ahead"</a>
* @todo add a proper primary key (bid) to the blocks table so we don't have
* to mess around with this <i>module</i>_<i>delta</i> construct. currently,
* "blocks" has no primary key defined (bad)!
*/
function block_list($region) {
global $user;
static $blocks = array();
if (!isset($blocks[$region])) {
$blocks[$region] = array();
$result = db_query("SELECT * FROM {blocks} WHERE (status = '1' OR custom = '1') ". ($region != "all" ? "AND region = %d " : "") ."ORDER BY weight, module", $region == "left" ? 0 : 1);
while ($result && ($block = db_fetch_array($result))) {
if ((($block['status'] && (!$user->uid || !$block['custom'])) || ($block['custom'] && $user->block[$block['module']][$block['delta']])) && (!$block['path'] || preg_match($block['path'], str_replace("?q=", "", request_uri())))) {
$block = array_merge($block, module_invoke($block['module'], 'block', 'view', $block['delta']));
if ($block['content']) {
$blocks[$region]["$block[module]_$block[delta]"] = (object) $block;
}
}
}
}
return $blocks[$region];
}
?> ?>
...@@ -47,7 +47,7 @@ function marvin_header($title = "") { ...@@ -47,7 +47,7 @@ function marvin_header($title = "") {
$output .= "<table border=\"0\" style=\"width: 100%\" cellpadding=\"8\" cellspacing=\"0\">\n"; $output .= "<table border=\"0\" style=\"width: 100%\" cellpadding=\"8\" cellspacing=\"0\">\n";
$output .= " <tr>\n"; $output .= " <tr>\n";
$blocks = render_blocks("left"); $blocks = theme("blocks", "left");
if ($blocks) { if ($blocks) {
$output .= " <td style=\"width: 200px; vertical-align: top;\">\n"; $output .= " <td style=\"width: 200px; vertical-align: top;\">\n";
$output .= $blocks; $output .= $blocks;
...@@ -171,7 +171,7 @@ function marvin_links($links, $delimiter = " &middot; ") { ...@@ -171,7 +171,7 @@ function marvin_links($links, $delimiter = " &middot; ") {
function marvin_footer() { function marvin_footer() {
$output = " </td>\n"; $output = " </td>\n";
$blocks = render_blocks("right"); $blocks = theme("blocks", "right");
if ($blocks) { if ($blocks) {
$output .= " <td style=\"width: 200px; vertical-align: top;\">\n"; $output .= " <td style=\"width: 200px; vertical-align: top;\">\n";
$output .= $blocks; $output .= $blocks;
......
...@@ -274,7 +274,7 @@ function unconed_footer() { ...@@ -274,7 +274,7 @@ function unconed_footer() {
</td> </td>
<td valign="top" width="20%"> <td valign="top" width="20%">
<?php <?php
print render_blocks("all", $this); print theme("blocks", "all");
?> ?>
</td> </td>
</tr> </tr>
......
...@@ -5,7 +5,6 @@ ...@@ -5,7 +5,6 @@
include_once("themes/xtemplate/xtemplate.inc"); include_once("themes/xtemplate/xtemplate.inc");
} }
$GLOBALS["xtemplate"]->sidebar = variable_get("xtemplate_sidebar", "right");
$GLOBALS["xtemplate"]->template = new XTemplate("themes/xtemplate/xtemplate.xtmpl"); $GLOBALS["xtemplate"]->template = new XTemplate("themes/xtemplate/xtemplate.xtmpl");
$GLOBALS["xtemplate"]->template->SetNullBlock(" "); // "" doesnt work! $GLOBALS["xtemplate"]->template->SetNullBlock(" "); // "" doesnt work!
...@@ -113,17 +112,7 @@ function xtemplate_header($title = "") { ...@@ -113,17 +112,7 @@ function xtemplate_header($title = "") {
$xtemplate->template->parse("header.message"); $xtemplate->template->parse("header.message");
} }
/* if ($blocks = theme("blocks", "left")) {
if ($xtemplate->sidebar == "left") {
$blocks = render_blocks("all");
}
else if ($xtemplate->sidebar == "both") {
$blocks = render_blocks("left");
}
*/
$blocks = render_blocks("left");
if ($blocks) {
$xtemplate->template->assign("blocks", $blocks); $xtemplate->template->assign("blocks", $blocks);
$xtemplate->template->parse("header.blocks"); $xtemplate->template->parse("header.blocks");
} }
...@@ -162,17 +151,7 @@ function xtemplate_box($title, $content, $region = "main") { ...@@ -162,17 +151,7 @@ function xtemplate_box($title, $content, $region = "main") {
function xtemplate_footer() { function xtemplate_footer() {
global $xtemplate; global $xtemplate;
/* if ($blocks = theme("blocks", "right")) {
if ($xtemplate->sidebar == "right") {
print render_blocks("all");
}
else if ($xtemplate->sidebar == "both") {
print render_blocks("right");
}
*/
$blocks = render_blocks("right");
if ($blocks) {
$xtemplate->template->assign("blocks", $blocks); $xtemplate->template->assign("blocks", $blocks);
$xtemplate->template->parse("footer.blocks"); $xtemplate->template->parse("footer.blocks");
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment