Drupal block FAQ - How do I create a custom Drupal PHP block, i.e., a Drupal block that is really a snippet of PHP code? (As opposed to plain HTML.)
One of the cool things about Drupal is that you can create and use blocks so easily, including custom Drupal PHP blocks. I use custom Drupal blocks for all sorts of things on my Drupal websites, including rotating Drupal block content, as I'll show in this example.
Custom Drupal PHP block example - rotating content
At the time of this writing (June 19, 2010), I haven't decided what to do with the block I have on the upper-left corner of the devdaily website. There are several things I want to show up there, including:
- A link to let people subscribe to my newsletter.
- A link to the devdaily.com RSS feed.
- A link to let other authors write on the devdaily.com website.
Unable to decide what to show up there, I finally decided to just randomly rotate these three links, and think about something else for a while.
Custom Drupal PHP block example - source code
Given that decision (not to decide), I just created the following Drupal PHP block to rotate the content for these three links. I did that with the following PHP source code. I won't describe the code here, because I have added comments to it, but if you have questions about this code, just use the Comments section below to share your questions.
<?php // a drupal php block example // alvin alexander, http://devdaily.com // first, here are the three snippets of HTML that I want to display in // my upper-left corner block // (1) html for "subscribe to newletter" $newsletter = <<< NEWSLETTER <!-- NEWSLETTER --> <p class="rtecenter"><a target="_blank" border="0" href="http://eepurl.com/BhYt"><img title="subscribe to our newsletter" alt="subscribe to our newsletter" src="/sites/default/files/users/user3/devdaily-news-96x77.png" /><br /> subscribe to our<br /> monthly newsletter</a></p> NEWSLETTER; // (2) html for "rss feed html" $dd_rss_feed = <<< DD_RSS <!-- RSS --> <p class="rtecenter"><a href="/rss.xml"><img alt="Subscribe to our RSS feed" title="Subscribe to our RSS feed" src="/images/feed-icon-64x64-ds.png" /></a> <br />rss feed</p> DD_RSS; // (3) html for "write for devdaily html" $dd_write = <<< WRITE_FOR_DD <!-- WRITE --> <p class="rtecenter"><br /> <a alt="Write for devdaily" title="Write for devdaily" href="/news/2010/06/03/devdaily-open-to-writers"><img alt="" src="/images/write/article-48.png" /><br />write for devdaily </a></p> <p> </p> WRITE_FOR_DD; // get a random number that varies from "1" to the number of // html content blocks you want to rotate $r = rand(1,3); // now just emit the output if ($r == 1) { echo $newsletter; } elseif ($r == 2) { echo $dd_rss_feed; } else { echo $dd_write; } ?>
Admittedly this custom PHP code could probably be improved a little, but I like it this way, because it's very simple, and easy to maintain.
Creating your custom Drupal PHP block
Getting this custom Drupal PHP block code to work on your website is now fairly straightforward. Just follow these steps, being careful to use the "Plain text editor" when entering this PHP code:
- Go to your Drupal blocks admin page (admin/build/block).
- Click the "configure" link for the block you want to modify, or, create a new block.
- Enter the Block Description, as usual.
- Enter a Block Title, if desired.
- In the Block Body section, switch to the Plain Text Editor. (This is a must!)
- Paste your custom PHP code into the text editor area.
- In the Input Format section, click the "PHP code" radio button. This is also a must.
- Save your block.
- If the block isn't already showing on your website, position it so it is showing, and hopefully everything looks great!
Custom Drupal PHP block example - summary
I hope this Drupal PHP block example has been helpful. One of the great things about PHP is that you can quickly learn how to create PHP snippets like this. And, as mentioned, a great feature of Drupal is that you can create custom blocks very easily.