-
Notifications
You must be signed in to change notification settings - Fork 4
Comment
Comment module is used (obviously) for commenting specific entities. Let's look how it works.
-
Open your application in browser, go to a dashboard and choose Comment/All entities (url=/comment/management/index). Here it is a list of entities that can have comments. By default, there is an entity of type Test with alias test. It means, that if there are created objects of class Test you will be able to comment them. Of course, for testing purposes, we have already created an entity of this class for you (you can see it if you go to a dashboard TestingFeatures/TestUsers url=/test/management/index).
-
Now let's try to make our first comment. Go to a dashboard and choose TestingFeatures/WriteComments. Here you can see a list of entities of class Test and a textarea where you can type a comment. Try to add it and look at the result. It is a base view of comments that includes user's email (cut to 6 characters), a date when he created it, possibilities to edit or delete this comment.
Yes, of course it is possible, but this is disabled by default (for a lot of reasons). To enable this feature, just go to a dashboard Comment/Create entity and create an entity with alias comment and choose Comment\Entity\Comment and mark fields checked to enable visibility and possibility to comment. From this moment all your comments will have a button Answer, try it!
If you have not already understood how to do it, or just forgot something, here are the steps you need to do.
- Step 1 Go to a dashboard Comment/Create entity and create an entity based on your custom made one. So you need to give it an alias and choose it from the list. Then mark as checked needed options
- Step 2 Create an object of your Entity (e.g. add a new row to a table in your db).
- Step 3 Create a controller for displaying this entity, and pass to a viewmodel $commentService = $this->getServiceLocator()->get('Comment\Service\Comment'); Also, pass to a view an array of comments using this service
$comments = $this->getServiceLocator()
->get('Comment\Service\Comment')
->listComments($yourEntity->toArray());
Also, we need to check in the Controller (or in a view, if you want) if comments are visible for your entity. You must to get entityType->isVisible()
- Step 4
In a view somehow display this entity (if you want, of course) and generate a form for entering comments, using method from commentService:
$commentService->generateAddCommentForm($commentService->createForm(), $entityId, $entity); - Step 5 Display comments using view helper
if ($comments && $isVisible !== 0) {
echo $this->comment('comment/index/partial/comment.phtml', $comments);
}; ?>
Here we are checking if we passed comments to a view and if they are visible. Ok, I think that's all!