1
MadFish
Tutorial: How to clone a block
  • 2006/8/1 11:55

  • MadFish

  • Friend of XOOPS

  • Posts: 1056

  • Since: 2003/9/27


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 block
Cloning 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 template

7. 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!

2
Quest
Re: Tutorial: How to clone a block
  • 2006/8/1 12:04

  • Quest

  • Friend of XOOPS

  • Posts: 1034

  • Since: 2005/11/19


Thank you for this tutorial. I had tried this myself once and nearly got it to work but couldn't. Thanks to your tutorial I can now do it. Expecially knowing about checking for to make sure the new cloned block has a Unique number.

Quest

3
Djiman
Re: Tutorial: How to clone a block
  • 2006/8/1 13:29

  • Djiman

  • Just popping in

  • Posts: 91

  • Since: 2006/7/26


Will it be silly to ask about the image for news.
How to get at-least one image inside the block, in a specific thumbnail size (like recent news block-have no idea if it is there yet).

I mean the image that is used to illustrate the news item, if any! and not the topic image.))

..thanks for the clone too.

4
MadFish
Re: Tutorial: How to clone a block
  • 2006/8/1 13:51

  • MadFish

  • Friend of XOOPS

  • Posts: 1056

  • Since: 2003/9/27


I'm not sure, I would probably use the spotlight module for that. I think the Recent News block actually has a spotlight functionality if you go into blocks=>edit, but I haven't played with it much.

5
jensclas
Re: Tutorial: How to clone a block

Would you consider putting this 'tutorial' at xoopsdocs? These kinds of resources are what we are after, if you to to the workspace and scroll down to 'extras'-'tutorials' you will see there is even a place for them. Personally I think this is a better palce than FAQ's.

6
MadFish
Re: Tutorial: How to clone a block
  • 2006/8/4 7:28

  • MadFish

  • Friend of XOOPS

  • Posts: 1056

  • Since: 2003/9/27


Hi Jen

Yes no problem, I'll post it up tonight :)

7
jensclas
Re: Tutorial: How to clone a block

Thats excellent

8
homergz
Re: Tutorial: How to clone a block
  • 2006/8/4 8:09

  • homergz

  • Just popping in

  • Posts: 80

  • Since: 2006/5/21


Thank you for this excellent tutorial MadFish.

Login

Who's Online

431 user(s) are online (363 user(s) are browsing Support Forums)


Members: 0


Guests: 431


more...

Donat-O-Meter

Stats
Goal: $100.00
Due Date: Nov 30
Gross Amount: $0.00
Net Balance: $0.00
Left to go: $100.00
Make donations with PayPal!

Latest GitHub Commits