MyBB Community Forums
Hook build_forumbits_forum and Templates - Printable Version

+- MyBB Community Forums (https://community.mybb.com)
+-- Forum: Extensions (https://community.mybb.com/forum-201.html)
+--- Forum: Plugins (https://community.mybb.com/forum-73.html)
+---- Forum: Plugin Development (https://community.mybb.com/forum-68.html)
+---- Thread: Hook build_forumbits_forum and Templates (/thread-233359.html)



Hook build_forumbits_forum and Templates - windkind - 2021-08-22

Hi!

I'm writing a little plugin for my forum, to show a random thread between two categories. In the attachement it's the part "Eigener Inhalt" (Translation: Kategorie = Category, Eigener Inhalt = own content (like a random thread)).

Now I began writing my plugin, install/deinstall/activate/deactivate works great - but the main function creats an Error 500 as soon as I try to include a new template that I created during installation of the plugin.

The working format by now is:

$plugins->add_hook('build_forumbits_forum', 'wanted_index');

function wanted_index(&$forum)
{
  global $mybb, $db, $templates;

// Reading the settings - at which category should it be shown?
// In the settings this is a selectforumsingle
  $wanted_show = $mybb->settings['wanted_show'];

// If the ForumID isn't the selected forum, then ..
  if ($forum['fid'] != $wanted_show) return;


// In other cases
  $forum['wanted_index'] = "Test text";
}


As soon as I try calling a template with
eval('$forum['wanted_index'] = "'.$templates->get('wanted_index').'";');
it shows Error 500.

With
eval('$wanted_index = "'.$templates->get('wanted_index').'";'); 
and including {$wanted_index} in forumbit_depth1_cat (instead of {$forum['wanted_index']}), it shows nothing.


Yes, there is missing a lot of stuff, but before I write this, my question is: Is there any possibility to include a template at any way using that hook?
I'm working with MyBB 1.8.27

Thank you! Smile
Please ask if you don't understand everything. English isn't my native language so mistakes and misunderstandings happen!


RE: Hook build_forumbits_forum and Templates - Matt - 2021-08-22

You'll need to globalise $wanted_index, and I think escape some characters in the eval itself:

function wanted_index(&$forum)
{
	global $mybb, $db, $templates, $wanted_index;

	...
	eval("\$wanted_index = \"".$templates->get('wanted_index')."\";");
}

See if that works.

The reason this breaks and causes a 500:

eval('$forum['wanted_index'] = "'.$templates->get('wanted_index').'";');

is because you're using single quotes in the eval, and in the array key, so it closes the quotes and would result in a parse error. If you do it that way, you need to escape them, or use double quotes:

eval("\$forum['wanted_index'] = \"".$templates->get('wanted_index')."\";");

or:

eval('\$forum[\'wanted_index\'] = "'.$templates->get('wanted_index').'";');



RE: Hook build_forumbits_forum and Templates - windkind - 2021-08-22

Hi Matt!

Thank you very very much. Smile Now it is working! 

With the $wanted_index in global it still showed nothing.
But with the corrected eval it works fine! Smile



Thank you! Smile


RE: Hook build_forumbits_forum and Templates - Matt - 2021-08-22

You're welcome Smile


RE: Hook build_forumbits_forum and Templates - Omar G. - 2021-08-22

$wanted_index = eval($templates->render('wanted_index'));



RE: Hook build_forumbits_forum and Templates - Matt - 2021-08-22

(2021-08-22, 10:50 PM)Omar G. Wrote:
$wanted_index = eval($templates->render('wanted_index'));

:o Since when?!


RE: Hook build_forumbits_forum and Templates - Omar G. - 2021-08-23

(2021-08-22, 11:48 PM)Matt Wrote:
(2021-08-22, 10:50 PM)Omar G. Wrote:
$wanted_index = eval($templates->render('wanted_index'));

:o Since when?!

Since 1.8.0 😅 The core just wasn't updated to make use of it so it isn't as used as it should be by now.


RE: Hook build_forumbits_forum and Templates - windkind - 2021-08-23

(2021-08-22, 10:50 PM)Omar G. Wrote:
$wanted_index = eval($templates->render('wanted_index'));

I tried it (and of course make $wanted_index global again) - but nothing. I think it is because of that special hook that I need to work with the array $forum Smile


RE: Hook build_forumbits_forum and Templates - Omar G. - 2021-08-23

$wanted_index = eval($templates->render('wanted_index'));

Is just a replacement of :

eval("\$wanted_index = \"".$templates->get('wanted_index')."\";");

It should simply work.