Users running a non-download version of Vanilla (pulled from github), on branch release/2019.016 or master from the last 2 weeks should upgrade to release/2019.017 or latest master for security reasons. Downloaded official open sources releases are not affected.
Please upgrade here. These earlier versions are no longer being updated and have security issues.

Content handler before default filters

msikmamsikma New
edited December 2010 in Vanilla 2.0 - 2.8
Hi there, I'm new to Vanilla2 and in order to teach myself the ropes I've started working on a simple BBCode parser plugin.

I ran into a problem when writing the function for [img] tags. Normally you'd format the [img] tag like this:
[img]http://link-to-image.com/img.jpg[/img]
The problem is that by BBCode parsing script is performed *after* the regular filters have been applied, so the [img] tag receives an HTML link as the content instead of just a plain text image URI.

Here's my code: http://pastie.org/1407521

<?php if (!defined('APPLICATION')) exit();

$PluginInfo['SaltCode'] = array(
'Name' => 'SaltCode',
'Description' => 'Blha blah blah',
'Version' => '1.0',
'Author' => 'Michiel Sikma',
'AuthorEmail' => '[email protected]',
'AuthorUrl' => 'http://wedemandhtml.com/',
'License' => 'GNU GPLv2'
);

/*TODO You can set up some options here. */
$options = array(
/* Convert two
tags to wraps. */
'br_to_p' => true,

/* Removes superfluous
tags if there are more than two consecutive ones. */
'trim_br' => false,

/* Avoid using browser-specific CSS prefixes. */
'avoid_css_prefix' => false,
);

/* Install the SaltCode class as a singleton. */
Gdn::FactoryInstall('SaltCodeFormatter', 'SaltCode', dirname(__FILE__).DS.'classSaltCode.php', Gdn::FactorySingleton);
Gdn::Factory('SaltCodeFormatter')->Setup('saltcode', $options);

class SaltCodePlugin extends Gdn_Plugin
{
/**
* Format each comment using the SaltCode class.
*/
public function DiscussionController_AfterCommentFormat_Handler(&$Sender)
{
$Sender->EventArguments['Object']->FormatBody = Gdn::Factory('SaltCodeFormatter')->Filter($Sender->EventArguments['Object']->FormatBody);
}

/**
* Only performed on enable.
*/
public function Setup()
{
SaveToConfig('Plugins.SaltCode.Enabled', TRUE);
}

/**
* Only performed on disable.
*/
public function OnDisable()
{
SaveToConfig('Plugins.SaltCode.Enabled', FALSE);
}
}
I would assume that the DiscussionController_AfterCommentFormat_Handler needs to be something else, but what? (I got that function name from another plugin.)

Thanks!

Comments

  • Okay, I figured out that these events are fired in the discussion/helper_functions.php file of the Vanilla views. From there I was able to find the right event that I needed: BeforeCommentBody

    I actually tried that before but it didn't work. The reason, however, was that the FormatBody attribute isn't available in the $Sender->EventArguments['Object'] at that time; it's just Body, which gets copied to FormatBody later.
    	public function DiscussionController_BeforeCommentBody_Handler(&$Sender)
    {
    $Sender->EventArguments['Object']->Body = Gdn::Factory('SaltCodeFormatter')->Filter($Sender->EventArguments['Object']->Body);
    }
Sign In or Register to comment.