Skip to content

Commit

Permalink
feat(filament): added custom slug field (#712)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyrch authored Jul 6, 2024
1 parent 50e0bab commit 36d7c3e
Show file tree
Hide file tree
Showing 36 changed files with 86 additions and 437 deletions.
9 changes: 9 additions & 0 deletions app/Concerns/Filament/Actions/HasActionLogs.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ public function updateLog(Model $relatedModel, Model $pivot): void
public function failedLog(Throwable|string|null $exception): void
{
$this->actionLog->failed($exception);

// Filament notifications
$this->failureNotificationTitle(Str::limit($exception, 200));
$this->failure();
}

/**
Expand All @@ -95,6 +99,9 @@ public function finishedLog(): void
{
if (!$this->isFailedLog()) {
$this->actionLog->finished();

// Filament notifications
$this->success();
}
}

Expand All @@ -117,4 +124,6 @@ public function isFailedLog(): bool
{
return $this->actionLog->isFailed();
}


}
7 changes: 3 additions & 4 deletions app/Filament/Actions/BaseAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,14 @@ protected function setUp(): void
parent::setUp();

$this->requiresConfirmation();
$this->failure();

$this->afterFormValidated(function (BaseAction $action, $livewire) {
$this->before(function (BaseAction $action) {
$this->createActionLog($action);
$livewire->dispatch('updateAllRelationManager');
});

$this->after(function ($livewire) {
$this->after(function () {
$this->finishedLog();
$livewire->dispatch('updateAllRelationManager');
});

$this->modalWidth(MaxWidth::FourExtraLarge);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
use Filament\Forms\Form;
use Filament\Notifications\Actions\Action as NotificationAction;
use Filament\Notifications\Notification;
use Filament\Support\Enums\MaxWidth;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
Expand Down Expand Up @@ -66,8 +65,6 @@ protected function setUp(): void
$this->label(__('filament.actions.anime.backfill.name'));
$this->icon('heroicon-o-bars-4');

$this->modalWidth(MaxWidth::FourExtraLarge);

$this->authorize('create', Anime::class);

$this->action(fn (Anime $record, array $data) => $this->handle($record, $data));
Expand Down
1 change: 0 additions & 1 deletion app/Filament/BulkActions/BaseBulkAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use App\Concerns\Filament\Actions\HasActionLogs;
use App\Models\BaseModel;
use Filament\Forms\Form;
use Filament\Support\Enums\MaxWidth;
use Filament\Tables\Actions\BulkAction;
use Illuminate\Database\Eloquent\Collection;
Expand Down
40 changes: 40 additions & 0 deletions app/Filament/Components/Fields/Slug.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

namespace App\Filament\Components\Fields;

use Filament\Forms\Components\TextInput;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Validation\Rule;

/**
* Class Slug.
*/
class Slug extends TextInput
{
/**
* Initial setup for the field.
*
* @return void
*/
protected function setUp(): void
{
parent::setUp();

$this->required();
$this->unique(ignoreRecord: true);
$this->rules([
fn (?Model $record) => [
'required',
'max:192',
'alpha_dash',
$record !== null
? Rule::unique($record::class, $this->getName())
->ignore($record->getKey(), $record->getKeyName())
->__toString()
: Rule::unique($this->getModel(), $this->getName())->__toString(),
],
]);
}
}
2 changes: 1 addition & 1 deletion app/Filament/HeaderActions/BaseHeaderAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ protected function setUp(): void

$this->requiresConfirmation();

$this->afterFormValidated(function (BaseHeaderAction $action, $livewire) {
$this->before(function (BaseHeaderAction $action, $livewire) {
$this->createActionLog($action);
$livewire->dispatch('updateAllRelationManager');
});
Expand Down
12 changes: 0 additions & 12 deletions app/Filament/Resources/Admin/ActionLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,18 +98,6 @@ public static function getRecordSlug(): string
return 'action-logs';
}

/**
* Get the route key for the resource.
*
* @return string
*
* @noinspection PhpMissingParentCallCommonInspection
*/
public static function getRecordRouteKeyName(): string
{
return ActionLogModel::ATTRIBUTE_ID;
}

/**
* The form to the actions.
*
Expand Down
12 changes: 0 additions & 12 deletions app/Filament/Resources/Admin/Announcement.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,18 +91,6 @@ public static function getRecordSlug(): string
return 'announcements';
}

/**
* Get the route key for the resource.
*
* @return string
*
* @noinspection PhpMissingParentCallCommonInspection
*/
public static function getRecordRouteKeyName(): string
{
return AnnouncementModel::ATTRIBUTE_ID;
}

/**
* The form to the actions.
*
Expand Down
12 changes: 0 additions & 12 deletions app/Filament/Resources/Admin/Dump.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,18 +96,6 @@ public static function getRecordSlug(): string
return 'dumps';
}

/**
* Get the route key for the resource.
*
* @return string
*
* @noinspection PhpMissingParentCallCommonInspection
*/
public static function getRecordRouteKeyName(): string
{
return DumpModel::ATTRIBUTE_ID;
}

/**
* The form to the actions.
*
Expand Down
12 changes: 0 additions & 12 deletions app/Filament/Resources/Admin/Feature.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,18 +93,6 @@ public static function getRecordSlug(): string
return 'features';
}

/**
* Get the route key for the resource.
*
* @return string
*
* @noinspection PhpMissingParentCallCommonInspection
*/
public static function getRecordRouteKeyName(): string
{
return FeatureModel::ATTRIBUTE_ID;
}

/**
* The form to the actions.
*
Expand Down
12 changes: 0 additions & 12 deletions app/Filament/Resources/Admin/FeaturedTheme.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,18 +105,6 @@ public static function getRecordSlug(): string
return 'featured-themes';
}

/**
* Get the route key for the resource.
*
* @return string
*
* @noinspection PhpMissingParentCallCommonInspection
*/
public static function getRecordRouteKeyName(): string
{
return FeaturedThemeModel::ATTRIBUTE_ID;
}

/**
* The form to the actions.
*
Expand Down
12 changes: 0 additions & 12 deletions app/Filament/Resources/Auth/Permission.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,18 +96,6 @@ public static function getRecordSlug(): string
return 'permissions';
}

/**
* Get the route key for the resource.
*
* @return string
*
* @noinspection PhpMissingParentCallCommonInspection
*/
public static function getRecordRouteKeyName(): string
{
return PermissionModel::ATTRIBUTE_ID;
}

/**
* The form to the actions.
*
Expand Down
12 changes: 0 additions & 12 deletions app/Filament/Resources/Auth/Role.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,18 +104,6 @@ public static function getRecordSlug(): string
return 'roles';
}

/**
* Get the route key for the resource.
*
* @return string
*
* @noinspection PhpMissingParentCallCommonInspection
*/
public static function getRecordRouteKeyName(): string
{
return RoleModel::ATTRIBUTE_ID;
}

/**
* The form to the actions.
*
Expand Down
12 changes: 0 additions & 12 deletions app/Filament/Resources/Auth/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,18 +101,6 @@ public static function getRecordSlug(): string
return 'users';
}

/**
* Get the route key for the resource.
*
* @return string
*
* @noinspection PhpMissingParentCallCommonInspection
*/
public static function getRecordRouteKeyName(): string
{
return UserModel::ATTRIBUTE_ID;
}

/**
* The form to the actions.
*
Expand Down
15 changes: 15 additions & 0 deletions app/Filament/Resources/BaseResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,21 @@ public static function canGloballySearch(): bool
return false;
}

/**
* Get the route key for the resource.
*
* @return string
*
* @noinspection PhpMissingParentCallCommonInspection
*/
public static function getRecordRouteKeyName(): string
{
/** @var Model */
$model = static::getModel();

return (new $model)->getKeyName();
}

/**
* The index page of the resource.
*
Expand Down
12 changes: 0 additions & 12 deletions app/Filament/Resources/Discord/DiscordThread.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,18 +113,6 @@ public static function getRecordSlug(): string
return 'discord-thread';
}

/**
* Get the route key for the resource.
*
* @return string
*
* @noinspection PhpMissingParentCallCommonInspection
*/
public static function getRecordRouteKeyName(): string
{
return DiscordThreadModel::ATTRIBUTE_ID;
}

/**
* The form to the actions.
*
Expand Down
33 changes: 3 additions & 30 deletions app/Filament/Resources/Document/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use App\Enums\Http\Api\Filter\ComparisonOperator;
use App\Filament\Components\Columns\TextColumn;
use App\Filament\Components\Fields\Select;
use App\Filament\Components\Fields\Slug;
use App\Filament\Components\Infolist\TextEntry;
use App\Filament\Resources\BaseResource;
use App\Filament\Resources\Document\Page\Pages\CreatePage;
Expand All @@ -27,7 +28,6 @@
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Illuminate\Validation\Rule;

/**
* Class Page.
Expand Down Expand Up @@ -101,18 +101,6 @@ public static function getRecordSlug(): string
return 'pages';
}

/**
* Get the route key for the resource.
*
* @return string
*
* @noinspection PhpMissingParentCallCommonInspection
*/
public static function getRecordRouteKeyName(): string
{
return PageModel::ATTRIBUTE_ID;
}

/**
* The form to the actions.
*
Expand Down Expand Up @@ -141,25 +129,10 @@ public static function form(Form $form): Form
->live(true)
->afterStateUpdated(fn (Set $set, Get $get) => static::setSlug($set, $get)),

TextInput::make(PageModel::ATTRIBUTE_SLUG)
Slug::make(PageModel::ATTRIBUTE_SLUG)
->label(__('filament.fields.page.slug.name'))
->helperText(__('filament.fields.page.slug.help'))
->required()
->unique(PageModel::class, PageModel::ATTRIBUTE_SLUG, ignoreRecord: true)
->maxLength(192)
->regex('/^[\pL\pM\pN\/_-]+$/u')
->rules([
fn ($record) => [
'required',
'max:192',
'regex:/^[\pL\pM\pN\/_-]+$/u',
$record !== null
? Rule::unique(PageModel::class)
->ignore($record->getKey(), PageModel::ATTRIBUTE_ID)
->__toString()
: Rule::unique(PageModel::class)->__toString(),
]
]),
->regex('/^[\pL\pM\pN\/_-]+$/u'),

MarkdownEditor::make(PageModel::ATTRIBUTE_BODY)
->label(__('filament.fields.page.body.name'))
Expand Down
12 changes: 0 additions & 12 deletions app/Filament/Resources/List/External/ExternalEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,18 +102,6 @@ public static function getRecordSlug(): string
return 'external-entries';
}

/**
* Get the route key for the resource.
*
* @return string
*
* @noinspection PhpMissingParentCallCommonInspection
*/
public static function getRecordRouteKeyName(): string
{
return ExternalEntryModel::ATTRIBUTE_ID;
}

/**
* The form to the actions.
*
Expand Down
Loading

0 comments on commit 36d7c3e

Please sign in to comment.