News:

Want to get involved in developing SMF, then why not lend a hand on our github!

Main Menu

Change Text Via Hook File (str_replace)

Started by Hj Ahmad Rasyid Hj Ismail, September 05, 2014, 11:48:00 PM

Previous topic - Next topic

Hj Ahmad Rasyid Hj Ismail

Hi all,

I am trying to convert most of my 2.0 mods to fully using hook. I noted that some mods make necessary changes to the template files.

In one mod, I am using str_replace to change a code inside the header in index.template.php. However, I didn't seems to be able to replicate that if the code is inside the body i.e. in the template files.

The one that I successfully did (in the header section) by using integrate_buffer was changing this:
<title>' . $context['page_title_html_safe'] . '</title>
to this:
<title>' . (empty($context['page_title_html_safe']) ? $context['forum_name'] : $context['forum_name']. ' - ' . $context['page_title_html_safe']) . '</title>

But using the same, I am not able to change (in the body section) from this:
<div class="', $message['approved'] ? ($message['alternate'] == 0 ? 'windowbg' : 'windowbg2') : 'approvebg', '">
to this:
<div class="' . $message['approved'] && $message['member']['is_buddy'] && $modSettings['hlbm_enable'] ? 'buddybg' : ($message['approved'] ? ($message['alternate'] == 0 ? 'windowbg' : 'windowbg2') : 'approvebg') . '">

The installation when fine but the replaced code is somehow broken when I implement changes on the template file (in the body section). I was also not able to detect error(s) that can help to fix this.

Oh yes, the full code for each. The successful one:
function RIAST_Titles(&$buffer)
{
global $modSettings, $context, $txt;

if (isset($_REQUEST['xml']) || $context['current_action'] == 'printpage') return $buffer;

$riast = array();

// Description
if (!empty($modSettings['riast_enable'])) {
// Define the old and the new title
$title_old = '<title>' . $context['page_title_html_safe'] . '</title>';
$title_new = '<title>' . (empty($context['page_title_html_safe']) ? $context['forum_name'] : $context['forum_name']. ' - ' . $context['page_title_html_safe']) . '</title>';
$riast[$title_old] = $title_new;
}
// Now let's change the title, if we're allowed to
return str_replace(array_keys($riast), array_values($riast), $buffer);
}


The unsuccessful one:
function HLBM_Buffer(&$buffer)
{
global $modSettings, $context, $txt;

// Load the HLBM language
loadLanguage('HLBM');

if (isset($_REQUEST['xml']) || $context['current_action'] == 'printpage') return $buffer;

$hlbm = array();

// Description
if (!empty($modSettings['hlbm_enable'])) {
// Define the old and the new title
$oldbg = '<div class="' . $message['approved'] ? ($message['alternate'] == 0 ? 'windowbg' : 'windowbg2') : 'approvebg' . '">';
$newbg = '<div class="' . $message['approved'] && $message['member']['is_buddy'] ? 'buddybg' : ($message['approved'] ? ($message['alternate'] == 0 ? 'windowbg' : 'windowbg2') : 'approvebg') . '">';
$hlbm[$oldbg] = $newbg;
}
// Now let's change the title, if we're allowed to
return str_replace(array_keys($hlbm), array_values($hlbm), $buffer);
}


Also tell me whether this is safe or otherwise not advisable etc. Any ideas?

SA™

$message wont be available in the buffer. Some times it is just better to use edits instead of hooks.
http://samods.github.io/SAChatBar/

Xbox Live: smokerthecheese 360 or xbone
My Work
Piano Movers / Delivery service
QuoteMy allies are dead.
I'm 'bout to be too.
Zombies are chasing me.
F*** it, I'm screwed -___-

Hj Ahmad Rasyid Hj Ismail

Thanks. I think so too. Especially, after few $context['html_headers'] inserted via hook are not properly working in some users forum, I begin to realize that I might just have to prepare another version on the mod that modify the relevant file(s) directly instead.

Advertisement: