Simple Machines Community Forum

Customizing SMF => SMF Coding Discussion => Topic started by: Hj Ahmad Rasyid Hj Ismail on September 05, 2014, 11:48:00 PM

Title: Change Text Via Hook File (str_replace)
Post by: Hj Ahmad Rasyid Hj Ismail on September 05, 2014, 11:48:00 PM
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?
Title: Re: Change Text Via Hook File (str_replace)
Post by: SA™ on September 06, 2014, 12:32:07 AM
$message wont be available in the buffer. Some times it is just better to use edits instead of hooks.
Title: Re: Change Text Via Hook File (str_replace)
Post by: Hj Ahmad Rasyid Hj Ismail on September 08, 2014, 09:39:25 AM
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.