XF 2.0 Error overriding function in addon

Reid2

Member
I'm trying to override function actionPreview in the Thread controller, and am getting this error:

ErrorException: [E_WARNING] Declaration of SageWebDesign\ThreadPreview\XF\Pub\Controller\Thread::actionPreview() should be compatible with XF\Pub\Controller\Thread::actionPreview(XF\Mvc\ParameterBag $params)

Here is my code:
<?php

namespace SageWebDesign\ThreadPreview\XF\Pub\Controller;

class Thread extends XFCP_Thread
{
/**
* Gets a preview of the last post in a thread.
*
* @return XenForo_ControllerResponse_Abstract
*/
public function actionPreview()
{
$reply = parent::actionPreview();

if ($reply.getParam('post') != false)
{
$reply->setParam('post', $postModel->getPostById($thread['last_post_id'],
array('join' => XenForo_Model_Post::FETCH_USER)));
}

return $reply;
}
}
 
Your method needs to take in a ParameterBag argument:

PHP:
use XF\Mvc\ParameterBag;

// ...

public function actionPreview(ParameterBag $params)
{
    $reply = parent::actionPreview($params);
    // ...
    return $reply;
}
 
I tried that - it doesn't work. The base class function has no argument.
The parent DOES use the parameterBag...
PHP:
public function actionPreview(ParameterBag $params)
{
    $thread = $this->assertViewableThread($params->thread_id, ['FirstPost']);
Therefore, your extended function MUST do the same.

The error itself even tells you that...
ErrorException: [E_WARNING] Declaration of SageWebDesign\ThreadPreview\XF\Pub\Controller\Thread::actionPreview() should be compatible with XF\Pub\Controller\Thread::actionPreview(XF\Mvc\ParameterBag $params)
 
Last edited:
I am using XenForo 2.1.2, and my copy of the Thread Controller has:

public function actionPreview()
{
$threadId = $this->_input->filterSingle('thread_id', XenForo_Input::UINT);
 
It was upgraded to 2.1.2. I am looking at public_html/library/XenForo/ControllerPublic/Thread.php. Is that the right one?
 
XF2+ no longer uses the library/ directory. The correct file is located at src/XF/Pub/Controller/Thread.php.
 
Back
Top Bottom