What events to choose in code listener

  • Thread starter Thread starter account8226
  • Start date Start date
A

account8226

Guest
Hello,

I would like to create an add-on that would do something when sO posts a new thread, how do I have to choose the appropriate "event" when creating my listener ?

Regards.
 
There's a few options actually.

You could extend the controller responsible for creating a new thread.

You can usually identify the controller by looking at the URL:
http://xenforo.com/community/forums/xenforo-development-discussions.34/create-thread

XenForo URLs are usually split into a few parts. The first is the board URL (http://xenforo.com/community) then the route (forums), then the data (forum name . forum ID in the above example) then the action (create-thread).

So you can tell by looking at the URL that you will probably be looking to alter:

library/XenForo/ControllerPublic/Forum.php

In there you will find a public function called actionCreateThread. That, at least, is the code that renders the view for creating a new thread (so before the thread is created). If you want to do something after the thread is created you will need the actionAddThread. (This is the form action, if you look at the source of the Create Thread page, you will see the form action is forum/add-thread).

So, ultimately you will be wanting to extend that controller action.

Another alternative is to look at the DataWriter.

The add-thread controller action instantiates the Thread DataWriter and the Post Data Writer. This interacts with the database and inserts the thread in the database.

All DataWriters have a pre and postSave function that you can extend. The postSave function can be useful because by this point, the thread has definitely been created, and you have access to things such as the thread ID and first post ID which you wouldn't necessarily be able to get very easily from the Controller.

With all this in mind, you will be looking to play with either the load_class_controller event or load_class_datawriter event :)
 
There's a few options actually.

You could extend the controller responsible for creating a new thread.

You can usually identify the controller by looking at the URL:
http://xenforo.com/community/forums/xenforo-development-discussions.34/create-thread

XenForo URLs are usually split into a few parts. The first is the board URL (http://xenforo.com/community) then the route (forums), then the data (forum name . forum ID in the above example) then the action (create-thread).

So you can tell by looking at the URL that you will probably be looking to alter:

library/XenForo/ControllerPublic/Forum.php

In there you will find a public function called actionCreateThread. That, at least, is the code that renders the view for creating a new thread (so before the thread is created). If you want to do something after the thread is created you will need the actionAddThread. (This is the form action, if you look at the source of the Create Thread page, you will see the form action is forum/add-thread).

So, ultimately you will be wanting to extend that controller action.

Another alternative is to look at the DataWriter.

The add-thread controller action instantiates the Thread DataWriter and the Post Data Writer. This interacts with the database and inserts the thread in the database.

All DataWriters have a pre and postSave function that you can extend. The postSave function can be useful because by this point, the thread has definitely been created, and you have access to things such as the thread ID and first post ID which you wouldn't necessarily be able to get very easily from the Controller.

With all this in mind, you will be looking to play with either the load_class_controller event or load_class_datawriter event :)

And let's imagine I would like when a member click the "create thread" button to check if the thread title contain the word "LMAO" (just as an example), would'nt it be easier to directly change the /XenForo/ControllerPublic/Forum.php actionAddThread() method ?
 
"Easier to directly change" as in, edit the core file?

The AddThread method is definitely a good one to extend with an add-on as you can pull out the thread title from the posted input. But it depends what you're doing after "LMAO" has been detected in the title.

One other good area to extend is in the DataWriter. You could extend the "_verifyTitle" function.

This function specifically looks at the title of each thread being posted. And right now all it does is change the case of the title based on the Admin CP option to adjust the title letter case.

You could extend that to do something if the title contains "LMAO".

There's basically no right answer usually. Often you can do things in multiple ways.
 
Okay, so I have actions happening on thread creation by extending the addThread method, but let's say the I want to write to another DB the threadId, post contents, etc how would I go about it.
 
You can connect to any database using some handy adapters provided by the Zend Framework.

The below connects to a MySQL database called 'your_database' on localhost.

Below that, it uses the $mydb object to insert a new row into 'your_table' with various parameters. The array is column => value pairs.

So you don't even need to write a real query. Nice and easy :)

PHP:
			$mydb = new Zend_Db_Adapter_Pdo_Mysql(array(
				'host'     => 'localhost',
				'username' => 'root',
				'password' => 'your_password',
				'dbname'   => 'your_database'
			));
			
			$mydb->insert('your_table', array(
				'thread_id' => $threadId,
				'thread_title' => $threadTitle,
				'first_post_id' => $firstPostId,
				'first_post_content' => $firstPostContent
			));

Connecting to a database tutorial source: http://xenforo.com/community/thread...ent-db-and-display-them-in-a-page-node.42311/
 
Connection and insertion is not the issue, I can't find the exact location for this code injection.
 
Top Bottom