Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Content Resource Listing #22

Merged
merged 5 commits into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,45 @@ Password: password
## Protecting resources

Add the `IsProtectedResource` trait to your Filament resources to have them automatically obey `view <resource-name>` and `manage <resource-name>` permissions.

## Extending the Abstract Content

To add additional models or tables that extends the AbstractContent, you start by executing `php artisan make:filament-resource {Resource}`.

This command will generate a Resource file in your App\Filament\Resources folder. Add the next line in your class:
`use Portable\FilaCms\Filament\Resources\AbstractContentResource;`

Then go to your generated Resource file (e.g. `RecipeResource.php`), and change the `extends Resource` part to `extends AbstractContentResource`.

You should declare the proper model in your `$model` variable.

Then go to your model and add the following line:
`use Portable\FilaCms\Models\AbstractContentModel;`
Then change the `extends Model` to `extends AbstractContentModel`

Next is to create a Plugin class in your `app/Plugins` folder (or create the folder if it's not present yet). The content should look like this (change the appropriate values such as the Resource and the ID):

~~~
namespace App\Plugins;

use App\Filament\Resources\RecipeResource;
use Filament\Panel;
use Filament\Contracts\Plugin;

class RecipesPlugin implements Plugin
{
public function getId(): string
{
return 'filacms-recipes';
}

public function register(Panel $panel): void
{
$panel->resources([
RecipeResource::class
]);
}
}
~~~

Finally, add the plugin in your `app/config/fila-cms.php` file
21 changes: 17 additions & 4 deletions src/Filament/Resources/AbstractContentResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Portable\FilaCms\Filament\Resources;

use Filament\Forms\Form;
use Filament\Forms\Components\CheckboxList;
use Filament\Forms\Components\DatePicker;
use Filament\Forms\Components\Fieldset;
Expand All @@ -13,16 +14,16 @@
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Toggle;
use Filament\Forms\Components\View;
use Filament\Forms\Form;
use Filament\Tables;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Filters\TernaryFilter;

use FilamentTiptapEditor\TiptapEditor;
use FilamentTiptapEditor\Enums\TiptapOutput;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletingScope;
use Illuminate\Support\Str;
use Portable\FilaCms\Filament\Forms\Components\StatusBadge;
use Portable\FilaCms\Filament\Resources\AbstractContentResource\Pages;
use Portable\FilaCms\Filament\Resources\AbstractContentResource\RelationManagers;
Expand All @@ -32,6 +33,7 @@
use Portable\FilaCms\Models\Scopes\PublishedScope;
use Portable\FilaCms\Models\TaxonomyResource;
use RalphJSmit\Filament\SEO\SEO;
use Str;

class AbstractContentResource extends AbstractResource
{
Expand Down Expand Up @@ -65,7 +67,7 @@ public static function form(Form $form): Form
->columnSpanFull()
->required(),
static::tiptapEditor()->output(\FilamentTiptapEditor\Enums\TiptapOutput::Json),
SEO::make(['description']),
SEO::make(['description']),
]),
Tabs\Tab::make('Taxonomies')
->schema([
Expand Down Expand Up @@ -177,6 +179,17 @@ public static function table(Table $table): Table
])
->filters([
Tables\Filters\TrashedFilter::make(),
TernaryFilter::make('is_draft')
->label('Draft')
->attribute('is_draft')
->nullable()
->placeholder('All Records')
->falseLabel('Non-Drafts Only')
->trueLabel('Drafts Only')
->queries(
true: fn (Builder $query) => $query->where('is_draft', true),
false: fn (Builder $query) => $query->where('is_draft', false),
)
])
->actions([
Tables\Actions\DeleteAction::make(),
Expand Down
20 changes: 20 additions & 0 deletions src/Filament/Resources/PageResource/Pages/ListPages.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,28 @@

use Portable\FilaCms\Filament\Resources\AbstractContentResource\Pages\ListAbstractContentResources;
use Portable\FilaCms\Filament\Resources\PageResource;
use Illuminate\Database\Eloquent\Builder;

class ListPages extends ListAbstractContentResources
{
protected static string $resource = PageResource::class;

public function isTableSearchable(): bool
{
return true;
}

protected function applySearchToTableQuery(Builder $query): Builder
{
if (filled($searchQuery = $this->getTableSearch())) {
$searchQuery = '%' . $searchQuery . '%';
$query->where('title', 'LIKE', $searchQuery)
->orWhere('slug', 'LIKE', $searchQuery)
->orWhere('contents', 'LIKE', $searchQuery)
->orWhere('slug', 'LIKE', $searchQuery)
->orWhere('slug', 'LIKE', $searchQuery);
}

return $query;
}
}
21 changes: 21 additions & 0 deletions tests/Filament/PageResourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Portable\FilaCms\Models\TaxonomyTerm;
use Portable\FilaCms\Tests\TestCase;
use Spatie\Permission\Models\Role;
use RalphJSmit\Laravel\SEO\Models\SEO;

class PageResourceTest extends TestCase
{
Expand Down Expand Up @@ -77,6 +78,26 @@ public function test_can_create_record(): void
]);
}

public function test_can_save_seo(): void
{
$data = $this->generateModel(true);
$data['seo.description'] = 'Test Description';
$data['is_draft'] = 0;
$data['publish_at'] = now()->subday();
$data['expire_at'] = now()->addDay();

Livewire::test(TargetResource\Pages\CreatePage::class)
->fillForm($data)
->call('create')
->assertHasNoFormErrors();

// check last record
$model = TargetModel::orderBy('id', 'desc')->first();

$this->assertTrue($model->Seo instanceof SEO);

}

public function test_can_render_edit_page(): void
{
$data = $this->generateModel();
Expand Down
Loading