-
Notifications
You must be signed in to change notification settings - Fork 7
Current User Field #93
Comments
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: 1. Hide/remove the name field from the comment formIf you are using the comment form rendered by the plugin (i.e. the .commentions-form-name { display:none; } Alternatively, you could write your own form snippet and omit the 2. Ensure that only authenticated users can submit commentsIn 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.");
}
},
], 3. Use our after-hook to store the ID in the name fieldIn your // 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 4. Display the user information from your database in the comment listingInstead of outputting the default comments list using the // 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.
The text was updated successfully, but these errors were encountered: