Tagging in comments system?

This is a speculative question from a novice - for context, I have created a website that is essentially a digital programme for an art exhibition, users can click an image which opens up a modal with information about a piece and it’s artist. The website also has a comments page where visitors can leave comments about the exhibition, for this I have used the Kirby Comments plugin.

I was wondering how to approach a system where the user could tag a piece of work in exhibition if they wanted to leave a comment about a specific piece. This could work in similar to way to @ works on here or on social media platforms or even just a select element. In turn there would be a comment counter and links to the comments in the information modal of that particular piece. Is there something inherent in the Kirby API which would help me make this?

Just to make sure I get this right: You haven’t set up comments for each piece of art, but just a general comments page where all visitor comments are stored?

Yes, that is correct. It it’s a single page for all the comments.

Kirby Comments version 1.5 now features custom fields. You could create a custom field which stores the URL of the artwork (or some other kind of ID).

Add this to your config.php:

c::set('comments.custom-fields', array(
  array('name' => 'artwork_id')
));

and place a field in the comments form:

<input name="artwork_id" value="<?= $comments->customFieldValue('artwork_id') ?>" type="text">

You can also define a default value of the field by linking from the artwork to the comments page with a special HTTP GET parameter and set it as a default value:

<?php
$default = isset($_GET['from_id']) ? $_GET['from_id'] : '';
?>
<input name="artwork_id" value="<?= $comments->customFieldValue('artwork_id', $default) ?>" type="text">

About linking back to the comments …

I think the value of custom fields can not be searched for using Kirby’s standard find()/filter() methods. What you could do is use the did-save-comment hook, which is called when a comment is successfully saved, grab the ID from the comment, find the artwork page based on the ID and update the counter on that page (or modify it in other ways …)

Here is some untested code as a starting point:

c::set('comments.hooks.did-save-comment', function ($comments, $comment) {
  $artworkId = $comment->customField('artwork_id');
  
  if ($artworkId !== null) {
    $artPage = page('artworks')->find('artwork-'.$artworkId);
    $artPage->update(array(
      'commentscount' => function($count) {
        if ($count == null) {
          $count = 0;
        }
        return $count + 1;
      }
    ));
  }
});

For more on custom fields have a look at the custom fields guide and the API documentation.

2 Likes

Thank you so much for taking the time to write this. I will play around with this and try and implement it onto my site