Placing the contents of a block in any location
PLEASE NOTE These snippets are user submitted. Use at your own risk. For users who have setup drupal using an alternate database to the default (MYSQL), please note that the snippets may contain some database queries specific to MYSQL.
If you want the contents of a block to appear within a page you can use the following snippet (Tested with 4.6 and 4.7):
<?php
$block = module_invoke('module_name', 'block', 'view', 0);
print $block['content'];
?>To specify which block from which module, you simply edit the two following variables in the first line:
module_name = The name of the module
The number = Is the $delta of the block
For example to call the block on the Font Size module use:
<?php
$block = module_invoke('fontsize', 'block', 'view', 0);
print $block['content'];
?>Or for example to display who is a new user:
<?php
$block = module_invoke('user', 'block', 'view', 2);
print $block['content'];
?>
Print custom block
And this prints out a custom block created in the admin interface, in this example the first one created:
<?php$block = module_invoke('block', 'block', 'view', 1);
print $block['content'];
?>
Placing blocks outside sidebars
This is a GREAT snippet ... but I ran into a couple of problems when I started using it. Here are my solutions:
Problem: The snippet only prints the contents of a block, and not the block itself as it would normally appear in the sidebar. Primarily, I needed the title of the block to show, and I needed to enclose the block in a div for styling.
Solution: Just as
$block['content']calls the content of the block,$block['subject']calls the title of the block as defined in admin>>blocks>>configure. To style it, I wrapped the whole thing in a set of divs (as done on the sidebars) and wound up with this:<?php$block = module_invoke('block', 'block', 'view', 1);
$output = "<div class=\"front-page-block\">\n";
$output .= "<div id=\"front-block-title\"><h2>".$block['subject']."</h2></div>\n";
$output .= "<div class=\"content\">".$block['content']."</div>\n";
$output .= "</div>\n";
print $output;
?>
Problem: In the sidebars, Drupal automatically hides blocks that don't have content. That doesn't happen when you pull a block elsewhere on the page. Specifically, though there's no content to show, the block title and the div formatting still display. The result is an ugly empty block.
Solution: After a little trial and error, I settled on a simple IF statement to check if
$block['content']was empty:<?php$block = module_invoke('block', 'block', 'view', 1);
if ($block['content']) {
$output = "<div class=\"front-page-block\">\n";
$output .= "<div id=\"front-block-title\"><h2>".$block['subject']."</h2></div>\n";
$output .= "<div class=\"content\">".$block['content']."</div>\n";
$output .= "</div>\n";
print $output;
}
?>
Now my non-sidebar blocks look great, and if there's no content to show, they don't display. Just like the real thing!
Gary
Display the list of the books in a page/story...
Trying to display the list of my books in a page/story, I could not get module_invoke() to work...
After a little research the following works:
<?php
return book_render();
?>
Note, the following also works (no sure the difference):
print book_render();
For newbies (who do not know PHP...yet...), if you want to see how I
got to that result see the following article.
I'm totally extrapolating most of my deductions...so take then for what they
are, guesses:
http://www.sitebuddy.com/drupal/display_block_inside_node
Chris D
Tech. articles with a WAMP+IIS+Drupal focus
Need a break: Travel France
PHP snippet - Tagcloud on any page.
nuther newbie, guys
tagadelic.module 10-03-2006 is in 4.7 beta 6
renamed its block.
works ok in sidebar.
wanted it at a page sticking to top.
tried php snippet stuff (here). no luck, most likely me as usual.
and then from the Bryght Wiki:
<?phpdrupal_set_html_head('<style type="text/css">@import url('.drupal_get_path('module','tagadelic').'/tagadelic.css);</style>');
$vocs[] = 7; // id of the vocabulary of which you want to display a tag cloud
$output = theme('tagadelic_weighted',tagadelic_get_weighted_tags($vocs));
print $output;
?>
changed the id from 7 to 6 and it worked!
BUT... it doesnt display all my tagwords, only around 60, and i have the vocabulary set to show 196.
went into tagadelic.module and changed
function tagadelic_get_weighted_tags($vids, $steps = 6, $size = 60from default size of 60 to 196.
worked.
cheers from the canuk in germany.
d_tect
You could have just changed
You could have just changed your embedded php snippet, rather than modifying tagalicious.module itself...
Change this line:
$output = theme('tagadelic_weighted',tagadelic_get_weighted_tags($vocs));to:
$output = theme('tagadelic_weighted',tagadelic_get_weighted_tags($vocs, 6, 200));That will render a tag cloud with 6 levels of size differences (the default) and 200 tags in the cloud.
Cheers, from the canuck in Canada :-)
- D'Arcy
Add Quotes block to a View
This took a bit of trial and error, or I could have read the manual, but what's the fun in that?
To add a Quotes block to a View (or just about anywhere), create the Quote block you desire and then place this in your View's header or footer:
<?php$block = module_invoke('Quotes', 'block', 'view', 1);
print $block['content'];
?>
<br>
The break at the end gives a little spacing. To see an example of a random quote being displayed on a random front page, visit our sites home page.
Enjoy, and thanks to whoever started this thread, as you saved me a lot of coding,
M.J. Taylor
Publisher
from Reason to Freedom
Wekly libertarian magazine promoting thinking for oneself, thus helping to create a free, benevolent society.
Appearantly $delta is not always a number...
I tried to use this php code to show the feed-content of an aggregator feed on a page. It turned up to work out this way:
<?php$block = module_invoke('aggregator', 'block', 'view', 'feed-1');
print $block['content'];
?>
So $delta here is supposed to be of the form 'feed-x' where x stands for the number of the feed.