View Full Version : Custom Template Hook
Hi,
I want to add a template hook called 'above_header' right at the beginning of the 'header' template. So in the 'header' template I've added {vb:raw template_hook.above_header}.
This template hook shall be used by a plugin which hooks 'forumdisplay_start'. The code of the plugin is:
$forum = $vbulletin->forumcache["$forumid"];
$templater = vB_Template::create('ivwcode');
$templater->register('forum', $forum);
$template_hook['above_header'] .= $templater->render();
The 'ivwcode' template currently consists of just the text "foobar".
Unfortunately the content of the 'ivwcode' template is missing in the generated HTML code. Why?
Regards
TiKu
It may be that forumdisplay_start is too late, because the header can be rendered before that. You could try to set $template_hook['above_header'] to some string constant just to make sure that it's not a problem with your custom template. If you still don't see anything, try using hook parse_templates (and you'd probably want to enclose it in if (THIS_SCRIPT == 'forumdisplay') so it's not happening on every page).
Thank you, it works with parse_templates. However, how can I get the forum info on this hook?
nhawk
06-07-2013, 10:23 AM
Thank you, it works with parse_templates. However, how can I get the forum info on this hook?
$foruminfo['forumid'] is available in the parse_templates hook.
That's true, but I think you may need a "global $foruminfo;" statement.
nhawk
06-07-2013, 01:19 PM
You are correct. I forgot about that.
Many thanks, after adding "global $foruminfo;", it is working.
But now I've another problem. I've added a custom field to the 'forum' table. I did this directly in phpMyAdmin, because I could not make the following installcode work:
require_once(DIR . '/includes/class_dbalter.php');
$db_alter = new vB_Database_Alter_MySQL($db);
if($db_alter->fetch_table_info('forum')) {
$db_alter->add_field(
array(
'name' => 'ivw_code',
'type' => 'varchar',
'length' => '25',
'attributes' => '',
'null' => true,
'default' => '',
'extra' => ''
)
);
}
unset($db_alter);
Anyway, now the SQL field is existing in the table 'forum', but in my hook, $foruminfo does not include this field: $foruminfo['ivw_code'] is not set, although the database contains data for this forum in this column. What am I doing wrong?
I think the $foruminfo array comes from the forumcache and not from a read of the database, so if you can get the forumcache updated, it should include your new field. Otherwise you'd need to read your field from the database yourself.
If you edit any forum in the forum manager it should rebuild the cache, so you can try that to see if it works. If it does, there's a function called build_forum_permissions() that you could try calling in your install code. It takes one parameter, "rebuild_genealogy" which defaults to true, but I think it's a flag for something that you don't need to do so you can probably pass false to override it (you can see the function in file includes/adminfunctions.php).
Ah, now I can see the field in the foruminfo array. Many thanks.
I've noticed an interesting detail: The foruminfo array contains 'ivwcode' as well as 'ivw_code'. In the database I can see only 'ivw_code', but I remember that in the beginning I had named the field 'ivwcode', without underscore, in my installer code. So it seems like the installer code somehow worked, but it did not actually create the field in the database?!
/Edit: Okay, I've added code to drop the 'ivwcode' field on uninstall and then uninstalled and reinstalled the add-on. Now the 'ivwcode' field is gone, and foruminfo contains only the 'ivw_code' field. But it does not get inserted to the database, I have to do this manually.
--------------- Added 1370685483 at 1370685483 ---------------
ARGH! Never mind, I'm doing all this on a test installation and my colleague has moved this test installation to a new database server without telling me. He has left the old database on the old server, so I have been looking at the wrong database all the time.
--------------- Added 1370729379 at 1370729379 ---------------
I have an issue with unwanted data type conversion. My custom SQL field is of type varchar(25). When it contains the string '12E03611', it will be stored as a float in the $foruminfo array. How can I force VB4 forum cache to store this as a string?
vBulletin® v3.8.12 by vBS, Copyright ©2000-2024, vBulletin Solutions Inc.