Skip to content
This repository has been archived by the owner on Nov 29, 2023. It is now read-only.

Current User Field #93

Closed
timhartmann opened this issue Jan 9, 2021 · 3 comments
Closed

Current User Field #93

timhartmann opened this issue Jan 9, 2021 · 3 comments
Assignees
Labels
☑️ done Ready for beta, but not released yet
Milestone

Comments

@timhartmann
Copy link

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.

@sebastiangreger
Copy link
Owner

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 form

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.

2. Ensure that only authenticated users can submit comments

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.");
    }
  },
],

3. Use our after-hook to store the ID in the name field

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.

4. Display the user information from your database in the comment listing

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.

@timhartmann
Copy link
Author

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! :)

@sebastiangreger sebastiangreger added the future ideas Backlog of potential ideas for later label Feb 16, 2021
@sebastiangreger sebastiangreger added ☑️ done Ready for beta, but not released yet and removed future ideas Backlog of potential ideas for later labels Apr 29, 2021
@sebastiangreger sebastiangreger self-assigned this Apr 29, 2021
@sebastiangreger sebastiangreger added this to the 2.0 milestone Apr 29, 2021
@sebastiangreger
Copy link
Owner

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
☑️ done Ready for beta, but not released yet
Projects
None yet
Development

No branches or pull requests

2 participants