panakour/filament-flat-page

This is my package filament-flat-page

v0.0.2 2024-10-03 11:29 UTC

This package is auto-updated.

Last update: 2024-12-07 09:52:35 UTC


README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

FilamentFlatPage is a plugin for Filament that allows you to easily create and manage flat file pages with support for translations. It provides a simple way to add configurable, translatable pages to your Filament admin panel without the need for a database.

Features

  • Easily create flat file pages with customizable forms
  • Requires no database; data is stored in JSON files
  • Built-in multilingual support for creating translatable content
  • Seamless integration with Filament admin panel
  • Language switcher in the admin page header (integrates with Spatie Translatable, if available)

Installation

You can install the package via composer:

composer require panakour/filament-flat-page

Optionally, you can publish the config file with:

php artisan vendor:publish --tag="filament-flat-page-config"

This will create a config/filament-flat-page.php file where you can set your preferred options:

return [
    'locales' => ['en', 'fr', 'el'],
];

Optionally, you can publish the views using

php artisan vendor:publish --tag="filament-flat-page-views"

Usage

  1. Create a new page that extends FlatPage:
<?php

namespace App\Filament\Pages;

use Filament\Forms\Components\Section;
use Filament\Forms\Components\Tabs;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\TextInput;
use Panakour\FilamentFlatPage\Pages\FlatPage;

class Settings extends FlatPage
{

    protected static ?string $navigationIcon = 'heroicon-o-cog-6-tooth';
    protected static ?string $navigationGroup = 'Settings';
    protected static ?string $title = 'Settings';


    public function getSubheading(): ?string
    {
        return __('Manage your website settings');
    }

    public function getFileName(): string
    {
        return 'settings.json';
    }

    protected function getTranslatableFields(): array
    {
        return ['site_name', 'site_description', 'contact_address', 'contact_form_title', 'contact_form_content'];
    }

    protected function getFlatFilePageForm(): array
    {
        return [
            Tabs::make('Settings')
                ->tabs([
                    Tabs\Tab::make('General')
                        ->icon('heroicon-o-computer-desktop')
                        ->schema([
                            Section::make('App Settings')
                                ->icon('heroicon-o-computer-desktop')
                                ->schema([
                                    TextInput::make('site_name')
                                        ->required()
                                        ->hint('Translatable field.')
                                        ->hintIcon('heroicon-o-language')
                                        ->label('Site Name'),
                                    Textarea::make('site_description')
                                        ->hint('Translatable field.')
                                        ->hintIcon('heroicon-o-language')
                                        ->label('Site Description')
                                        ->rows(3),
                                ]),
                        ]),

                    Tabs\Tab::make('Contact')
                        ->icon('heroicon-o-envelope')
                        ->schema([
                            Section::make('Contact Information')
                                ->icon('heroicon-o-envelope')
                                ->schema([
                                    TextInput::make('contact_email')
                                        ->email()
                                        ->required()
                                        ->label('Contact Email'),
                                    TextInput::make('contact_phone')
                                        ->tel()
                                        ->label('Contact Phone'),
                                    Textarea::make('contact_address')
                                        ->hint('Translatable field.')
                                        ->hintIcon('heroicon-o-language')
                                        ->label('Address')
                                        ->rows(3),
                                ]),

                            Section::make('Contact Form')
                                ->schema([
                                    TextInput::make('contact_form_title')
                                        ->hint('Translatable field.')
                                        ->hintIcon('heroicon-o-language')
                                        ->label('Contact Form Title'),
                                    Textarea::make('contact_form_content')
                                        ->hint('Translatable field.')
                                        ->hintIcon('heroicon-o-language')
                                        ->label('Contact Form Content')
                                        ->rows(3),
                                ]),
                        ]),

                    Tabs\Tab::make('Social Networks')
                        ->icon('heroicon-o-heart')
                        ->schema([
                            Section::make('Social Media Links')
                                ->icon('heroicon-o-heart')
                                ->schema([
                                    TextInput::make('facebook')
                                        ->url()
                                        ->label('Facebook URL'),
                                    TextInput::make('twitter')
                                        ->url()
                                        ->label('Twitter URL'),
                                    TextInput::make('instagram')
                                        ->url()
                                        ->label('Instagram URL'),
                                    TextInput::make('linkedin')
                                        ->url()
                                        ->label('LinkedIn URL'),
                                ]),
                        ]),
                ])
                ->columnSpan('full'),
        ];
    }

}
  1. Define your form schema in the getFlatFilePageForm() method.

  2. Specify the translatable fields in the getTranslatableFields() method.

  3. Set the filename for storing the flat file data in the getFileName() method.

Using the FlatPage Facade

You can easily access your flat file data from anywhere in your application using the FlatPage facade:

use \Panakour\FilamentFlatPage\Facades\FilamentFlatPage;

// Get a non-translatable field
$contactEmail = FilamentFlatPage::get('settings.json', 'contact_email');

// Get a translatable field (uses current locale)
$siteName = FilamentFlatPage::get('settings.json', 'site_name');

// Get a translatable field in a specific locale
$siteNameGreek = FilamentFlatPage::get('settings.json', 'site_name', 'el');

// Get all
$allSettings = FilamentFlatPage::all('settings.json');

In Blade templates, you can use it like this:

<h1>{{ \Panakour\FilamentFlatPage\Facades\FilamentFlatPage::get("page.json", "title") }}</h1>
<p>Contact Email: {{ Panakour\FilamentFlatPage\Facades\FilamentFlatPage::get('settings.json', 'contact_email') }}</p>
<p>Site Name: {{ Panakour\FilamentFlatPage\Facades\FilamentFlatPage::get('settings.json', 'site_name', 'en') }}</p>

Testing

There is a FlatPageTest.php file where you can run to check if tests are passing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

Show your support

Give a ⭐️ if you like this project or find it helpful!

License

The MIT License (MIT). Please see License File for more information.