XF 2.0 How to create new widget definition?

Tipmoose

Member
I am running into some sort of pathing problem while trying to create a new widget/widget definition. Im creating the widget class in my addons folder under AntlerApps/MostUsersOnline/XF/Widget. The widget I am creating has to extend XF\Widget\AbstractWidget. Any attempt to reference the XF version of AbstractWidget from my class fails with the below error. I have tried setting up class extensions for AbstractWidget to my widget. Same error. The *only* thing that will make this work is if I have an AbstractWidget.php file (and class) in the same location as my own widget. That can't be the right way to do this.

Are there any special rules needed for creating ones own widget that I'm not following? I have to be making some kind of basic error.

The error I am getting is:
Fatal error: Class 'AntlerApps\MostUsersOnline\XF\Widget\AbstractWidget' not found in /var/www/public/xenforo/src/addons/AntlerApps/MostUsersOnline/XF/Widget/MostUsersOnline.php on line 5

Here is the content of my widget class

<?php

namespace AntlerApps\MostUsersOnline\XF\Widget;

class MostUsersOnline extends AbstractWidget
{
}
 
I wouldn’t recommend putting the class in the XF folder as that’s really only best practice when it comes to core classes which are extended rather than new features that have to extend our Abstract classes. Though that’s just a minor point.

The error you’re seeing is because you’ve indicated the code should be in the AntlerApps\MostUsersOnline\XF\Widget namespace so when you write extends AbstractWidget it will look for the AbstractWidget class inside that namespace and it doesn’t exist there hence the error.

Changing the code to extends \XF\Widget\AbstractWidget will solve the problem.
 
I wouldn’t recommend putting the class in the XF folder as that’s really only best practice when it comes to core classes which are extended rather than new features that have to extend our Abstract classes. Though that’s just a minor point.

The error you’re seeing is because you’ve indicated the code should be in the AntlerApps\MostUsersOnline\XF\Widget namespace so when you write extends AbstractWidget it will look for the AbstractWidget class inside that namespace and it doesn’t exist there hence the error.

Changing the code to extends \XF\Widget\AbstractWidget will solve the problem.

That makes sense as to why the base class isn't being picked up, but my widget isn't in the base class's namespace. Its in my add on's name space. Shouldn't I put all of my objects into my own namespace and derive/extend the base classes in XF's namespace?
 
That makes sense as to why the base class isn't being picked up, but my widget isn't in the base class's namespace. Its in my add on's name space. Shouldn't I put all of my objects into my own namespace and derive/extend the base classes in XF's namespace?

Nevermind. I misread your reply.

I had tried to do that but had omitted the starting \. I knew it was something stupid.

Thank you!
 
Back
Top Bottom