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

Stuck: Need to add a field when creating a thread

Discussion in 'XenForo Development Discussions' started by RickM, Nov 2, 2010.

  1. RickM

    RickM Well-Known Member

    Hi all,

    I'm a bit stuck again! I'm trying to add a custom field to the add thread form. Adding it to the template isnt a problem. But I cant for the life of me figure out how to then get that field data inserted into a specific table.

    I cant even find where a thread is inserted! I've been using eclipse to search for 'INTO xf_thread ' thinking it would return the location of the statement, but it only picks up inserts into xf_thread_watch and xf_thread_read.

    Any idea where I can hook into to pull the custom field?

  2. dmnkhhn

    dmnkhhn Active Member

    Take a look at the Datawriters - they do all the saving and updating. :)
  3. Jeremy

    Jeremy XenForo Moderator Staff Member

    You'll need to modify the correct DataWriter class via an event listener, overwrite the save method and add your field, then call the parent:: method so that you can add your field, then get have it passed alone to the parent save function.
  4. RickM

    RickM Well-Known Member

    Been looking through the datawriters - still cant make heads or tales of them. There's no form of query to a database in any of them, so I am assuming they send data off to another part of the system.

    I cant even work out if I should be looking at the thread, discussion or post datawriter as the comments on the files are of no help :/ Really stuck here :(
  5. Jeremy

    Jeremy XenForo Moderator Staff Member

    What exactly is your field for? Is it for a Thread or a Post? Or a Discussion. I forget what discussion is for, but Thread and Post are self explanatory.
  6. RickM

    RickM Well-Known Member

    Its basically an extra field when a thread is being created in a specified forum. However I'm inserting it into a custom table (not the standard 'xf_thread' table).

    I *think* I'm on the right path now. From what I can tell its the 'XenForo_DataWriter_Discussion_Thread' that I need.
  7. Wired

    Wired Active Member

    To clarify a small bit, it's one field per thread in that specific forum.
  8. RickM

    RickM Well-Known Member

  9. RickM

    RickM Well-Known Member

    EDIT: W000t! (That means it's working btw!)

    Ok, for anyone trying to modify thread creations, here's how it works:

    You'll need a code event listener for this.

    In your 'Listener.php' (you dont have to call it this):

    class WebsiteRatings_DataWriter_Listener
        public static function 
    loadClassDataWriter($class, &$extend)
            if (
    $class == 'XenForo_DataWriter_Discussion_Thread')
    $extend[] = 'WebsiteRatings_DataWriter_WebsiteRatings';


    This basically 'hooks' you into the class that creates threads.

    In your own datawriter file, you're now basically going to replace the function that writes the thread to the database. Even though you're replacing it, it will still run as you call into the original function with 'parent::_discussionPostSave($messages);'

    So, here's how my system works. Let me first explain what it does: It inserts a URL (custom field on the thread creation form) into another table. You could replace this with any field you like.

    class WebsiteRatings_DataWriter_WebsiteRatings extends XFCP_WebsiteRatings_DataWriter_WebsiteRatings

        protected function 
    _discussionPostSave(array $messages)
    $db XenForo_Application::get('db');
    $threadId $this->get('thread_id');
    $forumId $this->get('node_id');

    // This if statement is custom for my mod - it checks which forum we're using this modification in, and only runs it on that forum.
            // It also only runs if the rating system is turned on.
    if($forumId == XenForo_Application::get('options')->ratingforum_id && XenForo_Application::get('options')->ratings_enabled == 1)
    // Again - this is custom for my modification and would be different with yours.
    $db->query("INSERT INTO <removed/hidden> (threadid, some, fields) VALUES('".$threadId."', 'testing123', 'another value')");
    Really glad its working now - thanks to all above that helped out! If anyone else gets stuck on this, take a look at the above code or shoot me a message if you are well and truly stuck and I'll help where possible.

Share This Page