1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

What events to choose in code listener

Discussion in 'XenForo Development Discussions' started by account8226, Dec 9, 2012.

  1. account8226

    account8226 Guest


    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 ?

  2. Chris D

    Chris D XenForo Developer Staff Member

    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:

    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:


    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 :)
    AlexT, Hlchia, luutruong and 2 others like this.
  3. account8226

    account8226 Guest

    Thanks you very much for you great awnser Chris, I'm understanding how XF works :) Thanks.
    Chris D likes this.
  4. account8226

    account8226 Guest

    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 ?
  5. Chris D

    Chris D XenForo Developer Staff Member

    "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.
    Jake Bunce and Elly like this.
  6. MOZ

    MOZ Well-Known Member

    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.
  7. Chris D

    Chris D XenForo Developer Staff Member

    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 :)

                $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/
  8. MOZ

    MOZ Well-Known Member

    Connection and insertion is not the issue, I can't find the exact location for this code injection.
  9. MOZ

    MOZ Well-Known Member

    Got it, I was injecting at the wrong position in the addThread method.
  10. Chris D

    Chris D XenForo Developer Staff Member

    That wasn't clear.

Share This Page