From 9e439c8e0217c9a455f46ed4ba3ad62d9757191b Mon Sep 17 00:00:00 2001 From: Navneet Rai Date: Wed, 17 Sep 2025 10:07:02 -0400 Subject: [PATCH 1/7] Add comments section to Filament forms and update README --- README.md | 22 +++++++++++++++++++ resources/lang/en/comments.php | 1 + .../filament/forms/comments-section.blade.php | 12 ++++++++++ 3 files changed, 35 insertions(+) create mode 100644 resources/views/filament/forms/comments-section.blade.php diff --git a/README.md b/README.md index d9d92e2..da98ad4 100644 --- a/README.md +++ b/README.md @@ -120,6 +120,28 @@ protected function getHeaderActions(): array } ``` +4. Or directly in form schemas (Filament 4): + +> This works for Filament 4 with the new Schema system. + +```php +use Filament\Forms\Components\ViewField; + +public static function configure(Schema $schema): Schema +{ + return $schema + ->components([ + // Your other form fields... + + ViewField::make('comments_section') + ->view('commentions::filament.forms.comments-section') + ->viewData(fn ($livewire) => ['record' => $livewire->record ?? null]) + ->columnSpanFull() + ->hiddenLabel(), + ]); +} +``` + ### Subscription Management Commentions includes a subscription system that allows users to subscribe to receive notifications when new comments are added to a commentable resource. diff --git a/resources/lang/en/comments.php b/resources/lang/en/comments.php index caba4d3..b6d5486 100644 --- a/resources/lang/en/comments.php +++ b/resources/lang/en/comments.php @@ -3,6 +3,7 @@ return [ 'label' => 'Comments', 'no_comments_yet' => 'No comments yet.', + 'save_record_first' => 'Save the record first to enable comments.', 'user_avatar_alt' => 'User Avatar', diff --git a/resources/views/filament/forms/comments-section.blade.php b/resources/views/filament/forms/comments-section.blade.php new file mode 100644 index 0000000..b660c73 --- /dev/null +++ b/resources/views/filament/forms/comments-section.blade.php @@ -0,0 +1,12 @@ +
+

{{ __('commentions::comments.label') }}

+ + @if($record ?? null) + @livewire('commentions::comments', [ + 'record' => $record, + 'mentionables' => config('commentions.mentionables.default', \App\Models\User::all()) + ]) + @else +

{{ __('commentions::comments.save_record_first', ['default' => 'Save the record first to enable comments.']) }}

+ @endif +
From 3a68aca0cb696b60dcbea8818d25813517a2af8c Mon Sep 17 00:00:00 2001 From: Navneet Rai Date: Wed, 17 Sep 2025 10:08:27 -0400 Subject: [PATCH 2/7] Remove outdated note about Filament 4 schema system from README --- README.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/README.md b/README.md index da98ad4..e78fee6 100644 --- a/README.md +++ b/README.md @@ -122,8 +122,6 @@ protected function getHeaderActions(): array 4. Or directly in form schemas (Filament 4): -> This works for Filament 4 with the new Schema system. - ```php use Filament\Forms\Components\ViewField; From 1e2ea30b3e4ce0b882f4982f368196e41739016f Mon Sep 17 00:00:00 2001 From: Navneet Rai Date: Wed, 17 Sep 2025 10:59:53 -0400 Subject: [PATCH 3/7] Update README to clarify form schemas for Edit pages in Filament 4 and add note for View pages --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index e78fee6..06cafa9 100644 --- a/README.md +++ b/README.md @@ -120,7 +120,7 @@ protected function getHeaderActions(): array } ``` -4. Or directly in form schemas (Filament 4): +4. Or directly in form schemas for Edit pages (Filament 4): ```php use Filament\Forms\Components\ViewField; @@ -140,6 +140,8 @@ public static function configure(Schema $schema): Schema } ``` +**Note:** For View pages, continue using the infolist approach (option 1) as it works perfectly in that context. + ### Subscription Management Commentions includes a subscription system that allows users to subscribe to receive notifications when new comments are added to a commentable resource. From 6bc781d2a8112b5f641f235eb07ea627cd50ade4 Mon Sep 17 00:00:00 2001 From: Navneet Rai Date: Wed, 17 Sep 2025 13:42:39 -0400 Subject: [PATCH 4/7] Add readonly functionality to comments and reactions components --- README.md | 68 ++++++- resources/views/comment-list.blade.php | 1 + resources/views/comment.blade.php | 3 +- resources/views/comments-modal.blade.php | 1 + resources/views/comments.blade.php | 3 +- .../filament/forms/comments-section.blade.php | 3 +- resources/views/reactions.blade.php | 45 +++-- src/Filament/Actions/CommentsAction.php | 3 + src/Filament/Actions/CommentsTableAction.php | 3 + src/Filament/Concerns/IsReadonly.php | 20 ++ src/Livewire/Comment.php | 8 +- src/Livewire/CommentList.php | 2 + src/Livewire/Comments.php | 6 + src/Livewire/Concerns/IsReadonly.php | 13 ++ src/Livewire/Reactions.php | 7 + tests/Concerns/ReadonlyTraitsTest.php | 105 ++++++++++ tests/Filament/ReadonlyActionsTest.php | 73 +++++++ tests/Integration/ReadonlyIntegrationTest.php | 186 ++++++++++++++++++ tests/Livewire/ReadonlyCommentListTest.php | 81 ++++++++ tests/Livewire/ReadonlyCommentTest.php | 152 ++++++++++++++ tests/Livewire/ReadonlyCommentsTest.php | 120 +++++++++++ tests/Livewire/ReadonlyReactionsTest.php | 141 +++++++++++++ 22 files changed, 1022 insertions(+), 22 deletions(-) create mode 100644 src/Filament/Concerns/IsReadonly.php create mode 100644 src/Livewire/Concerns/IsReadonly.php create mode 100644 tests/Concerns/ReadonlyTraitsTest.php create mode 100644 tests/Filament/ReadonlyActionsTest.php create mode 100644 tests/Integration/ReadonlyIntegrationTest.php create mode 100644 tests/Livewire/ReadonlyCommentListTest.php create mode 100644 tests/Livewire/ReadonlyCommentTest.php create mode 100644 tests/Livewire/ReadonlyCommentsTest.php create mode 100644 tests/Livewire/ReadonlyReactionsTest.php diff --git a/README.md b/README.md index 06cafa9..789fd94 100644 --- a/README.md +++ b/README.md @@ -133,15 +133,81 @@ public static function configure(Schema $schema): Schema ViewField::make('comments_section') ->view('commentions::filament.forms.comments-section') - ->viewData(fn ($livewire) => ['record' => $livewire->record ?? null]) + ->viewData(fn ($livewire) => [ + 'record' => $livewire->record ?? null + ]) ->columnSpanFull() ->hiddenLabel(), ]); } ``` +To make the form comments readonly, pass the `readonly` flag in the viewData: + +```php +ViewField::make('comments_section') + ->view('commentions::filament.forms.comments-section') + ->viewData(fn ($livewire) => [ + 'record' => $livewire->record ?? null, + 'readonly' => true, // Enable readonly mode + ]) + ->columnSpanFull() + ->hiddenLabel(), +``` + **Note:** For View pages, continue using the infolist approach (option 1) as it works perfectly in that context. +### Readonly Mode + +You can make comments readonly by chaining the `readonly()` method on the action. In readonly mode: +- Users cannot add new comments +- Users cannot edit existing comments +- Users cannot delete comments +- Users cannot react to comments (reactions are displayed but not interactive) + +```php +// Make comments readonly for all actions +CommentsAction::make() + ->readonly() + ->mentionables(User::all()) + +CommentsTableAction::make() + ->readonly() + ->mentionables(User::all()) + +// You can also conditionally enable readonly mode +CommentsAction::make() + ->readonly(auth()->user()->cannot('create', Comment::class)) + ->mentionables(User::all()) +``` + +This is useful for scenarios like: +- Archived or closed records where no further comments should be allowed +- View-only access for certain user roles +- Historical comment viewing +- Audit trails where comments should be preserved but not modified + +#### Testing Readonly Functionality + +The package includes comprehensive tests for readonly functionality designed for Filament 4. To run the readonly-specific tests: + +```bash +# Run all readonly tests +./vendor/bin/pest tests/Livewire/Readonly* tests/Filament/ReadonlyActionsTest.php tests/Concerns/ReadonlyTraitsTest.php tests/Integration/ReadonlyIntegrationTest.php + +# Run specific test categories +./vendor/bin/pest tests/Livewire/ReadonlyCommentsTest.php # Comments component tests +./vendor/bin/pest tests/Livewire/ReadonlyCommentTest.php # Individual comment tests +./vendor/bin/pest tests/Livewire/ReadonlyReactionsTest.php # Reactions tests +./vendor/bin/pest tests/Filament/ReadonlyActionsTest.php # Filament 4 action tests +./vendor/bin/pest tests/Integration/ReadonlyIntegrationTest.php # Integration tests + +# Run trait tests +./vendor/bin/pest tests/Concerns/ReadonlyTraitsTest.php # IsReadonly trait tests +``` + +**Note**: These tests are specifically designed for Filament 4 and may not be compatible with Filament 3. + ### Subscription Management Commentions includes a subscription system that allows users to subscribe to receive notifications when new comments are added to a commentable resource. diff --git a/resources/views/comment-list.blade.php b/resources/views/comment-list.blade.php index a8c458a..1662e4c 100644 --- a/resources/views/comment-list.blade.php +++ b/resources/views/comment-list.blade.php @@ -19,6 +19,7 @@ class="comm:w-8 comm:h-8 comm:text-gray-400 comm:dark:text-gray-500" :key="$comment->getContentHash()" :comment="$comment" :mentionables="$mentionables" + :readonly="$this->isReadonly()" /> @endforeach diff --git a/resources/views/comment.blade.php b/resources/views/comment.blade.php index d574332..51f96a1 100644 --- a/resources/views/comment.blade.php +++ b/resources/views/comment.blade.php @@ -34,7 +34,7 @@ class="comm:text-xs comm:text-gray-300 comm:ml-1" @endif - @if ($comment->isComment() && Config::resolveAuthenticatedUser()?->canAny(['update', 'delete'], $comment)) + @if (!$this->isReadonly() && $comment->isComment() && Config::resolveAuthenticatedUser()?->canAny(['update', 'delete'], $comment))
@if (Config::resolveAuthenticatedUser()?->can('update', $comment)) isComment()) @endif diff --git a/resources/views/comments-modal.blade.php b/resources/views/comments-modal.blade.php index 83c12d5..bb85d3c 100644 --- a/resources/views/comments-modal.blade.php +++ b/resources/views/comments-modal.blade.php @@ -10,5 +10,6 @@ :per-page-increment="$perPageIncrement ?? null" :sidebar-enabled="$sidebarEnabled ?? true" :show-subscribers="$showSubscribers ?? true" + :readonly="$readonly ?? false" />
diff --git a/resources/views/comments.blade.php b/resources/views/comments.blade.php index a18cc3d..e7f6dd1 100644 --- a/resources/views/comments.blade.php +++ b/resources/views/comments.blade.php @@ -3,7 +3,7 @@
{{-- Main Comments Area --}}
- @if (Config::resolveAuthenticatedUser()?->can('create', Config::getCommentModel())) + @if (!$this->isReadonly() && Config::resolveAuthenticatedUser()?->can('create', Config::getCommentModel()))
{{-- tiptap editor --}}
@@ -40,6 +40,7 @@ :per-page="$perPage ?? 5" :load-more-label="$loadMoreLabel ?? 'Show more'" :per-page-increment="$perPageIncrement ?? null" + :readonly="$this->isReadonly()" />
diff --git a/resources/views/filament/forms/comments-section.blade.php b/resources/views/filament/forms/comments-section.blade.php index b660c73..6e5e6cb 100644 --- a/resources/views/filament/forms/comments-section.blade.php +++ b/resources/views/filament/forms/comments-section.blade.php @@ -4,7 +4,8 @@ @if($record ?? null) @livewire('commentions::comments', [ 'record' => $record, - 'mentionables' => config('commentions.mentionables.default', \App\Models\User::all()) + 'mentionables' => config('commentions.mentionables.default', \App\Models\User::all()), + 'readonly' => $readonly ?? false ]) @else

{{ __('commentions::comments.save_record_first', ['default' => 'Save the record first to enable comments.']) }}

diff --git a/resources/views/reactions.blade.php b/resources/views/reactions.blade.php index eeff02e..5690bd0 100644 --- a/resources/views/reactions.blade.php +++ b/resources/views/reactions.blade.php @@ -2,25 +2,39 @@ {{-- Inline buttons for existing reactions --}} @foreach ($this->reactionSummary as $reactionData) - + > + {{ $reactionData['reaction'] }} + {{ $reactionData['count'] }} + + @endif @endforeach {{-- Add Reaction Button --}} -
+ @if (!$this->isReadonly()) +