I submitted the following tutorial to the XOOPSFAQ a couple of days ago, but seeing as I'm an impatient type, I thought I would post it here too! Enjoy.
How to clone a blockCloning a block is so easy that a monkey could do it. The following example focuses on cloning the ‘recent news’ block of the News module, although the procedure should be the same for every block.
1. Open the file xoops_version.php in the module for which you want to clone a block. The path is: /modules/news/xoops_version.php
This file contains the declarations for creating all the blocks in the module. To clone the Recent News block, look through the code (under the // Blocks comment) until you find its declaration, which looks like this:
$modversion['blocks'][4]['file'] = "news_top.php";
$modversion['blocks'][4]['name'] = _MI_NEWS_BNAME5;
$modversion['blocks'][4]['description'] = "Shows recent articles";
$modversion['blocks'][4]['show_func'] = "b_news_top_show";
$modversion['blocks'][4]['edit_func'] = "b_news_top_edit";
$modversion['blocks'][4]['options'] = "published|10|25|0|0|0|0||1||||||";
$modversion['blocks'][4]['template'] = 'news_block_top.html'; // news_block_recent.html
As you can see, each block is assigned a unique number in its declaration. The Recent News block is number 4 out of a total of 7 in the version of this module that was current at the time of writing (1.44). We are going to make a copy of this block, which will become number 8.
2. To clone the block, copy the above code, and paste it in at the end of the other block declarations, changing the module number to 8. The pasted code should look like this:
$modversion['blocks'][8]['file'] = "news_top.php";
$modversion['blocks'][8]['name'] = _MI_NEWS_BNAME5;
$modversion['blocks'][8]['description'] = "Shows recent articles";
$modversion['blocks'][8]['show_func'] = "b_news_top_show";
$modversion['blocks'][8]['edit_func'] = "b_news_top_edit";
$modversion['blocks'][8]['options'] = "published|10|25|0|0|0|0||1||||||";
$modversion['blocks'][8]['template'] = 'news_block_top.html'; // news_block_recent.html
If you wanted to, you could also change the ‘description’ to something else, like ‘Shows football news’, or whatever.
3. Optional: If you want to give your new block a unique name (not strictly necessary as you can give it whatever user side name you like in blocks administration), open the relevant language file, which is something like: /modules/news/language/english/modinfo.php.
Look through the modinfo.php code until you find the block name definitions, which in this case are:
// Names of blocks for this module
define('_MI_NEWS_BNAME1','News Topics');
define('_MI_NEWS_BNAME3','Big Story');
define('_MI_NEWS_BNAME4','Top News');
define('_MI_NEWS_BNAME5','Recent News');
define('_MI_NEWS_BNAME6','Moderate News');
define('_MI_NEWS_BNAME7','Navigate thru topics');
Add another definition for your new block, using a unique number, eg:
define('_MI_NEWS_BNAME9','Yet another news block');
Don't forget to change the 'name' line in the block declaration code in xoops_version.php above to point at your new _MI_NEWS_BNAME9 definition in modinfo.php. (The reason I used the number ‘9’ was that one of the blocks in xoops_version.php already uses 8, even though there is no corresponding entry in the language file. I don’t know why – but anyway, the important thing is to use a unique number).
4. Save and upload your amended xoops_version.php file (and optionally, the amended modinfo.php).
5. You must update the module for the new block to become available. Go to Administration => System module => Modules and press the ‘Update’ button for the News module. This will recompile the templates. You should see your new block listed as created in the results screen.
6. The new block should now be available in Administration => System => Blocks. You can do with it as you wish, just like any other block.
Optional: Creating a custom template7. If you wanted to, you could create a custom template for your new block. If we go back to the last line of our cloned Recent News block declaration:
$modversion['blocks'][8]['template'] = 'news_block_top.html'; // news_block_recent.html
The procedure would be to i) make a copy of the relevant template, in this case news_block_top.html with a new name such as ‘news_block_football.html’, ii) make whatever changes are necessary, and iii) amend the code in xoops_version.php to point at the cloned and amended template, in this example, to read:
$modversion['blocks'][8]['template'] = 'news_block_football.html'; // news_block_football.html
If you don’t create a separate template for the new block (and you don’t have to), then any changes you make to the template for the original block will affect the clone.
Cool applications of cloning blocks.1. Allows you to position a block in different locations in different modules.
2. Some blocks, like Recent News (or Recent Posts in the Forum modules) allow you to filter their content by category (this functionality is available via the ‘edit block’ links in blocks administration). So you could conceivably create a ‘recent news’ block for every category that you have. If you combine this feature with a duplicatable content module like TinyD, you can effectively create topic-specific sub-portals within your website – without doing any major or risky hacks!