XF 2.1 Add json params on view level

Lukas W.

Well-known member
Currently struggling with finding the right entry point for this. Goal is to add a JSON parameter to XF\Pub\Controller\Thread::actionAddReply.

I've previously done so in the controller through calling the setJsonParam() method on the response object, but it seems that the templater isn't fully initialized on controller level yet, so rendering post messages there throws errors in some circumstances. I've attempted to move this to the view class XF\Pub\View\Thread\NewPosts, but as the class doesn't exit, there's no renderJson() method to extend and thus only the default mechanism that converts the HTML response to JSON handles the reply.

It seems the request still gets passed through the renderJson() method I created as a test and afterwards is processed correctly to the reply, but I don't seem to be able to add any json parameters from there without having to recreate the full response myself, which seems a nightmare for compatibility.

Anyone got an idea how to achieve this more gracefully? I've thought about initializing the templater variables at controller level, but I reckon this might also cause compatibility issues.
 
I've worked around this with the following renderJson method for the meantime, but it seems quite suboptimal in terms of compatibility:

PHP:
public function renderJson()
{
    if (method_exists(get_parent_class($this), 'renderJson')) {
        /** @noinspection PhpUndefinedMethodInspection */
        $response = parent::renderJson();
    } else {
        $html = $this->renderTemplate($this->getTemplateName(), $this->getParams());

        $response = [
            'html' => $this->renderer->getHtmlOutputStructure($html)
        ];
    };

    // Append my own response parameters

    return $response;
}
 
I've previously done so in the controller through calling the setJsonParam() method on the response object, but it seems that the templater isn't fully initialized on controller level yet, so rendering post messages there throws errors in some circumstances.
A bit curious what you mean here, as using setJsonParam() seems to be the path of least resistance.
 
A bit curious what you mean here, as using setJsonParam() seems to be the path of least resistance.
As the first post says, the templater isn't fully set up on controller level yet, so you can't reliably render there, thus throwing errors
 
As the first post says, the templater isn't fully set up on controller level yet, so you can't reliably render there, thus throwing errors

This shouldn't be the case. How are you instantiating the templater? You should be able to do $this->app->templater()->renderTemplate('public:<template_name>', $viewParams).

I'd imagine any errors are due to missing view parameters.
 
I don't recall the exact problem that's been there anymore. Looking at my current code, I think it wasn't the templater, but a call to the BB Code renderer, that was casing the errors. @Xon debugged the issue back then and brought this to my attention, along with the recommendation to move the conversion to the view class.
 
Back
Top Bottom