Modify datas from an existing controller (or datawriter) before there are sent to the Database

cclaerhout

Well-known member
I would like to modify the datas sent when a user creates a post or reply to one before they are registered inside the Database.

I see two ways of doing it: extend the controller or the datawriter. P.S: I don't want to override the function, but to extend it.

#Controller solution?
Is there a way to call the parent function and modify it without saving twice the datas inside the db?

PHP:
function actionSave(){
$parent = function actionSave();// => save first
//... my code
// => datawritter => save twice... not good
#Datawriter solution?
Can I modify the message content from here?:
PHP:
    /**
    * Designed to be overridden by child classes
    */
    protected function _messagePreSave()
    {
    }
Any advices will be welcome :)


Edit:
It seems I should be able to play with $this->_existingData['xf_post']['message'] from _messagePreSave() function.

Edit2: failure of previous edit

Edit3: Seems I need to play both with $this->_existingData['xf_post']['message'] AND
$this->_newData['xf_post']['message'];
 

cclaerhout

Well-known member
The solution seems to be the following one:
Class: XenForo_DataWriter_DiscussionMessage
Function to modify(extend): protected function _messagePreSave()
Variable: $this->_newData['xf_post']['message']
 

time

Active member
I think :
Class: XenForo_DataWriter_DiscussionMessage_Post
Function to modify(extend): protected function _messagePreSave()

PHP:
<?php
class LilTee_AutoChangeString_DataWriter_DiscussionMessage_Post extends XFCP_LilTee_AutoChangeString_DataWriter_DiscussionMessage_Post
{
    protected function _messagePreSave()
    {
        $post = $this->getMergedData();
        $post['message'] = $post['message'].' test';
        $newMessage = $post['message'];
        $this->set('message', $newMessage);
    }
}
That's work.
When I post content : Hello world
It's change to : Hello world test
But have a problem : When I edit this post and save it, 'Hello world test' change to 'Hello world test test'
Can you solution it, @cclaerhout ?
 

time

Active member
Use $this->_existingData
You can check here.
Can you explain the details? I don't understand.
I'm learning PHP Basic, so i don't know more about PHP.
How to modify the datas sent when a user creates a post or reply to one before they are registered inside the Database?
Example, how to convert string 'cos' to '\cos' ?
 

truonglv

Formerly Nobita.Kun
I think :
Class: XenForo_DataWriter_DiscussionMessage_Post
Function to modify(extend): protected function _messagePreSave()

PHP:
<?php
class LilTee_AutoChangeString_DataWriter_DiscussionMessage_Post extends XFCP_LilTee_AutoChangeString_DataWriter_DiscussionMessage_Post
{
    protected function _messagePreSave()
    {
        $post = $this->getMergedData();
        $post['message'] = $post['message'].' test';
        $newMessage = $post['message'];
        $this->set('message', $newMessage);
    }
}
That's work.
When I post content : Hello world
It's change to : Hello world test
But have a problem : When I edit this post and save it, 'Hello world test' change to 'Hello world test test'
Can you solution it, @cclaerhout ?
Of course. Because You manual add text `test` into $message. :)
 

time

Active member
Of course. Because You manual add text `test` into $message. :)
I use
PHP:
<?php
class LilTee_AutoChangeString_DataWriter_DiscussionMessage_Post extends XFCP_LilTee_AutoChangeString_DataWriter_DiscussionMessage_Post
{
    protected function _messagePreSave()
    {
        $post = $this->getMergedData();
        $post['message'] = preg_replace('/cos/', '\cos', $post['message']);
        $newMessage = $post['message'];
        $this->set('message', $newMessage);
    }

}
To change 'cos' to '\cos' when member post a new thread or reply, but when I edit this post and save it, '\cos' change to '\\cos', I don't wan't it. Can you help me to fix that? Thanks.
 

cclaerhout

Well-known member
PHP:
if($this->_existingData)
{
   /*Data exists, so it's an edit*/
   return parent::_messagePreSave();
}

/*Data doesn't exit, so it's a new post*/

//Put your code here
Check the link I gave you.
 

time

Active member
PHP:
if($this->_existingData)
{
   /*Data exists, so it's an edit*/
   return parent::_messagePreSave();
}

/*Data doesn't exit, so it's a new post*/

//Put your code here
Check the link I gave you.
Thanks so much, that's nice.
 
Top