namespace MyVendor\FilamentBlog;
use Filament\Contracts\Plugin as PluginContract;
use Filament\Panel;
use Filament\Navigation\NavigationGroup;
class BlogPlugin implements PluginContract
{
protected bool $hasComments = true;
protected bool $hasTags = true;
protected bool $hasCategories = true;
protected string | null $urlPrefix = 'blog';
public static function make(): static
{
return app(static::class);
}
public static function get(): static
{
return filament(static::class);
}
public function withComments(bool $condition = true): static
{
$this->hasComments = $condition;
return $this;
}
public function withTags(bool $condition = true): static
{
$this->hasTags = $condition;
return $this;
}
public function withCategories(bool $condition = true): static
{
$this->hasCategories = $condition;
return $this;
}
public function urlPrefix(string | null $prefix): static
{
$this->urlPrefix = $prefix;
return $this;
}
public function getId(): string
{
return 'blog';
}
public function register(Panel $panel): void
{
$resources = [
Resources\PostResource::class,
];
if ($this->hasCategories) {
$resources[] = Resources\CategoryResource::class;
}
if ($this->hasTags) {
$resources[] = Resources\TagResource::class;
}
$panel
->resources($resources)
->pages([
Pages\BlogSettings::class,
])
->widgets([
Widgets\BlogStatsWidget::class,
])
->navigationGroups([
NavigationGroup::make('Blog')
->icon('heroicon-o-newspaper')
->collapsed(false),
]);
// Register render hooks
$panel->renderHook(
'panels::head.end',
fn (): string => view('filament-blog::head')->render()
);
}
public function boot(Panel $panel): void
{
// Register routes
Route::prefix($this->urlPrefix ?? 'blog')
->middleware(['web'])
->group(__DIR__.'/../routes/web.php');
}
public function hasComments(): bool
{
return $this->hasComments;
}
public function hasTags(): bool
{
return $this->hasTags;
}
public function hasCategories(): bool
{
return $this->hasCategories;
}
}