MyBB Hacks

Full Version: Changing Default Forumbit in forumlist
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,

I'm currently in the process of developing a plugin that will allow me to change which forumbit templates are used for my forums based on a specific forum setting.

Essentially, my forums can be checked as either "A" or "B". I'm trying to hook into the functions_forumlist to change the $forum_list eval so get $templates->forumbit_a_ or $templates->forumbit_b.

Trying to just change the eval with an if/else statement doesn't seem to work, nor can I get access to the $forumcat or $depth variables to tack a prefix on to those.

Here's the code:

Code:
$plugins->add_hook("build_forumbits_forum", "plugin_forumbits");
function plugin_forumbits($forum)
{
	global $mybb, $templates;
	
	if($forum['type_a'] == 1) {
		eval("\$forum_list .= \"".$templates->get("forumbit_depth$depth$forumcat_a")."\";");
	}
	else {
		eval("\$forum_list .= \"".$templates->get("forumbit_depth$depth$forumcat")."\";");
	}
}


I have been able to accomplish this by directly editing the functions_forumlist.php file, but I wanted to see if I could make it a plugin instead.

I'm aware that xthreads has this capability, but the xthreads plugin is overkill for what I'm trying to accomplish. I would also like to avoid putting conditionals in all the individual templates.

Thanks in advance for any help.

Edit: Not sure who changed the topic title of my request - but Threaded Mode | Linear Mode isn't what I'm talking about.

Would it be acceptable to just evaluate into a separate variable instead and display that in the template?  I admit that the placement of the hook is rather suboptimal though.
Remapping the actual templates is not exactly an easy thing.
Evaluating into a separate variable could possibly work, but because of the drastic visual differences between _a forums and regular forums, it would mean creating three template sets: one for each of the types of forum and then one template with the variable in it that determines which of the other two templates it used.

I've googled this pretty extensively and nothing like this seems to exist, so unfortunately I don't think it's easily possible. I've gone ahead and just made the core edit - I just find it funny that a relatively simple 4 line edit cannot be done with the plugin system.

I would be interested in learning how it's done and did look through the xthreads source code to try and figure it out. It's definitely a complicated system, so I can understand why there aren't any other examples out there that would help explain it.

Thanks for taking the time to answer, I appreciate it.
A core edit is definitely the simplest approach.  The plugin API gives you points of access, but it's often actually difficult to modify behavior.

The proposed method of having multiple templates may be problematic due to how PHP scopes variables (some variables may be unavailable to you).

Some methods I can think of, using the plugin system, to achieve this:
  • Overriding the $templates->cache store, example:

    PHP Code:
    $plugins->add_hook("build_forumbits_forum", "plugin_forumbits");
    function plugin_forumbits($forum)
    {
    	global $mybb, $templates;
    	static $revert = null;
    	$tpl = array('forumbit_depth1'); // list all the templates you wish to override here
    	
    	if(!isset($revert)) {
    		// initialisation code
    		
    		$tpl_a = array();
    		foreach($tpl as $t) $tpl_a[] = $t.'_a';
    		
    		$templates->cache(implode(',', $tpl_a)); 
    		// backup copy of templates
    		foreach($tpl as $t)
    			$templates->cache['__plugin_'.$t] = $templates->cache[$t];
    		
    		$revert = false;
    	}
    	
    	if($forum['type_a'] == 1) {
    		// do template hack
    		foreach($tpl as $t)
    			$templates->cache[$t] = $templates->cache[$t.'_a'];
    		$revert = true;
    	}
    	elseif($revert) {
    		// revert template hack
    		foreach($tpl as $t)
    			$templates->cache[$t] = $templates->cache['__plugin_'.$t];
    		$revert = false;
    	}
    }

  • Putting a placeholder in the template which you replace (perhaps using regex) later on
  • Dynamically extending (ala control_object) the $templates object - this is what XThreads / Template Conditionals does

Hope that gives you ideas should you wish to try out doing it through a plugin again.
Reference URL's