XF 2.2 Custom widget help

grantus

Active member
I created my widget definition called "test_widget".

I created a template called "test_widget_tpl".

Here's my code:

Code:
namespace Test\ILL\Widgets;
use XF\Widget\AbstractWidget; 

class TestWidget extends AbstractWidget {

    protected $defaultOptions = [];

    public function render() {
        $stmt = \XF::finder('Test\ILL:Battles')
            ->where('status', 'active')
            ->order('battle', 'desc')
            ->limit(5)
            ->fetch();

        $viewParams = [
            'battles' => $stmt
        ];

        return $this->renderer('test_widget_tpl', $viewParams);

   }

    public function verifyOptions(\XF\Http\Request $request, array &$options, &$error = null) {
        return true;
    }

}

When adding my widget, I set the method to "render" but I get the error that it's not static. When I change it to static it just gives an error to try again later.

What could be the issue?
 
It's the same as other callbacks in XenForo in that it only accepts a static method. You're passed the \XF\Widget\PhpCallback instance as an argument and expected to return a string, so you could rewrite your class with that in mind. However if it's for an add-on and the widget is meant to be reusable, creating a definition is what allows for this.

For example, the PHP callback widget is itself a widget definition. Each widget definition is tied to \XF\Widget\AbstractWidget sub-classes, and each widget an instance of that class.
 
It's the same as other callbacks in XenForo in that it only accepts a static method. You're passed the \XF\Widget\PhpCallback instance as an argument and expected to return a string, so you could rewrite your class with that in mind. However if it's for an add-on and the widget is meant to be reusable, creating a definition is what allows for this.

For example, the PHP callback widget is itself a widget definition. Each widget definition is tied to \XF\Widget\AbstractWidget sub-classes, and each widget an instance of that class.
Ah, ok I get it. I just added it as a widget definition instead and it works fine. There was an error about and admin template so I created that and the error went away. Thanks for the tip!
 
Back
Top Bottom