Go Back   vb.org Archive > vBulletin Article Depository > Read An Article > vBulletin 4 Articles
How to create your own vBulletin 4 sidebar block.
Dave
Join Date: May 2010
Posts: 2,583

 

Show Printable Version Email this Page Subscription
Dave Dave is offline 01-31-2015, 10:00 PM

It's easier to read this on my blog at https://blog.technidev.com/how-to-cr...sidebar-block/.
Let me know if you have any tips/suggestions or need help.


Since I couldn't really find any guide on how to create your own forum sidebar block with custom settings, I decided to write this little guide. It consists of a few things:
- The .php file which takes care of most of the functionality
- The configuration
- Phrases

The main .php file
All of the current blocks are present in the /includes/block/ folder. To make your own block we have to make a new file in this directory. Name it something logical since the filename is dependent of a few things. We are simply going to copy the threads.php file in the /includes/block/ folder. For this guide, I copied the threads.php file to technidev.php. The functionality I will implement is to show the latest x registered users.

Now open the file you just duplicated and change the classname, currently it's "class vB_BlockType_Threads extends vB_BlockType" and we have to rename that to "class vB_BlockType_Technidev extends vB_BlockType".
Say you duplicated the threads.php file to onlineusers.php, you name it to "class vB_BlockType_Onlineusers extends vB_BlockType".

The file consists of a few important things:
- The protected $settings variable. This defines the settings of the block.
- getData function. You should use this to retrieve all of the necessary data.
- getHTML function. This will call the getData function and returns any output.

$settings variable
This will display all of the settings when you want to add a new block in the admincp. For our functionality I want the user to be able to select which usergroup is a "registered" usergroup and how many of the latest users we want to display. That means we have to add 2 settings as follows:

PHP Code:
    protected $settings = array(
        
'technidev_usergroupid' => array(
            
'defaultvalue' => 2,
            
'displayorder' => 1,
            
'datatype'     => 'integer'
        
),
        
'technidev_amount' => array(
            
'defaultvalue' => 5,
            
'displayorder' => 2,
            
'datatype'     => 'integer'
        
)
    ); 

That's how easy it is, the $settings variable is very flexible and you can add many things. All of the settings can be reached with the $this->config array, $this->config['technidev_usergroupid'] for example. But say you want to make a select box where the user will be able to select multiple usergroupid's, you can add an option such as:

PHP Code:
    protected $settings = array(
        
'technidev_usergroupids' => array(
            
'defaultvalue' => -1,
            
'optioncode'   => 'selectmulti:eval
            $uid = $vbulletin->db->query_read("SELECT usergroupid FROM " . TABLE_PREFIX . "usergroup ORDER BY title ASC");
            $options = array();
            while($row = $vbulletin->db->fetch_array($uid)){
                $options[$row[\'id\']] = $row[\'title\'];
            }'
,
            
'displayorder' => 4,
            
'datatype'     => 'arrayinteger'
        
)
    ); 
This will set $this->config['technidev_usergroupids'] as an array.

Note that if you are an advanced user, check out the includes/adminfunctions_options.php file and the function print_setting_row, you can see which options you can use in that function.

The getData and getHTML function
The getData function will be used to retrieve any data and return an array. In my case I want to retrieve the latest registered users based on the settings which are specified by the user. We can do something such as:

PHP Code:
    public function getData()
    {
        
$user $this->registry->db->query_read("
            SELECT userid, username
            FROM " 
TABLE_PREFIX "user
            WHERE usergroupid = " 
$this->config['technidev_usergroupid'] . "
            ORDER BY userid DESC
            LIMIT 0," 
$this->config['technidev_amount']);

        
$users = array();
        while(
$row $this->registry->db->fetch_array($user)){
            
$users[] = $row;
        }
        return 
$users;
    } 

The getHTML function should return the HTML output, in this case we can simply create a template, register variables and return the rendered output. Since we retrieve an array, we can simply register it as a variable to our own block_technidev template and then register the output to a variable for the block_html template.

The getHTML function will look like this:

PHP Code:
    public function getHTML($userarray false)
    {
        if (!
$userarray)
        {
            
$userarray $this->getData();
        }

        if (
$userarray)
        {
            
$templater vB_Template::create('block_technidev');
                
$templater->register('blockinfo'$this->blockinfo);
                
$templater->register('stream'$userarray);
            
$block $templater->render();

            
$templater vB_Template::create('block_html');
                
$templater->register('blockinfo'$this->blockinfo);
                
$templater->register('content'$block);
            return 
$templater->render();
        }
    } 

And the template block_technidev will look something like:
PHP Code:
        <ul id="block_activitystream_{vb:raw blockinfo.blockid}" class="blockrow activitystream">
            <
vb:each from="stream" key="id" value="item">
                <
li class="floatcontainer widget_post_bit">
                    <
a href="member.php?{vb:var item.userid}">{vb:var item.username}</a>
                </
li>
            </
vb:each>
        </
ul
Phrases
This is a very important thing because if you don't add the required phrases, the block will miss a lot of information such as the setting titles/descriptions and the name of the block itself.
We first add the block phrase:
1. Languages & Phrases -> Phrase Manager -> Add New Phrase
2. Select ?Forum Blocks? as the Phrase Type and ?vBulletin? as the product
3. Set the varname to blocktype_yourblock, blocktype_technidev in my case.
4. Set the text to your block name. "Technidev Registered Users" in my case and save.

Now we have to register the variables for the settings:
1. Languages & Phrases -> Phrase Manager -> Add New Phrase
2. Select ?Forum Blocks Settings? as the Phrase Type and ?vBulletin? as the product
3. Set the varname to "setting_technidev_usergroupid_title"
4. Set the text, this is the title of the setting and save.
5. Repeat this but use "setting_technidev_usergroupid_desc" as varname, this will be the description of the setting.

Testing
Now if you did it all right, you should go to Forums & Moderators -> Forum Blocks Manager -> Click at the bottom on "Reload Block Types" -> Now add a new block -> Find the name of your block -> Configure the settings and add it.



I added and configured my block, and I now see this in the sidebar:



It doesn't look fancy, but you get the idea. You can fully customize it to your needs within the template and the .php file.

It's easier to read this on my blog at https://blog.technidev.com/how-to-cr...sidebar-block/.
Let me know if you have any tips/suggestions or need help.
Reply With Quote
  #2  
Old 07-21-2015, 03:39 PM
friendlymela's Avatar
friendlymela friendlymela is offline
 
Join Date: Dec 2012
Location: Karachi, Pakistan
Posts: 272
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

do i create side block for RSS poster?
Reply With Quote
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 11:54 PM.


Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2024, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.03502 seconds
  • Memory Usage 2,241KB
  • Queries Executed 17 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (5)bbcode_php
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)modsystem_article
  • (1)navbar
  • (4)navbar_link
  • (120)option
  • (2)post_thanks_box
  • (3)post_thanks_box_bit
  • (2)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (1)post_thanks_postbit
  • (2)post_thanks_postbit_info
  • (1)postbit
  • (2)postbit_onlinestatus
  • (2)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_postinfo_query
  • fetch_postinfo
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • fetch_musername
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • post_thanks_function_fetch_thanks_bit_start
  • post_thanks_function_show_thanks_date_start
  • post_thanks_function_show_thanks_date_end
  • post_thanks_function_fetch_thanks_bit_end
  • post_thanks_function_fetch_post_thanks_template_start
  • post_thanks_function_fetch_post_thanks_template_end
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete