Closed timhartmann closed 1 year ago
Hi @timhartmann - thanks for sharing your scenario. This is valuable food for thought regarding future development.
Storing logged in users' id is an idea to be evaluated for a future update. However, since your project apparently deals solely with authenticated users (no mix of unauthenticated and logged in commentators), this can be achieved with the current design as follows:
If you are using the comment form rendered by the plugin (i.e. the commentions()
or commentions('form')
helpers), you could hide the name field using CSS. This is easiest, and since the name will be replaced anyway (see step below), the theoretical possibility for a user to submit a name is not an issue here (just an inconvenience for the edge case of a user having CSS disabled).
.commentions-form-name { display:none; }
Alternatively, you could write your own form snippet and omit the name
field entirely. Just make sure that name
is not defined as a compulsory field in your setup as otherwise the submission will fail due to the empty field.
In addition to displaying the comment form to logged-in users only, you also want to have a check in place before processing a submission (in theory, a user could have figured out the POST request required to submit comments to the site):
// in site/config.php
'hooks' => [
'commentions.add:before' => function ($page, $data) {
if(!kirby()->user()) {
throw new Exception("Only logged-in users are allowed to comment.");
}
},
],
In your site/config.php
, configure the following hook to alter the stored form data after submission:
// in site/config.php
'hooks' => [
'commentions.add:after' => function ($page, $data) {
$uid = $data['uid'];
$newdata = [
'name' => kirby()->user()->id() ?? '',
];
$page->updateCommention($uid, $newdata);
},
],
Now all comments submitted will be stored with the Kirby user ID as name
.
Instead of outputting the default comments list using the commentions()
or commentions('list')
helpers, you can use the $page->commentions() method to write your own rendering of the comments list:
// in the page template or a snippet
foreach ($page->commentions() as $item) {
// get the user object
$user = $kirby->user($item->name());
// output the user name as stored in their account
echo $user->name()->value();
// output the comment itself as desired
print_r($item->content());
}
PS: All of the above assumes you are not using webmentions on your site (as they are not comments from logged-in users), but could probably be developed further to accommodate those as well.
Thanks for your help, Sebastian! So I could solve my problem for now and thus pull the data from the current user. Maybe it is still worth the feature at some point to consider logged in users directly.
Otherwise, thank you for your exceptionally good work. Everything looks totally clean and structured. I hope that you will continue to develop the existing plugins and many new ones will come from you - very enriching! :)
I have the case that I show the comment area only when the user is logged in. So he doesn't need to enter the name, email etc. yet. Also, this would be error prone in this case, because he can change the data in his profile. Only the ID does not change.
It would be nice if the data would be extended and the generated comment contains the ID of the current user. On the basis of this, further content such as name can be filled in the comment listing by default.