District Core Developer DocsDistrict Core Developer Docs
Developers
Boilerplate
Modules
Bitbucket
Developers
Boilerplate
Modules
Bitbucket
  • Modules

    • ABN
    • ActivityLog
    • AnalyticsApi
    • ApiConnector
    • BlockApi
    • CategoryApi
    • CloneApi
    • CommentApi
    • ContentApi
    • Core
    • Documents
    • EmbedApi
    • Event
    • ExportApi
    • FeatureApi
    • FormApi
    • GTM
    • GalleryApi
    • HelpApi
    • Hotspot
    • IdeaSurvey
    • ImportApi
    • InteractionsApi
    • Intercom
    • MailApi
    • MapApi
    • MapSurvey
    • MediaApi
    • MenuApi
    • MetaTagApi
    • NlpApi
    • NotificationApi
    • Page
    • ParentableContent
    • PaymentApi
    • PermissionsApi
    • Postcode
    • ReCaptcha
    • Redirects
    • Renderer
    • ReportApi
    • RestrictionApi
    • RevisionApi
    • SearchApi
    • Settings
    • ShareableApi
    • Slack
    • SlugApi
    • SubscribableApi
    • Survey
    • Team
    • TenantApi
    • TestApi
    • ThemeApi
    • Timeline
    • TranslationApi
    • Update
    • Users
    • VisualisationApi
    • WorkflowApi
    • Wysiwyg

Comment API

This module provides components and other necessary traits required by other modules / models to start posting a comment.

Adding comment to a model

  1. Any model that needs to have ability to add in a comment should implement the HasCommentsInterface contract and use the HasComments concern.
<?php

namespace Modules\__\Models;

use Modules\CommentApi\Concerns\HasComments;
use Modules\CommentApi\Contracts\HasCommentsInterface;

class Model extends BaseModel implements HasCommentsInterface
{
   use HasComments;

}
  1. The model should then define a getCommentableComponentActionUrls method that defines the action urls for the models comment.
/**
     * {@inheritdoc}
     */
    public function getCommentableComponentActionUrls(?Comment $comment = null): array
    {
        $baseCommentRouteName = SurveyRouteService::SUBMISSION_RELATIONSHIP.'.comment.';
        $baseParams = [SurveyRouteService::SUBMISSION_RESOURCE => $this->getFrontendKey()];
        $actionUrls = [
            'store' => route($baseCommentRouteName.'store', $baseParams, false),
        ];

        if (! $comment) {
            return $actionUrls;
        }

        $params = array_merge($baseParams, ['comment' => $comment->getFrontendKey()]);

        return array_merge($actionUrls, [
            'update' => route($baseCommentRouteName.'update', $params, false),
            'delete' => route($baseCommentRouteName.'delete', $params, false),
            'reply' => route($baseCommentRouteName.'reply', $params, false),
        ]);
    }
  1. Now the module should define appropriate routes and controllers for comment actions. The controller should extend FrontendCommentController provided by this module.
<?php

namespace Modules\Survey\Http\Controllers\Frontend;

use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Modules\CommentApi\Http\Controllers\Frontend\FrontendCommentController;
use Modules\CommentApi\Models\Comment;
use Modules\Survey\Models\Submission;

class FrontendSubmissionCommentController extends FrontendCommentController
{
    // Define actions here.
}
  1. Now the model can access the required commentable component props required for the frontend component via the getCommentableComponentAttribute.
    $model->commentable_component;
  1. A global commentable component is also registered via this module which can be used after model has been setup.

<component
        v-if="model.commentable_component"
        :is="model.commentable_component.component"
        v-bind="model.commentable_component.props"
/>

Adding author to a comment model

To add a author to a comment model, the model in this case Modules\Users\Models\User, implements HasAuthorComments and uses HasAuthorCommentsInterface.

Nesting

The comment module is configured to handle multiple level of nesting. See config('comment.max_comment_depth'). Please use wisely as it may cause too excessive queries. The default nesting is set to 3, but is completely customizable via ( available in HasComments trait ):

/**
     * {@inheritdoc}
     */
    public function getCommentableComponentCommentsDepth(): int
    {
        return CommentService::getCommentsDepth();
    }
Edit this page
Prev
CloneApi
Next
ContentApi