Since redesigning this site, I've been getting a ton of requests for Drupal theme design. Most of the projects are too small for me to take or require too much work for the amount of money people have budgeted. So to help people who are new to doing theme design with Drupal, I'm going to start a series of tips on getting more out of your Drupal themes. My focus will be on using the PHP Template engine for my examples because it's the best option for flexibility of your theme design. I should note that not all tips will be theme specific, however. This first tip is really about re-using content blocks in pages.
The portal model
Some of you may be familiar with the concept of portlets inside portals like Oracle Portal or some such. Portlets are little windows of content that you insert into a portal page. So you might have a page on a topic, e.g. Human Resources in your portal. But to populate that page, you may take existing content from other sections of your portal. To do this, you select a portlet from your admin menu and tell your portal page to use it. You typically select a bunch of these to build up a page's content.
In Drupal we can do similar things to populate content, but this type of modular approach to individual page building is usually left to content management systems like Mambo/Joomla and the commercial portal CMS's. To stack blocks of content in Drupal, we need to do a little more work and take a few more steps, but it's doable. One approach is to build your content modules within Drupal's block system. And then when you create a new page (has to be a PHP page), you simply insert block calls into your page body.
Here's an example showing how a typical portal page is built and how you might build a Drupal page with blocks in a similar manner:

So you see that what I plan to do is display a block using PHP. To give you an example, let's take a to do list. I'm a fan of the GTD method of time management, so I created a new "To do items" content type using flexinode. Taking that example in the diagram above, I want to insert a block of my latest to do items in a content area on the left, and a list of new links I've tracked in a content area on the right.
Inserting blocks in Drupal pages
To insert the blocks, we use some PHP code in the page body and be sure to select "PHP code" for the Input format. (More on this technique in the Drupal node: Placing the contents of a block in any location.)
PHP code to insert my to do list
For this content area, I created a block that does an SQL search for my to flexinode-5 items. Then I use the module_invoke() function to display the block within the page. Note that "7" is the ID number of the block I created in the admin -> blocks menu.
<?php
$block = module_invoke('block', 'block', 'view', 7);
print $block['content'];
?>
Alternatively I can simply insert the PHP with the SQL statement into the page itself like so:
<?php
$sql = "SELECT node.title, node.nid FROM node WHERE type='flexinode-5' ORDER BY node.created DESC LIMIT 50" ;
$output .= "<ul>";
$result = db_query($sql);
while ($anode = db_fetch_object($result)) {
$output .= "<li>".l($anode->title, "node/$anode->nid")."</li>";
}
$output .= "</ul>";
$output .='<div class="more-link"><a href="to_do_list" class="small">more</a></div>';
print $output;
?>
Displays this:
PHP code to insert latest blog entries
<?php
$block = module_invoke('blog', 'block', 'view', '0');
print $block['content'];
?>
Displays this:
- urlgreyhot goes dormant...
- Konigi Wireframe Icons: Royalty Free EPS and PNG
- ToneMatrix
- Prospect Park may host Woodstock's 40th anniversary
- idaft
- Jorge Colombo | iSketches
- Make a bag
- THRU YOU | Kutiman mixes YouTube
- WebEnabled Offers Software Sales Platform and Marketplace
- Jill Bolte Taylor's Idea Worth Spreading
Wrapping it up
That's it really. You can then use CSS to format your columnar layout. Very simple method to get near portal-like functionality. Might be nice if the "Page" content type used the content management model so we select blocks of content from within the "Create" interface and move the blocks around. That would require an entirely new administration UI, however.If you get comfortable with the method above, this might be a suitable approach for re-using content in pages. This is one method I would definitely try using in client's sites, but your site administrator will have to be familiar with PHP to create these kinds of blocks. Anyone who's wanting this kind of functionality and who is not comfortable touching PHP might be better served by other CMS packages.
Comments
01/13/06 @ 18:29
See also the new-in-CVS Insert Block module, as well as Insert View that works with the new and no-programming-required Views module.
01/13/06 @ 18:37
Cool. Will definitely install these too. As always, thanks for the tip, Boris.
09/02/06 @ 09:27
how about the php code for displaying the latest guestbook entries.
kindly please send it to my provided email.
thanks in advance :)
09/02/06 @ 10:53
Wowie: There is no "guestbook" in Drupal.
11/28/06 @ 11:18
hey guys,
working on the new beta 5 version...
Does anyone know how to define your own regions for phptemplate, so that you are able to have more choices as to where you want specific block to end up on the page?
In 4.6 you created a function that returned an array.
but it seems that this isnt the case anymore...
can anyone enlighten me as to how i would go about defining m own regions for my themes?
Thanks in advance
11/28/06 @ 11:32
wowie :
I think you are assuming that drupal save the entries created with the contact form(s) as nodes....it does not, unfortunatly...
it sends it immeadiatly to the desingated email address
But i imagine a mod/extension/plugin/dooberwackey/thingymebob would be available to do such a task.
Or you could :
# create a page that allows comments, called 'guestbook'
Im not sure how to hide comments on this page only, so this you may have to live with or
# create a page called gest book replies, making its page thusly : admin/guestbook-replies, and force it to appear somwhere in the Administer or Logs menu area, your choice. The text of the page should proly explain that the following table of nose is a list of comments from the questbook page.
# install the views module
# create a view that lists all comment nodes that are related to the guestbook node.
# remember to make the view expose it self as a block
# Then in the blocks admin section, find the 'block representation' of the view and set it to display only on the following path : admin/guestbook-replies within the 'content' region.
visit the admin section of your site, and you should see the menu item created in teh second step, the page should contain the result of the view query created in step 4.
11/28/06 @ 16:17
airtonix: see Boris' comment above regarding Insert Block module.
09/22/07 @ 03:14
The use of SQL should be discouraged, as this should be decoupled from the presentation / business layers. With the database abstraction layer in 5.0 we should as a last resort, use this rather than native mysql
09/22/07 @ 06:55
Yeah. I would use a module to insert views or insert a block. This entry is almost 2 years old now and pertained to 4.7 at the time.
11/04/07 @ 04:43
i'll try it on my drupal site thx
01/20/08 @ 07:35
Great article - thanks, although may be a bit out of date for me
02/21/08 @ 15:25
I used to have this on my old drupal site, but i don't handle it anymore, my wife does :D
04/05/08 @ 00:33
Thank you sir.. I monetized your site for you in appreciation
07/01/08 @ 02:58
Thanks A Lot! Its help me to understand module_invoke function.
07/18/08 @ 10:19
Thanks for the tip, very useful!
07/26/08 @ 12:53
Great tip, thanks for sharing.
04/21/09 @ 12:34
I found out that the module "blockreference" allows adding of blocks to a page/node..
05/04/09 @ 07:57
Thank you very much! You saved my nerves!
05/04/09 @ 07:57
Thank you very much! You saved my nerves!
07/03/09 @ 05:42
Just to thank you a lot for this post!! I've learned a lot!!
Cheers,
Henry
Post new comment