> For the complete documentation index, see [llms.txt](https://xoops.gitbook.io/xoops-modules-cookbook/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://xoops.gitbook.io/xoops-modules-cookbook/best-practices/best-practices/addcomments.md).

# Add Comments

In this tutorial I will explain, how the comment system of XOOPS is working and how you can implement or adapt it.

Pay attention: You do not get notifications about event caused by you. For testing purposes you need minimum two users (one who has subscribed to the event, one who is executing the event).

In this explaination the name of the module is "myexample", we have a table "articles", and the goal is to get notifications concerning events related to this table.

## 1) xoops\_version.php

you have to declare the callback functions, which should be used by your comment system

```
    // ------------------- Comments ------------------- //
    $modversion['hasComments'] = 1;
    $modversion['comments']['pageName'] = 'article.php';
    $modversion['comments']['itemName'] = 'art_id';
    // Comment callback functions
    $modversion['comments']['callbackFile'] = 'include/comment_functions.php';
    $modversion['comments']['callback'] = [
        'approve' => 'myexampleCommentsApprove',
        'update'  => 'myexampleCommentsUpdate',
    ];
```

### Explaination:

'hasComments' defines in general, that this module is using comment system.

With 'pageName' you define, from which pages you can send a comment.

With 'itemName' you define the linked item id for the comment.

In our example the article id from table articles will be linked with the comment and you can send a comment from articles.php

if not existing please add the definition to xoops\_version.php

## 3) File include/comment\_functions.php

As in xoops\_version.php you have to add the file where the lookup functions are defined.

Each time a comment event is happening this function will be called.

In our example we have two functions:

### Update function 'myexampleCommentsUpdate'

This function will be called in each update event. You can add here e.g. the code to update your comments counter in your table

```
    function myexampleCommentsUpdate($itemId, $itemNumb)
    {
        // Get instance of module
        $helper = \XoopsModules\Myexample\Helper::getInstance();
        $articlesHandler = $helper->getHandler('Articles');
        $artId = (int)$itemId;
        $articlesObj = $articlesHandler->get($artId);
        $articlesObj->setVar('art_comments', (int)$itemNumb);
        if ($testfieldsHandler->insert($articlesObj)) {
            return true;
        }
        return false;
    }
```

### Approve function 'myexampleCommentsApprove'

This function will be called in each time a comment is approved. You can add here e.g. the code to notify users about this comment. As a parameter you get the comment object with all necessary information

```
    function myexampleCommentsApprove(&$comment)
    {
        // Notification event
        // Get instance of module
        $helper = \XoopsModules\Myexample\Helper::getInstance();
        $articlesHandler = $helper->getHandler('Testfields');
        $artId = $comment->getVar('com_itemid');
        $articlesObj = $articlesHandler->get($artId);
        $artTitle = $articlesObj->getVar('art_title');

        $tags = [];
        $tags['ITEM_NAME'] = $artTitle;
        $tags['ITEM_URL']  = XOOPS_URL . '/modules/myexample/articles.php?op=show&art_id=' . $artId;
        $notificationHandler = xoops_getHandler('notification');
        // Event modify notification
        $notificationHandler->triggerEvent('global', 0, 'global_comment', $tags);
        $notificationHandler->triggerEvent('articles', $artId, 'articles_comment', $tags);
        return true;

    }
```

## 4) Add comment includes

Your module needs following files, which define what happens in the different comment events:

* comment\_edit.php
* comment\_delete.php
* comment\_post.php
* comment\_reply.php

These files have to contain the include to the corresponding comment file of xoops core system, e.g. comment\_edit.php contains

```
    include_once dirname(dirname(__DIR__)) . '/mainfile.php';
    include_once XOOPS_ROOT_PATH.'/include/comment_edit.php';
```

## 5) Add caller for comment system

Finally we have to add the caller from xoops core in each file where we want to have the comments.

E.g. in our example we add this code in the articles.php:

```
    require_once XOOPS_ROOT_PATH . '/include/comment_view.php';
```

Thats all :)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://xoops.gitbook.io/xoops-modules-cookbook/best-practices/best-practices/addcomments.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
