Stuck: Need to add a field when creating a thread


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?

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.
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 :(
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.
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.
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.
Top Bottom