Back to vBulletin 3 Articles

[How-to] Add a multiselect field in vBulletin options
by Coroner 23 Sep 2008

I wrote this article 'cause I didn't found anything else but it was needed for mod I wrote.

Before I start - will have to say this is an example for a forumchooser.

In our product under options, create an option like this:
PHP Code:
<setting varname="your_setting_varname" displayorder="1">
 <
datatype>free</datatype>
 <
optioncode>multiselect:eval
$options construct_forum_chooser_options(1);</optioncode>
 <
defaultvalue>0</defaultvalue>
</
setting
I'll use: multiselect:eval.

Ok, we finished our first part and need 2 new plugins.

The first plugin we used is admin_options_print.
PHP Code:
    <plugin active="1" product="yourproduct">
      <
title>expand options for multiselect</title>
      <
hookname>admin_options_print</hookname>
      <
phpcode><![CDATA[if (preg_match ('#^(multiselect):(eval)(\r\n|\n|\r)(.*)$#siU'$setting['optioncode'], $matches))
{
 
$options null;
 eval (
$setting['optiondata']);
 
// this is for multiselect options
 
$title $description;
 
$array construct_forum_chooser_options (0);
 
$selected explode (','$setting['value']);
 
$name .= "[]";
 
$htmlise 0;
 
$size 10;
 
$multiple true;
 global 
$vbulletin;
 
$uniqueid fetch_uniqueid_counter ();
 
$select "<div id=\"ctrl_$name\"><select name=\"$name\" id=\"sel_{$name}_$uniqueid\" tabindex=\"1\" class=\"bginput\"" iif($size" size=\"$size\"") . iif($multiple' multiple="multiple"') . iif($vbulletin->debug" title=\"name=&quot;$name&quot;\"") . ">\n";
 
$select .= construct_select_options ($array$selected$htmlise);
 
$select .= "</select></div>\n";
 
print_label_row ($title$select'''top'$name);
 
$handled true;
}]]></
phpcode>
    </
plugin
See the code above and take a look at this three lines:
$array = construct_forum_chooser_options (0);
$selected = explode (',', $setting['value']);
$name .= "[]";
The 1st line are our options array, filled with information coming from the function "construct_forum_chooser_options(0)". If you wanna have your own, create an array with your options.
2nd line: our value (saved later in the options will implode by a comma like: 5,8,11,26,.. we need to explode this value to get the selected values back.
3rd line: this is needed for multiselect

The next plugin is called: admin_options_processing.
PHP Code:
<plugin active="1" product="yourproduct">
      <
title>save our multiselect values</title>
      <
hookname>admin_options_processing</hookname>
      <
phpcode><![CDATA[
if (
preg_match ('/multiselect/i'$oldsetting['optioncode']))
{
 if (
is_array ($settings["$oldsetting[varname]"])) $settings["$oldsetting[varname]"] = implode (','$settings["$oldsetting[varname]"]);
}]]></
phpcode>
    </
plugin
This plugin will implode our selected values with a comma.

Regards

vblts.ru supports vBulletin®, 2022-2024