Installation:
composer config extra.symfony.allow-contrib true
composer require andchir/shopkeeper4-comments
Ensure autoload in composer.json includes:
"psr-4": {
"Andchir\\CommentsBundle\\": "vendor/andchir/shopkeeper4-comments/"
}
Register Bundle:
Add to config/bundles.php:
Andchir\CommentsBundle\CommentsBundle::class => ['all' => true]
Entity Setup:
Extend CommentAbstract for your comment model:
use Andchir\CommentsBundle\Document\CommentAbstract;
class ProductComment extends CommentAbstract { ... }
Repository Implementation:
Implement CommentRepositoryInterface:
use Andchir\CommentsBundle\Repository\CommentRepositoryInterface;
class ProductCommentRepository extends BaseRepository implements CommentRepositoryInterface { ... }
Twig Integration: Add CSS and include the async component:
{% block stylesheets %}
{{ parent() }}
<link href="{{ asset('bundles/comments/css/comments.css') }}" rel="stylesheet">
{% endblock %}
{% include '@Comments/Default/async.html.twig' with {'threadId': 'Product_' ~ product.id} %}
Define a Comment Entity:
// src/Document/ProductComment.php
namespace App\Document;
use Andchir\CommentsBundle\Document\CommentAbstract;
class ProductComment extends CommentAbstract {
// Custom fields (e.g., product reference)
private $productId;
}
Configure Repository:
// src/Repository/ProductCommentRepository.php
namespace App\Repository;
use Andchir\CommentsBundle\Repository\CommentRepositoryInterface;
use Andchir\CommentsBundle\Repository\BaseRepository;
class ProductCommentRepository extends BaseRepository implements CommentRepositoryInterface {
// Override methods as needed (e.g., findByProductId)
}
Render Comments in Twig:
{% include '@Comments/Default/async.html.twig' with {
'threadId': 'Product_' ~ product.id,
'template': '@Comments/Default/product.html.twig' // Custom template
} %}
Admin Menu (Shopker):
Add to config/resources/admin_menu.yaml:
- { title: 'COMMENTS', route: '/module/comments', icon: 'icon-message-circle' }
Creating a Comment:
Use the CommentsManager service to handle creation:
$commentManager = $this->get('comments.manager');
$comment = $commentManager->createComment(
'Product_' . $productId,
$authorId,
$content,
$rating
);
$commentManager->save($comment);
Fetching Comments:
$comments = $commentManager->getCommentsByThread('Product_' . $productId);
Moderation: Use repository methods to filter/moderate:
$pendingComments = $commentRepository->findPendingComments();
Admin Panel:
DocumentRepository with BaseRepository in CommentRepository.php:
class CommentRepository extends BaseRepository implements CommentRepositoryInterface { ... }
Frontend Display:
async.html.twig for lazy-loaded comments.templates/Comments/Default/.Custom Fields:
Add fields to your extended CommentAbstract entity:
class ProductComment extends CommentAbstract {
private $helpfulVotes = 0;
private $productSku;
}
Validation: Override validation in your entity:
use Symfony\Component\Validator\Constraints as Assert;
class ProductComment extends CommentAbstract {
/**
* @Assert\NotBlank
*/
private $content;
}
Events:
Listen to comment events (e.g., comment.created):
$eventDispatcher->addListener('comment.created', function ($event) {
// Log or notify admins
});
Thread ID Format:
threadId is unique and consistent (e.g., Product_123).MongoDB Schema:
DoctrineMongoDBBundle. Ensure your MongoDB schema matches the expected structure:
# config/packages/doctrine_mongodb.yaml
doctrine_mongodb:
connections:
default:
servers:
- mongodb://localhost:27017
document_managers:
default:
auto_mapping: true
Angular Build Issues:
baseHref matches your admin route (e.g., /admin/module/comments/).public/bundles/comments/ directory.Deprecated Code:
DocumentManager directly (deprecated in v1.0.7). Use CommentsManager instead.Missing Comments:
threadId matches the repository queries.php bin/console doctrine:mongo:schema:update --force
Admin Panel Not Loading:
ln -s vendor/andchir/shopkeeper4-comments/frontend/projects/comments/src/app \
public/admin/module/comments/src/app
php bin/console cache:clear
Translation Issues:
config/translations/messages.en.yaml
Custom Templates: Override default Twig templates by copying them from:
vendor/andchir/shopkeeper4-comments/Resources/views/
to:
templates/Comments/Default/
Rating System: Extend the rating logic in your entity:
class ProductComment extends CommentAbstract {
private $rating;
public function getAverageRating() {
// Custom logic to calculate average rating
}
}
Performance:
$comments = $commentRepository->findByThread('Product_123', 0, 10);
Testing:
CommentsManager in unit tests:
$commentManager = $this->createMock(CommentsManager::class);
$commentManager->method('getCommentsByThread')->willReturn([...]);
Upgrades:
ng build comments --configuration production
How can I help you explore Laravel packages today?