Parse BBcode in Model instead of View?

Jaxel

Well-known member
I know I can parse bbcode in a text field in the view public using this:
Code:
$bbCodeParser = new XenForo_BbCode_Parser(XenForo_BbCode_Formatter_Base::create('Base', array('view' => $this)));
$field['html'] = new XenForo_BbCode_TextWrapper($field['text'], $bbCodeParser);

Is it possible to parse the bbcode for a string in the model instead of the view?
 
Here may be working :)
PHP:
$request = new Zend_Controller_Request_Http();
$response = new Zend_Controller_Response_Http();

$dependencies = new XenForo_Dependencies_Public();

$viewRenderer = new XenForo_ViewRenderer_Abstract($dependencies, $response, $request);
$view = new XenForo_View($viewRenderer, response);

$bbCodeParser = new XenForo_BbCode_Parser(XenForo_BbCode_Formatter_Base::create('Base', array('view' => $view)));
$field['html'] = new XenForo_BbCode_TextWrapper($field['text'], $bbCodeParser);
 
You only need to pass in a View, when dealing with bbcode which uses templates. You can also reuse the View object and formatter object between XenForo_BbCode_Parser instances.

This is done a bunch of time in the XenForo codebase.

Code:
public function sendNotificationToWatchUsersOnReply(array $reply, array $thread = null, array $noAlerts = array())
{
...
                if (!isset($reply['messageText']) && $parseBbCode)
                {
                    $bbCodeParserText = XenForo_BbCode_Parser::create(XenForo_BbCode_Formatter_Base::create('Text'));
                    $reply['messageText'] = new XenForo_BbCode_TextWrapper($reply['message'], $bbCodeParserText);

                    $bbCodeParserHtml = XenForo_BbCode_Parser::create(XenForo_BbCode_Formatter_Base::create('HtmlEmail'));
                    $reply['messageHtml'] = new XenForo_BbCode_TextWrapper($reply['message'], $bbCodeParserHtml);
                }
...
}
 
Last edited:
You only need to pass in a View, when dealing with bbcode which uses templates.
Some bbcodes only parse if they have a $view. So when you parse the bbcode in a model, it doesnt parse specific codes.
 
Some bbcodes only parse if they have a $view. So when you parse the bbcode in a model, it doesnt parse specific codes.
Parse the Bb Codes in the view, that will be the cleanest and safest solution. There are some workarounds, but I don't see why you would have to use it if you code something yourself and not try to extend a XenForo feature that parse the Bb Codes without the view (as Xon said, it happens sometimes).

If you do need a workaround, play with the controller using the controller pre view listener, generate the view (before XenForo generates it just after ; which means this task would be done twice instead of once)
PHP:
protected static $_view;

public static function controllerPreView(XenForo_FrontController $fc,
   XenForo_ControllerResponse_Abstract &$controllerResponse,
   XenForo_ViewRenderer_Abstract &$viewRenderer,
   array &$containerParams
)
{
   if(!$viewRenderer instanceof XenForo_ViewRenderer_Json)
   {
     $response = $fc->getResponse();
     self::$_view = new XenForo_ViewPublic_Base($viewRenderer, $response, $containerParams);
   }
}
Once you get the view, just save it somewhere. You can't set a session variable [XenForo_Application::getSession()->set('myVariable', $data);] ; this pre view listener occurs too late ; it can only be done during the controller pre dispatch. But you should be able to set this variable using a XenForo application [XenForo_Application::set('myVariable', $data);]. Again, this workaround should be avoided.
 
Back
Top Bottom