Code Monkey home page Code Monkey logo

filament-drafts's Introduction

filament_drafts

Filament Drafts

Latest Version on Packagist Packagist PHP Version Total Downloads

This plugin adds the ability to manage your model's drafts and revisions in your filament resources.

It's a filament implementation for Laravel Drafts.

drafts-preview.mov

Installation

You can install the package with composer:

  composer require guava/filament-drafts

Usage

Model Trait

First make sure that you have correctly set-up a model to use drafts from Laravel Drafts.

At the very least you need to add the HasDrafts (please note this is a modified trait) trait to your model:

use Guava\FilamentDrafts\Concerns\HasDrafts;

class Post extends Model
{
    use HasDrafts;
}

and modify your migration:

return new class extends Migration
{
    public function up(): void
    {
        Schema::create('posts', function (Blueprint $table) {
            //...
            $table->drafts();
        };
    }
}

NOTE: If you use the HasDrafts trait from Laravel Drafts, make sure you have defined is_published in your $fillable properties.

After that, all you need to do is add a few traits to your resource and resource pages:

Resource Trait

Add the Draftable trait to your Resource:

use Guava\FilamentDrafts\Admin\Resources\Concerns\Draftable;

class PostResource extends Resource
{
    use Draftable;
}

Resource Pages

Add the respective Draftable trait to your Resource Pages: (Keep in mind that each page uses a different trait from another namespace)

Create Page

use Guava\FilamentDrafts\Admin\Resources\Pages\Create\Draftable;

class CreatePost extends CreateRecord
{
    use Draftable;
}

Edit Page

use Guava\FilamentDrafts\Admin\Resources\Pages\Edit\Draftable;

class EditPost extends EditRecord
{
    use Draftable;
}

List Page

use Guava\FilamentDrafts\Admin\Resources\Pages\List\Draftable;

class ListPosts extends ListRecords
{
    use Draftable;
}

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

License

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

Other packages

filament-drafts's People

Contributors

lukas-frey avatar mansoorkhan96 avatar ribesalexandre avatar albertobenavides avatar

Stargazers

Anibal Moradel avatar Chuck Rincón avatar TwoPinkyNoBrain avatar Dean Jansen avatar moneya avatar Ademir Mazer Jr [ Nuno ] avatar Manoj Halugona avatar Simtrami avatar Marvin Oßwald avatar Christos Veligkos avatar  avatar Rasul Chopurov avatar  avatar Marco Germani avatar entropie app avatar Vitalik avatar Milad avatar Son Banh avatar Baran Sekin avatar Gonras K avatar Jèfferson Gonçalves avatar  avatar Sebastian Popp avatar Dane Medussa avatar Ionut Marinescu avatar Bob Tantlinger avatar Sjoerd van Heijst avatar Chris Reed avatar Muhammad Ashraf Ishak avatar Torch avatar  avatar ChaairSoft avatar  avatar  avatar Diego Ibáñez avatar Gregor Vostrak avatar Tristan Bendixen avatar ADAM IBNU avatar Mostafa Khorashadi Zadeh avatar Mohamed Sherif avatar Mack Hankins avatar Mio noel dAvis avatar Steven Richardson avatar Ricardo Sawir avatar Joko Supriyanto avatar Muhammad Dahri avatar Bagaskara Wisnu Gunawan avatar  avatar Atila Silva avatar Vladislav Kambulov avatar O/I avatar  avatar Sadegh Barzegar avatar AriaieBOY avatar Kevin Jiang avatar Toby Evans avatar Saiful Azfar Ramlee avatar Sir Octopus avatar Tiago Fernandes avatar Martin Ro avatar  avatar milad sarli avatar Stepan Mocjak avatar  avatar

Watchers

 avatar Stepan Mocjak avatar

filament-drafts's Issues

Add support for Filament V3

Lets add support for filament v3.

I will send the PR, just need to know if there is anyone already working on v3 support? @LukasFreyCZ

Guava\FilamentDrafts\Admin\Resources\Pages\Edit\Draftable collision.

Hello. Great package, enjoying it so far, but on some of my multi lingual assets when using both Translatable and Draftable traits on the same EditRecord class - there is a collision error.

Code -

<?php

namespace App\Filament\Resources\PillarResource\Pages;

use App\Filament\Resources\PillarResource;
use Filament\Actions;
use Filament\Resources\Pages\EditRecord;
use Guava\FilamentDrafts\Admin\Resources\Pages\Edit\Draftable;
class EditPillar extends EditRecord
{
    use EditRecord\Concerns\Translatable;
    use Draftable;
    protected static string $resource = PillarResource::class;

    protected function getHeaderActions(): array
    {
        return [
            Actions\DeleteAction::make(),
            Actions\LocaleSwitcher::make(),

        ];
    }
}

Results in Trait method Guava\FilamentDrafts\Admin\Resources\Pages\Edit\Draftable::handleRecordUpdate has not been applied as App\Filament\Resources\PillarResource\Pages\EditPillar::handleRecordUpdate, because of collision with Filament\Resources\Pages\EditRecord\Concerns\Translatable::handleRecordUpdate

Some issues

  1. In the create view, clicking Save draft publish the current element model
  2. Unpublish button does not appear
  3. After creating a draft, clicking Publish makes all elements a draft and its impossible to publish again in the same element

HasDrafts functionality raised error from seeder

I added into laravel/framework 9 / filament/filamen 2 app guava/filament-drafts 1.2
but after I added
HasDrafts
to one of my models, I got error in app init seeder :

2023_03_21_145712_create_page_contents_table .................... 952ms FAIL

   TypeError 

  Oddvalue\LaravelDrafts\LaravelDrafts::getCurrentUser(): Return value must be of type Illuminate\Contracts\Auth\Authenticatable, null returned

  at vendor/oddvalue/laravel-drafts/src/LaravelDrafts.php:15
     11▕     protected bool $withDrafts = false;
     12▕ 
     13▕     public function getCurrentUser(): Authenticatable
     14▕     {
  ➜  15▕         return Auth::guard(config('drafts.auth.guard'))->user();
     16▕     }
     17▕ 
     18▕     public function previewMode(bool $previewMode = true): void
     19▕     {

      +10 vendor frames 
  11  database/seeders/pageContentsWithInitData.php:52
      App\Models\PageContent::save()

I supposed it was raised as seeders has no current logged user.
Any hint how this issue could be salved? Say, to turn off HasDrafts functionality temporary?
Telling the true I do not need HasDrafts functionality when data are inserted from seeder on app init...

Trait Method Collision Error

Description:
After adding the traits to all the resource pages I am getting the following error.

Note: It's happening because of Filament Translatable plugin.

Trait method Guava\FilamentDrafts\Admin\Resources\Pages\Create\Draftable::handleRecordCreation has not been applied as App\Filament\Resources\DeveloperResource\Pages\CreateDeveloper::handleRecordCreation, because of collision with Filament\Resources\Pages\CreateRecord\Concerns\Translatable::handleRecordCreation

Environment:

  • PHP version: 8.2
  • Laravel version: 11.14
  • Filament version: 3.2

Possible Solution:
To resolve the collision, consider renaming one of the conflicting methods..

Screenshot 2024-07-10 at 09 57 51

List Draft/Revisions in a separate Filament Tab

Problem

For some reason the table actions for draft/revisions table would not work. I have tried to add them separately and also tried to allow user to pass those actions but nothing works.

Solution

The work around for me was to use Tabs on ListRecord pages to filter records. This allowed the usage of existing actions. Other benefit of using tabs is:

  • Less space on page
  • Better UI
  • Less code to maintain, we can remove the list-draftable-records view file and the livewire components it loads
  • We dont need to define/pass which fields, actions and filters to load. Those will be loaded from resource itself.

The code we need:

use Illuminate\Database\Eloquent\Builder;
use Filament\Resources\Pages\ListRecords\Tab;

public function getTabs(): array
{
    return [
        'all' => Tab::make()
            ->modifyQueryUsing(fn (Builder $query) => $query->withoutDrafts()),
        'drafts' => Tab::make()
            ->modifyQueryUsing(fn (Builder $query) => $query->onlyDrafts()->where('is_current', true)),
    ];
}

@LukasFreyCZ I am planning to send this Pull request. I need to hear your thoughts.

Thanks :)

Incompatible with filament-nested-resources

Hi,
i found a couple of issues if used in tandem with filament-nested-resources.

  1. the handleRecordCreation needs to be using the getRelation Method
protected function handleRecordCreation(array $data): Model
    {
        $record = new ($this->getRelation()->getRelated())([
            ...$data,
            'is_published' => !$this->shouldSaveAsDraft,
        ]);

        if ($owner = $this->getOwnerRecord()) {
            $record = $this->associateRecordWithParent($record, $owner);
        }

        if (
            ($tenant = Filament::getTenant())
        ) {
            return $this->associateRecordWithTenant($record, $tenant);
        }
        $record->withoutRevision()->save();

        return $record;
    }
  1. SaveDraftAction needs to allow for CreateRelatedRecord as well as CreateRecord, i think even if you don't want to change the behaviour here it should throw an exception if its neither an instance of EditRecord nor CreateRecord.
protected function draft(): void
    {
        $livewire = $this->getLivewire();
        $livewire->shouldSaveAsDraft = true;

        if ($livewire instanceof EditRecord) {
            $livewire->save();
            return;
        }
        
        $livewire->create(); // don't know the side effects of just using create if it's not an instance of EditRecord but will see
    }

I'll update this issue as i find more.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.