How do I change comments before saving them?

Creating and modifying plugins.
blog.brockha.us
Regular
Posts: 695
Joined: Tue Jul 03, 2007 3:34 am
Location: Berlin, Germany
Contact:

Re: How do I change comments before saving them?

Post by blog.brockha.us »

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.
- Grischa Brockhaus - http://blog.brockha.us
- Want to make me happy? http://wishes.brockha.us/
blog.brockha.us
Regular
Posts: 695
Joined: Tue Jul 03, 2007 3:34 am
Location: Berlin, Germany
Contact:

Re: How do I change comments before saving them?

Post by blog.brockha.us »

Or maybe one last try in English ;)
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;
        }
    }
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?
- Grischa Brockhaus - http://blog.brockha.us
- Want to make me happy? http://wishes.brockha.us/
blog.brockha.us
Regular
Posts: 695
Joined: Tue Jul 03, 2007 3:34 am
Location: Berlin, Germany
Contact:

Re: How do I change comments before saving them?

Post by blog.brockha.us »

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:

Code: Select all

function hook_event($event_name, &$eventData, $addData = null)
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) :?
- Grischa Brockhaus - http://blog.brockha.us
- Want to make me happy? http://wishes.brockha.us/
Timbalu
Regular
Posts: 4598
Joined: Sun May 02, 2004 3:04 pm

Re: How do I change comments before saving them?

Post by Timbalu »

blog.brockha.us wrote:... addData is not handed by reference but copied.
Yes, that is what we were trying to say all the time ;-)

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
blog.brockha.us
Regular
Posts: 695
Joined: Tue Jul 03, 2007 3:34 am
Location: Berlin, Germany
Contact:

Re: How do I change comments before saving them?

Post by blog.brockha.us »

Timbalu wrote:Yes, that is what we were trying to say all the time ;-)
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). :wink:
Timbalu wrote:I don't get why you want to change usercontent
Well.. There are many reasons why you want to do it. Remove URLs, add BRs, what ever.

In my case I wanted to fetch content for pingbacks.
But I managed to do it in another way.
- Grischa Brockhaus - http://blog.brockha.us
- Want to make me happy? http://wishes.brockha.us/
Timbalu
Regular
Posts: 4598
Joined: Sun May 02, 2004 3:04 pm

Re: How do I change comments before saving them?

Post by Timbalu »

Well then we did not quite understand each other... :wink: No regret, since most of us are still on the way to understand some of these magic S9y internas.
blog.brockha.us wrote:But I managed to do it in another way.
That would be interesting to see here, after so many posts!
Regards,
Ian

Serendipity Styx Edition and additional_plugins @ https://ophian.github.io/ @ https://github.com/ophian
onli
Regular
Posts: 2825
Joined: Tue Sep 09, 2008 10:04 pm
Contact:

Re: How do I change comments before saving them?

Post by onli »

blog.brockha.us wrote:My question was all the time: why is that the case (or being more precise: what code made it readonly).
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?

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.
blog.brockha.us
Regular
Posts: 695
Joined: Tue Jul 03, 2007 3:34 am
Location: Berlin, Germany
Contact:

Re: How do I change comments before saving them?

Post by blog.brockha.us »

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).
- Grischa Brockhaus - http://blog.brockha.us
- Want to make me happy? http://wishes.brockha.us/
Post Reply