Store current user id in comment

I am using the plugin to create comments with kirby. It is only possible to create a comment when you are logged in. If the user has written a comment and clicks on submit, the ID of the user should be written to the comment. Unfortunately it does not work as described below.

Or can one hang in via the controller when the form is submitted? Can you then still submit your own data when creating the comment (the page)?

Config

c::set('comments.hooks.did-save-comment', function ($comments, $comment, $commentPage) {
  $commentPage->update([
    'name' => $kirby->user()->id()
  ]);
});

Plugin

Yes, of course. I don’t see why it would make sense to use a hook here.

BTW. Your syntax is old Kirby 2 syntax.

Can you show me how I can do it with a controller?

If you post the controller you are using. All you have to do is add that information.

Currently I use the comments snippets of this plugin at the blogposts. I don’t have any controller there. So it would be completely new. I don’t know to to do it in this case with the plugin to add more data like the id of the user.

The reason why your code doesn’t work is because $kirby is undefined (if this old syntax works at all).

Hm, I have never used this plugin.

Oh… What is the other way to get the current user?

Ok, looks like you cannot hook into the processing behavior of the plugin and the custom fields in the docs ToC does not seem to be documented or implemented.

So you’d probably really go the hook way.

You can get the Kirby object via the page object:

'name' => ( $user = $comment->kirby()->user() ) ? $user->id() : null,

Since you don’t seem to be using the name field otherwise, here is another option:

Overwrite the form snippet, make the name field hidden and add the current user id as value. Then you don’t need the hook and the value will be automatically processed.

The downside of this approach is that the value can be manipulated, unless you validate it with the validate option.

Good idea - this works very well. Thank you again for your help!