Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Shopkeeper4 Comments Laravel Package

andchir/shopkeeper4-comments

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. 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/"
    }
    
  2. Register Bundle: Add to config/bundles.php:

    Andchir\CommentsBundle\CommentsBundle::class => ['all' => true]
    
  3. Entity Setup: Extend CommentAbstract for your comment model:

    use Andchir\CommentsBundle\Document\CommentAbstract;
    
    class ProductComment extends CommentAbstract { ... }
    
  4. Repository Implementation: Implement CommentRepositoryInterface:

    use Andchir\CommentsBundle\Repository\CommentRepositoryInterface;
    
    class ProductCommentRepository extends BaseRepository implements CommentRepositoryInterface { ... }
    
  5. 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} %}
    

First Use Case: Displaying Comments on a Product Page

  1. 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;
    }
    
  2. 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)
    }
    
  3. Render Comments in Twig:

    {% include '@Comments/Default/async.html.twig' with {
        'threadId': 'Product_' ~ product.id,
        'template': '@Comments/Default/product.html.twig'  // Custom template
    } %}
    
  4. Admin Menu (Shopker): Add to config/resources/admin_menu.yaml:

    - { title: 'COMMENTS', route: '/module/comments', icon: 'icon-message-circle' }
    

Implementation Patterns

Workflow: CRUD Operations

  1. 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);
    
  2. Fetching Comments:

    $comments = $commentManager->getCommentsByThread('Product_' . $productId);
    
  3. Moderation: Use repository methods to filter/moderate:

    $pendingComments = $commentRepository->findPendingComments();
    

Integration with Shopker

  1. Admin Panel:

    • Replace DocumentRepository with BaseRepository in CommentRepository.php:
      class CommentRepository extends BaseRepository implements CommentRepositoryInterface { ... }
      
    • Build Angular assets (see Development section).
  2. Frontend Display:

    • Use async.html.twig for lazy-loaded comments.
    • Customize templates in templates/Comments/Default/.

Extending Functionality

  1. Custom Fields: Add fields to your extended CommentAbstract entity:

    class ProductComment extends CommentAbstract {
        private $helpfulVotes = 0;
        private $productSku;
    }
    
  2. Validation: Override validation in your entity:

    use Symfony\Component\Validator\Constraints as Assert;
    
    class ProductComment extends CommentAbstract {
        /**
         * @Assert\NotBlank
         */
        private $content;
    }
    
  3. Events: Listen to comment events (e.g., comment.created):

    $eventDispatcher->addListener('comment.created', function ($event) {
        // Log or notify admins
    });
    

Gotchas and Tips

Pitfalls

  1. Thread ID Format:

    • Ensure threadId is unique and consistent (e.g., Product_123).
    • Avoid dynamic IDs (e.g., timestamps) that may change.
  2. MongoDB Schema:

    • The bundle uses 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
      
  3. Angular Build Issues:

    • If Angular assets fail to build, verify:
      • baseHref matches your admin route (e.g., /admin/module/comments/).
      • Permissions for the public/bundles/comments/ directory.
  4. Deprecated Code:

    • Avoid using DocumentManager directly (deprecated in v1.0.7). Use CommentsManager instead.

Debugging

  1. Missing Comments:

    • Check if the threadId matches the repository queries.
    • Verify the comment entity is mapped in Doctrine:
      php bin/console doctrine:mongo:schema:update --force
      
  2. Admin Panel Not Loading:

    • Ensure the symlink for Angular assets is correct:
      ln -s vendor/andchir/shopkeeper4-comments/frontend/projects/comments/src/app \
      public/admin/module/comments/src/app
      
    • Clear cache:
      php bin/console cache:clear
      
  3. Translation Issues:

    • The bundle uses Symfony’s translation system. Add translations to:
      config/translations/messages.en.yaml
      

Tips

  1. Custom Templates: Override default Twig templates by copying them from:

    vendor/andchir/shopkeeper4-comments/Resources/views/
    

    to:

    templates/Comments/Default/
    
  2. Rating System: Extend the rating logic in your entity:

    class ProductComment extends CommentAbstract {
        private $rating;
    
        public function getAverageRating() {
            // Custom logic to calculate average rating
        }
    }
    
  3. Performance:

    • Use pagination for comment lists:
      $comments = $commentRepository->findByThread('Product_123', 0, 10);
      
    • Cache frequent queries (e.g., top-rated products).
  4. Testing:

    • Mock CommentsManager in unit tests:
      $commentManager = $this->createMock(CommentsManager::class);
      $commentManager->method('getCommentsByThread')->willReturn([...]);
      
  5. Upgrades:

    • After updating the bundle, rebuild Angular assets:
      ng build comments --configuration production
      
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
emuniq/filament-browser-notifications
syriable/filament-translator
hungnm28/livewire-form
wenprise/eloquent
crudly/encrypted
fadion/bouncy
cuci/prototurk-sdk
gos/pubsub-router-bundle
cuci/prototurk-sdk-symfony
clementtalleu/easyadmin-markdown-bundle
codeflextech/permission-manager
karnoweb/livewire-datepicker
sayedenam/sayed-dashboard
milito/query-filter
apiboxsym/user-bundle
apiboxsym/health-check-bundle
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui