How do I change comments before saving them?
-
- Regular
- Posts: 695
- Joined: Tue Jul 03, 2007 3:34 am
- Location: Berlin, Germany
- Contact:
Re: How do I change comments before saving them?
humm... I think I have to switch to the German forum, I seem to be completely unable to make my (very simple) problem clear in the English language.. Thanks.
-
- Regular
- Posts: 695
- Joined: Tue Jul 03, 2007 3:34 am
- Location: Berlin, Germany
- Contact:
Re: How do I change comments before saving them?
Or maybe one last try in English
Please forget everything I posted in this thread before. I have this function in my plugin:
The logfile of the plugin says, that addData was changed the way I would expect it: The addData['comment'] is changed. But the comment will be saved the same way it was before I changed it using this plugin.
Why is it like that, what went wrong?
Please forget everything I posted in this thread before. I have this function in my plugin:
Code: Select all
function event_hook($event, &$bag, &$eventData, &$addData) {
global $serendipity;
$hooks = &$bag->get('event_hooks');
if (isset($hooks[$event])) {
switch($event) {
case 'frontend_saveComment':
$result = $this->checkComment($eventData, $addData);
$this->log("after checkComment: " . print_r($eventData, true) . "\n" . print_r($addData,TRUE));
return $result;
break;
default:
return false;
break;
}
return true;
} else {
return false;
}
}
Why is it like that, what went wrong?
-
- Regular
- Posts: 695
- Joined: Tue Jul 03, 2007 3:34 am
- Location: Berlin, Germany
- Contact:
Re: How do I change comments before saving them?
Oh.. Now I have found the reason of my problem by myself.. It's simple and it is like documented: addData can not be changed in the plugins event_hook, no matter how I define the interface of my event_hook.
The reason is: in plugin_api.inc.php the called function is defined like that:
So addData will be "restored" after all event_hooks are called, as addData is not handed by reference but copied.
Hmm.. Bad.. Why is it like that, that we are not allowed to change a comment before saving it?
So I have to hook into frontend_saveComment_finish to be able to change the comment and resave it. (and I have to make sure, that that does not produce an endless recursion, as saving the comment in frontend_saveComment_finish will emit another frontend_saveComment_finish event)
The reason is: in plugin_api.inc.php the called function is defined like that:
Code: Select all
function hook_event($event_name, &$eventData, $addData = null)
Hmm.. Bad.. Why is it like that, that we are not allowed to change a comment before saving it?
So I have to hook into frontend_saveComment_finish to be able to change the comment and resave it. (and I have to make sure, that that does not produce an endless recursion, as saving the comment in frontend_saveComment_finish will emit another frontend_saveComment_finish event)
Re: How do I change comments before saving them?
Yes, that is what we were trying to say all the timeblog.brockha.us wrote:... addData is not handed by reference but copied.
About changing addData data - I don't get why you want to change usercontent... The plugin api is able to re-act on user actions via eventData (set to moderate, erase, etc.) which should be enough by POST.
Regards,
Ian
Serendipity Styx Edition and additional_plugins @ https://ophian.github.io/ @ https://github.com/ophian
Ian
Serendipity Styx Edition and additional_plugins @ https://ophian.github.io/ @ https://github.com/ophian
-
- Regular
- Posts: 695
- Joined: Tue Jul 03, 2007 3:34 am
- Location: Berlin, Germany
- Contact:
Re: How do I change comments before saving them?
Yes, I knew from the beginning, that this is the case. It is even documented. My question was all the time: why is that the case (or being more precise: what code made it readonly).Timbalu wrote:Yes, that is what we were trying to say all the time
Well.. There are many reasons why you want to do it. Remove URLs, add BRs, what ever.Timbalu wrote:I don't get why you want to change usercontent
In my case I wanted to fetch content for pingbacks.
But I managed to do it in another way.
Re: How do I change comments before saving them?
Well then we did not quite understand each other... No regret, since most of us are still on the way to understand some of these magic S9y internas.
That would be interesting to see here, after so many posts!blog.brockha.us wrote:But I managed to do it in another way.
Regards,
Ian
Serendipity Styx Edition and additional_plugins @ https://ophian.github.io/ @ https://github.com/ophian
Ian
Serendipity Styx Edition and additional_plugins @ https://ophian.github.io/ @ https://github.com/ophian
Re: How do I change comments before saving them?
I'm not offended or something like that, but didn't i answer exactly that question in my very first and with more certainty in my second answer?blog.brockha.us wrote:My question was all the time: why is that the case (or being more precise: what code made it readonly).
But I'm happy you found a way to solve the problem and would also like to encourage you to post your solution, in case someone finds this thread searching for the same question.
-
- Regular
- Posts: 695
- Joined: Tue Jul 03, 2007 3:34 am
- Location: Berlin, Germany
- Contact:
Re: How do I change comments before saving them?
Reading back: Yes, onli, you exactly answered my question, but it seems, I didn't understand it when first reading. (to be honest I can't even remember seeing a post of you so early, I remember talking to Ian only. Perhaps I read it over.. )
About my problem: The problem described here can't be solved, as it is a feature not a problem (although I still think it is a problematic feature in this case). Well, to tell the complete truth: It can be done, but it is tricky. You have to hook the commentSaved event (not the saveComment), save the changed comment on your own again and prevent a recursion by adding and checking a "source" attribute to the addData i.e.
But I used a completely other way to fetch pingback content now: I patch the Serendipity config. I added a functionality to the core doing this some years ago, but it is hidden to the "normal user" (you have to patch the local config file manually).
About my problem: The problem described here can't be solved, as it is a feature not a problem (although I still think it is a problematic feature in this case). Well, to tell the complete truth: It can be done, but it is tricky. You have to hook the commentSaved event (not the saveComment), save the changed comment on your own again and prevent a recursion by adding and checking a "source" attribute to the addData i.e.
But I used a completely other way to fetch pingback content now: I patch the Serendipity config. I added a functionality to the core doing this some years ago, but it is hidden to the "normal user" (you have to patch the local config file manually).