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

Discussion in 'XenForo Development Discussions' started by cclaerhout, Oct 21, 2012.

1. ### cclaerhoutWell-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'];

2. ### cclaerhoutWell-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'] 3. ### timeActive Member I think : Class: XenForo_DataWriter_DiscussionMessage_Post Function to modify(extend): protected function _messagePreSave() PHP:  <?phpclass 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 ? 4. ### cclaerhoutWell-Known Member Use$this->_existingData
You can check here.

time likes this.
5. ### timeActive Member

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' ?

Of course. Because You manual add text test into $message. time likes this. 7. ### timeActive Member I use PHP:  <?phpclass 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. 8. ### cclaerhoutWell-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 likes this.
9. ### timeActive Member

Thanks so much, that's nice.