XF 2.2 Recommended approach for changing post author on the fly?

asprin

Active member
As part of an addon that I'm building, I would need to change the owner/author of every post (to some set defined user_id) whenever a new one is made and this should be applicable to only a specific forum (the forum_id will be taken from the ACP as part of addon's option). My solution to this is as follows:

1. Create a new entity_pre_save code event listener
2. Use a hint of XF\Entity\Post to restrict it to only posts
3. In the static method, get the forum_id from the addon's options and use that inside an if condition to ensure that the code runs only if the new post is being made in the specified forum (At this point, I'm not sure if I'll have access to that info)
4. Put the code to modify the post owner inside this if block

Is this a good approach or is there a better way of achieving the goal?
 
Solution
Extend XF\Pub\Controller\Thread override setupThreadReply, call the parent first to get the generated Replier then do your checks for whether or not you want to change the post author. If you do, call Replier#setUser to set the post author to whichever user you want and return the Replier.

This way, you're changing the behavior at the location in the code where the original behavior is actually defined, which makes project organization a lot clearer. Plus, you have all of the data you'd need, such as the thread that's being replied to and thus the forum in which the thread exists.
Extend XF\Pub\Controller\Thread override setupThreadReply, call the parent first to get the generated Replier then do your checks for whether or not you want to change the post author. If you do, call Replier#setUser to set the post author to whichever user you want and return the Replier.

This way, you're changing the behavior at the location in the code where the original behavior is actually defined, which makes project organization a lot clearer. Plus, you have all of the data you'd need, such as the thread that's being replied to and thus the forum in which the thread exists.
 
Solution
Back
Top Bottom