Changing Default Forumbit in forumlist
jshort Offline
Junior Member
**
Posts: 3
Joined: Aug 2013
Post: #1
Changing Default Forumbit in forumlist
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:
1
2
3
4
5
6
7
8
9
10
11
12
$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.

(This post was last modified: 08-13-2013 06:15 AM by jshort.)
08-13-2013 02:33 AM
Find all posts by this user Quote this message in a reply
ZiNgA BuRgA Offline
Fag
*******
Posts: 3,357
Joined: Jan 2008
Post: #2
RE: Changing Default Forumbit in forumlist
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.

My Blog
08-13-2013 12:41 PM
Find all posts by this user Quote this message in a reply
jshort Offline
Junior Member
**
Posts: 3
Joined: Aug 2013
Post: #3
RE: Changing Default Forumbit in forumlist
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.
08-13-2013 02:25 PM
Find all posts by this user Quote this message in a reply
ZiNgA BuRgA Offline
Fag
*******
Posts: 3,357
Joined: Jan 2008
Post: #4
RE: Changing Default Forumbit in forumlist
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:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    $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.

My Blog
(This post was last modified: 08-14-2013 08:25 PM by ZiNgA BuRgA.)
08-14-2013 08:17 PM
Find all posts by this user Quote this message in a reply

« Next Oldest | Next Newest »

 Standard Tools
Forum Jump: