function theme_block

You are here

4.6 theme.inc theme_block($block)
4.7 theme.inc theme_block($block)
5 theme.inc theme_block($block)
6 theme.php theme_block()

Implemented using the block.tpl.php template.

See also

block.tpl.php

Related topics

1 theme call to theme_block()
theme_blocks in includes/theme.inc
Return a set of blocks available for the current user.

File

developer/theme.php, line 316
Shadow theme functions for theme templates.

Code

function theme_block() {
  // This function is never used; see the corresponding template file instead.
}

Comments

Ok, you most likely want to generate a block using code. How to do it?

You need a module and a delta. For example, the "Powered by Drupal" block's module is system and delta is 0. Another block example is one produced by Views: module is views and delta is generic_name-block_0.

Once you know the module and the delta, it's a simple matter of creating the correct $block object and passing it to the theme() function. The trick is you need to send an object with the right parameters. The code below should get you started. Drupal 7 may be different.

<?php
// setup vars
$block = new stdclass; // empty object
$module = 'system';
$delta = 0; // could also be a string

// renders the "Powered by Drupal" block
// @see hook_block()
// @see module_invoke()

$array = module_invoke($module, 'block', 'view', $delta);

// must be converted to an object
// @see block_list()
if (isset($array) && is_array($array)) {
  foreach (
$array as $k => $v) {
   
$block->$k = $v;
  }
}

$block->module = $module;
$block->delta = $delta;
$block->region = 'whateverYouWant';

echo

theme('block',$block);
?>

Same concept ... streamlined code:

<?php
// setup vars
$module = 'system';
$delta = 0; // could also be a string

// renders the "Powered by Drupal" block
// @see hook_block()
// @see module_invoke()

$block = module_invoke($module, 'block', 'view', $delta);

// must be converted to an object
$block = !empty($block) ? (object)$block : new stdclass;

$block->module = $module;
$block->delta = $delta;
$block->region = 'whateverYouWant';

echo

theme('block',$block);
?>

Thanks, finally a working solution!
It really took some time to find this page.

I've used it like this:

<?php
$block
= (object) module_invoke($module, 'block', 'view', $delta);
$block->module = $module;
$block->delta = $delta;
print
theme('block', $block);
?>

like it :) I've seen that before but you remind me this again, thanks!

This doesn't seem to add the block title/subject to user created blocks and has been driving me nuts.

Any block that's created thru the block GUI interface stores it's content in the boxes table and everything else related to the block in the blocks table.

Running module_invoke('block, 'block', 'view', $delta);

Will never seem to return the block title! It only returns the block content, and the reason seems to be because of the way block.module's implementation of "hook_block" doesn't attempt to return the title.

I'm lost on this one - still trying to figure out how Drupal renders the block titles normally for user created blocks.

// optional argument for title
function print_block_content($module, $block_id, $title = NULL ) {
  $theblock = module_invoke($module, 'block_view', $block_id);
  $title = (isset($title) ? $title : $theblock['subject']);
  $thetitle = $title ? "<h2>{$title}</h2>" : "";
  print $thetitle . render($theblock['content']['#content']);
}

This is what I came up with.
The optional third argument lets you use a different title. I keep this function in template.php and then use it in my templates.

Here's my solution for theming entire block (with subject).

<?php
$delta
= '20';
$block = db_fetch_object(db_query("SELECT bl.delta, bl.module, bl.title AS subject, bo.body AS content FROM {blocks} bl INNER JOIN {boxes} bo ON bo.bid = bl.delta WHERE module = '%s' AND delta = '%s'", array('block', $delta)));
print
theme('block', $block);
?>

Here's what I found while writing my sample essay.

<?php
function theme_username($object) {

  if (

$object->uid && $object->name) {
   
// Shorten the name when it is too long or it will break many tables.
   
if (drupal_strlen($object->name) > 20) {
     
$name = drupal_substr($object->name, 0, 15) .'...';
    }
    else {
     
$name = $object->name;
    }

    if (

user_access('access user profiles')) {
     
$output = l($name, 'user/'. $object->uid, array('title' => t('View user profile.')));
    }
    else {
     
$output = check_plain($name);
    }
  }
  else if (
$object->name) {
   
// Sometimes modules display content composed by people who are
    // not registered members of the site (e.g. mailing list or news
    // aggregator modules). This clause enables modules to display
    // the true author of the content.
   
if ($object->homepage) {
     
$output = l($object->name, $object->homepage);
    }
    else {
     
$output = check_plain($object->name);
    }

   

$output .= ' ('. t('not verified') .')';
  }
  else {
   
$output = variable_get('anonymous', t('Anonymous'));
  }

  return

$output;
}
       
?>