Pages

Wednesday 5 September 2012

How to make Drupal piece of content that refreshes itself every 24 hours


This Drupal tutorial is easy, yet it can come very handy and useful sometimes. For one website, I needed to make one part on the frontpage to display teaser of random article, and refreshes itself every 24 hours. Easy, I thought, I could use block_cache module and that's all. The thing is, I am not using this block on sidebars, I need to display it on the frontpage, and on the specific part of the page... That means I need to display block from the template directly.
After some research, I found out about one module that is called insert_block. Its function is to insert contents of any block from your templates. Of course you can do this with Drupal API, using php code, but this is much easier, and safer.
So, we're going to use a combination of views, block_cache and insert_block modules to display our random content in a block that refreshes itself every 24 hours. This could be useful, for example for your frontpage, to have a "featured article", that will change every day.
Take a look at Overcome anxiety and depression website . At the frontpage in the lower right part, there is a "featured article" That is actually a block that is using one view that randomly selects one article and displays it using insert_block.
I will assume you already have some articles on your website.
1. Enable modules: Views, Block Cache, Insert Block.
2. Make the following view:
  • name it "feat_article"
  • check "Provide Block", view type is "Teaser list"
  • nodes per block: 1
  • in Fields section add "Node: Title" and Node: Body (teaser)
  • in Filters section add "Node: type" is one of "Page" (or whatever your type is) and "Node:published" equals to "yes".
  • in "Sort criteria" select "Random" from dropdown menu, and add criteria.
  • Save your view.
3. Go to Administer > Site configuration > Input formats
4. We will use "Full HTML" input format, so click "Configure" next to "Full HTML"
5. In Filters section, check "insert block filter" and Save configuration.
6. Now, go to Administer > Site building > Blocks. Find block "feat_article [[-CACHED-]]". Click "Configure". Uncheck everything and for the "Cache lifetime" put 86400. That is the number of seconds our block will refresh itself. 86400 seconds = 24 hours. So our block will refresh itself every 24 hours. If you need different interval, just find out how many second it is and put it in. Save block.
7. Find out what is delta number of "feat_article [[-CACHED-]]" block. Hover over "configure" link next to "feat_article [[-CACHED-]]" block and see the number at the end. Write down that number, you will need it in a second.
8. Go to your template where you want to insert this block. For the frontpage I use regular page content type. The syntax for inserting a block is:
[block:name of module=delta of block]
So if your delta number of block is 20, your syntax would be:
[block:blockcache=20]
For input format select "Full HTML".
9. Save everything.
Now you should have your piece of content refreshed every 24 hours automatically. The more nodes you have, the better. It will randomly select one node and display its teaser for the following 24 hours. It's pretty cool stuff.

No comments:

Post a Comment