XF 2.0 Newbie with php :) and Xenforo dev Listeners (trying to grasp the idea)

ludak

Active member
This might be a newbie question but just reading though the documentation and trying to grasp the listeners concept. I have extensive development skills, just not in php and started playing with php just few days ago as we needed some add-ons for our community. Maybe some of the concepts are different and maybe I am just overthinking the listeners, but here is what I am wondering.

Are the listeners really suppose to be used when we are changing the default xenforo entities? For example, I would like my add-on to have additional information in User Entity. So I want to add a column into the xf_user table called my_addon_new_column.

Then I would have to create a listener in order to extent the User structure and have the column available to me in the code? Am I getting this correctly?

Another question would be, if I did not change the User table, but just created a new table for my addon that has a foreign key to the UserID, so that I can access the user that way, would I really then need a listener anymore? I am guessing no, since I am creating the new entity, I can just extend from \XF\Mvc\Entity\Entity ?

Sorry for the newbie questions, just trying to grasp the ideas before writing more complex stuff out. I like to be on the right path so that I have better code structure and coding standards used, for easier maintnance.

Thanks
 
Although it is possible to override getStructure() when you are extending an entity class, the preferred way is to use the event listener:

I get this, but I am wondering what if I do not want to change the entity at all, just create my own entity and join to it. (using with), I guess I do not need a listener then, I can just join them together and use.

I guess I am just trying to stay away as much as possible changing the out of the box xenforo stuff(so that in the case of the changes in future its more independant add-on)
 
Last edited:
I get this, but I am wondering what if I do not want to change the entity at all, just create my own entity and join to it. (using with), I guess I do not need a listener then, I can just join them together and use.
You would still need to create a listener for the structure and add a relation to the entity to use a 'with' when getting the XenForo entity.

Code:
public static function postEntityStructure(\XF\Mvc\Entity\Manager $em, \XF\Mvc\Entity\Structure &$structure)
{
   $structure->relations['YourRelationName'] = [
      'entity' => 'Your\Addon:Entity',
      'type' => entity::TO_ONE,
      'conditions' => 'your_condition',
      'primary' => true
   ];
}
Then if you were extending the Post entity as above, you could use a $finder->with('YourRelationName') when getting the post entity.

It's for this reason most people just extend the entity itself using $structure = parent::getStructure($structure); and adding to/returning the structure that way.

EDIT: Although many of us directly extend the XF entities, I can see the listener being the preferred way because if you disable listeners your extension won't fire. Whereas the direct extensions most likely would fire unless you disable the actual add-on itself. Either way has it's drawbacks and repercussions.
 
Last edited:
AFAIK, disableListeners in config.php also disables class extensions so that doesn't really make a difference.
 
And I just confirmed the class extentions do get disabled when listeners are disabled. ;)

So you can ignore my edit a couple of posts back.
 
Top Bottom