Run action on post save

Mikey

Well-known member
I am creating an add on.

I want to run a custom action to save the post ID to a custom table based on an algorithm I am developing.

However, I have looked at XenForo_DataWriter, and I see the _preSave method, and this is where my understanding goes a bit hazy.

I see in XenForo_ControllerPublic_Post the DataWriter is loaded as so
PHP:
XenForo_DataWriter::create('XenForo_DataWriter_DiscussionMessage_Post');

However, I don't know how I'd get the contents of the post by hooking into the _preSave method. Do I need to extend XenForo_ControllerPublic_Post and hook into the _preSave method on the DataWriter somehow?

Then I'll have the post ID? Help :)
 
I just realized that when you said "post save" in the title, you were actually referring to "saving a post" as opposed to "after saving". Though ironically, what you want is post post save. :) (After saving a post.)

You would want to extend the XenForo_DataWriter_DiscussionMessage_Post class and override/extend the _postSave method within that. You'd do this via the load_class_datawriter code event. When you select that, it should give you some rough docs on the class extension system, though there are also plenty of examples in all sorts of XF add-ons.
 
Thanks for the reply, Mike. Yeah, the title was a bit confusing, I thought it was a nice double meaning haha.

Just to clarify - I would add a new Code Event Listener on the Code Event for load_class_datawriter, and then my code event listener would essentially look like

PHP:
class Vendor_AddOn_DataWriter_DataWriter extends XFCP_Vendor_AddOn_DataWriter_DataWriter {

    public function _postSave()
    {
        print_r($message); // gets me post ID? 
    }
}

or would it be more like

PHP:
class Vendor_AddOn_DataWriter_DataWriter extends XenForo_DataWriter_DiscussionMessage_Post {

    public function _postSave()
    {
        print_r($message); // gets me post ID? 
    }
}

Sorry - it's been a while since I worked with XF.
 
Because XenForo_DataWriter_DiscussionMessage_Post has an extra layer of inheritance, I would do this slightly differently than an average data writer, though you will generally get the same result.

Code:
class Vendor_AddOn_DataWriter_DataWriter extends XFCP_Vendor_AddOn_DataWriter_DataWriter
{
    protected function _messagePostSave()
    {
        parent::_messagePostSave(); // It's quite important to make sure you call the original version of this method.

        $this->get('post_id'); // This will contains the post ID at this point.
    }
}

(Untested but should work.)
 
Because XenForo_DataWriter_DiscussionMessage_Post has an extra layer of inheritance, I would do this slightly differently than an average data writer, though you will generally get the same result.

Code:
class Vendor_AddOn_DataWriter_DataWriter extends XFCP_Vendor_AddOn_DataWriter_DataWriter
{
    protected function _messagePostSave()
    {
        parent::_messagePostSave(); // It's quite important to make sure you call the original version of this method.

        $this->get('post_id'); // This will contains the post ID at this point.
    }
}

(Untested but should work.)
As the post a new reply action is on ajax, something like

PHP:
        $post_id = $this->get('post_id');
        print_r($post_id); exit;
Does not execute in the manner I expect.. how would I log the post ID to confirm the method is being fired?

Edit: tried:

PHP:
        XenForo_Helper_File::log('test', $post_id, true);

but nothing is written to internal_data/test.log, does this mean it's not being fired?
 
Last edited:
If the print_r example doesn't work either, then yeah it does likely mean that.
I was panicking for a second there, but it turned out that I had to disable then re-enable the add on for the event listener to seemingly start to fire.. is that normal?

PS, you're up late :)
 
That doesn't seem normal, though I suppose another add-on could interfere with rebuilding the event listener cache.
 
Back
Top Bottom