Skip to content

Commit

Permalink
Page extension improvements (lunarphp#1575)
Browse files Browse the repository at this point in the history
- Provides access to the page's record when customising headings.
- Adds the ability to extend relation pages

- [x] Docs
- [x] Tests

---------

Co-authored-by: glennjacobs <[email protected]>
  • Loading branch information
glennjacobs and glennjacobs authored Feb 22, 2024
1 parent fdb4b74 commit 3749223
Show file tree
Hide file tree
Showing 22 changed files with 172 additions and 45 deletions.
45 changes: 37 additions & 8 deletions docs/admin/extending/pages.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ An example of extending a create page.

```php
use Filament\Actions;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Form;
use Lunar\Admin\Support\Extending\CreatePageExtension;
use Lunar\Admin\Filament\Widgets;

Expand Down Expand Up @@ -113,8 +111,6 @@ An example of extending an edit page.

```php
use Filament\Actions;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Form;
use Lunar\Admin\Support\Extending\EditPageExtension;
use Lunar\Admin\Filament\Widgets;

Expand Down Expand Up @@ -212,8 +208,6 @@ An example of extending a list page.

```php
use Filament\Actions;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;
use Lunar\Admin\Support\Extending\ListPageExtension;
use Lunar\Admin\Filament\Widgets;

Expand Down Expand Up @@ -274,8 +268,6 @@ An example of extending a view page.

```php
use Filament\Actions;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;
use Lunar\Admin\Support\Extending\ViewPageExtension;

class MyViewExtension extends ViewPageExtension
Expand Down Expand Up @@ -308,6 +300,43 @@ class MyViewExtension extends ViewPageExtension
LunarPanel::registerExtension(new MyViewExtension, \Lunar\Admin\Filament\Resources\OrderResource\Pages\ManageOrder::class);
```

## RelationPageExtension

An example of extending a relation page.

```php
use Filament\Actions;
use Lunar\Admin\Support\Extending\RelationPageExtension;

class MyRelationExtension extends RelationPageExtension
{
public function heading($title): string
{
return $title . ' - Example';
}

public function subheading($title): string
{
return $title . ' - Example';
}

public function headerActions(array $actions): array
{
$actions = [
...$actions,
Actions\ActionGroup::make([
Actions\Action::make('Download PDF')
])
];

return $actions;
}
}

// Typically placed in your AppServiceProvider file...
LunarPanel::registerExtension(new MyRelationExtension, \Lunar\Admin\Filament\Resources\ProductResource\Pages\ManageProductMedia::class);
```

## Extending Pages In Addons

If you are building an addon for Lunar, you may need to take a slightly different approach when modifying forms, etc.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

namespace Lunar\Admin\Filament\Resources\BrandResource\Pages;

use Filament\Resources\Pages\ManageRelatedRecords;
use Filament\Resources\RelationManagers\RelationGroup;
use Filament\Support\Facades\FilamentIcon;
use Lunar\Admin\Filament\Resources\BrandResource;
use Lunar\Admin\Support\Pages\BaseManageRelatedRecords;
use Lunar\Admin\Support\RelationManagers\MediaRelationManager;

class ManageBrandMedia extends ManageRelatedRecords
class ManageBrandMedia extends BaseManageRelatedRecords
{
protected static string $resource = BrandResource::class;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

namespace Lunar\Admin\Filament\Resources\CollectionResource\Pages;

use Filament\Resources\Pages\ManageRelatedRecords;
use Filament\Resources\RelationManagers\RelationGroup;
use Filament\Support\Facades\FilamentIcon;
use Lunar\Admin\Filament\Resources\CollectionResource;
use Lunar\Admin\Filament\Resources\ProductResource\RelationManagers\CustomerGroupRelationManager;
use Lunar\Admin\Support\Pages\BaseManageRelatedRecords;
use Lunar\Admin\Support\RelationManagers\ChannelRelationManager;

class ManageCollectionAvailability extends ManageRelatedRecords
class ManageCollectionAvailability extends BaseManageRelatedRecords
{
protected static string $resource = CollectionResource::class;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
namespace Lunar\Admin\Filament\Resources\CollectionResource\Pages;

use Filament\Forms\Form;
use Filament\Resources\Pages\ManageRelatedRecords;
use Filament\Support\Facades\FilamentIcon;
use Filament\Tables;
use Filament\Tables\Table;
use Illuminate\Contracts\Support\Htmlable;
use Illuminate\Database\Eloquent\Model;
use Lunar\Admin\Filament\Resources\CollectionResource;
use Lunar\Admin\Support\Pages\BaseManageRelatedRecords;
use Lunar\Admin\Support\Tables\Actions\Collections\CreateChildCollection;

class ManageCollectionChildren extends ManageRelatedRecords
class ManageCollectionChildren extends BaseManageRelatedRecords
{
protected static string $resource = CollectionResource::class;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

namespace Lunar\Admin\Filament\Resources\CollectionResource\Pages;

use Filament\Resources\Pages\ManageRelatedRecords;
use Filament\Resources\RelationManagers\RelationGroup;
use Filament\Support\Facades\FilamentIcon;
use Lunar\Admin\Filament\Resources\CollectionResource;
use Lunar\Admin\Support\Pages\BaseManageRelatedRecords;
use Lunar\Admin\Support\RelationManagers\MediaRelationManager;

class ManageCollectionMedia extends ManageRelatedRecords
class ManageCollectionMedia extends BaseManageRelatedRecords
{
protected static string $resource = CollectionResource::class;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

namespace Lunar\Admin\Filament\Resources\DiscountResource\Pages;

use Filament\Resources\Pages\ManageRelatedRecords;
use Filament\Resources\RelationManagers\RelationGroup;
use Filament\Support\Facades\FilamentIcon;
use Lunar\Admin\Filament\Resources\DiscountResource;
use Lunar\Admin\Filament\Resources\ProductResource\RelationManagers\CustomerGroupRelationManager;
use Lunar\Admin\Support\Pages\BaseManageRelatedRecords;
use Lunar\Admin\Support\RelationManagers\ChannelRelationManager;

class ManageDiscountAvailability extends ManageRelatedRecords
class ManageDiscountAvailability extends BaseManageRelatedRecords
{
protected static string $resource = DiscountResource::class;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@

use Filament\Forms;
use Filament\Forms\Form;
use Filament\Resources\Pages\ManageRelatedRecords;
use Filament\Support\Facades\FilamentIcon;
use Filament\Tables;
use Filament\Tables\Table;
use Lunar\Admin\Filament\Resources\ProductResource;
use Lunar\Admin\Support\Pages\BaseManageRelatedRecords;
use Lunar\Models\Product;
use Lunar\Models\ProductAssociation;

class ManageProductAssociations extends ManageRelatedRecords
class ManageProductAssociations extends BaseManageRelatedRecords
{
protected static string $resource = ProductResource::class;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

namespace Lunar\Admin\Filament\Resources\ProductResource\Pages;

use Filament\Resources\Pages\ManageRelatedRecords;
use Filament\Resources\RelationManagers\RelationGroup;
use Filament\Support\Facades\FilamentIcon;
use Lunar\Admin\Filament\Resources\ProductResource;
use Lunar\Admin\Filament\Resources\ProductResource\RelationManagers\CustomerGroupRelationManager;
use Lunar\Admin\Support\Pages\BaseManageRelatedRecords;
use Lunar\Admin\Support\RelationManagers\ChannelRelationManager;

class ManageProductAvailability extends ManageRelatedRecords
class ManageProductAvailability extends BaseManageRelatedRecords
{
protected static string $resource = ProductResource::class;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
namespace Lunar\Admin\Filament\Resources\ProductResource\Pages;

use Filament\Forms;
use Filament\Resources\Pages\ManageRelatedRecords;
use Filament\Support\Facades\FilamentIcon;
use Filament\Tables;
use Filament\Tables\Table;
use Lunar\Admin\Filament\Resources\ProductResource;
use Lunar\Admin\Support\Pages\BaseManageRelatedRecords;
use Lunar\Models\Collection;

class ManageProductCollections extends ManageRelatedRecords
class ManageProductCollections extends BaseManageRelatedRecords
{
protected static string $resource = ProductResource::class;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

namespace Lunar\Admin\Filament\Resources\ProductResource\Pages;

use Filament\Resources\Pages\ManageRelatedRecords;
use Filament\Resources\RelationManagers\RelationGroup;
use Filament\Support\Facades\FilamentIcon;
use Lunar\Admin\Filament\Resources\ProductResource;
use Lunar\Admin\Support\Pages\BaseManageRelatedRecords;
use Lunar\Admin\Support\RelationManagers\MediaRelationManager;

class ManageProductMedia extends ManageRelatedRecords
class ManageProductMedia extends BaseManageRelatedRecords
{
protected static string $resource = ProductResource::class;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

use Filament\Forms;
use Filament\Forms\Form;
use Filament\Resources\Pages\ManageRelatedRecords;
use Filament\Support\Facades\FilamentIcon;
use Filament\Tables;
use Filament\Tables\Table;
use Lunar\Admin\Filament\Resources\ProductResource;
use Lunar\Admin\Support\Pages\BaseManageRelatedRecords;

class ManageProductVariants extends ManageRelatedRecords
class ManageProductVariants extends BaseManageRelatedRecords
{
protected static string $resource = ProductResource::class;

Expand Down
7 changes: 0 additions & 7 deletions packages/admin/src/Support/Extending/BaseExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,10 @@

namespace Lunar\Admin\Support\Extending;

use Filament\Tables\Table;

abstract class BaseExtension
{
public function headerActions(array $actions): array
{
return $actions;
}

public function extendTable(Table $table): Table
{
return $table;
}
}
10 changes: 10 additions & 0 deletions packages/admin/src/Support/Extending/CreatePageExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@

abstract class CreatePageExtension extends BaseExtension
{
public function heading($title): string
{
return $title;
}

public function subheading($title): ?string
{
return $title;
}

public function formActions(array $actions): array
{
return $actions;
Expand Down
10 changes: 10 additions & 0 deletions packages/admin/src/Support/Extending/EditPageExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@

abstract class EditPageExtension extends BaseExtension
{
public function heading($title, Model $record): string
{
return $title;
}

public function subheading($title, Model $record): ?string
{
return $title;
}

public function formActions(array $actions): array
{
return $actions;
Expand Down
11 changes: 7 additions & 4 deletions packages/admin/src/Support/Extending/ListPageExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@

namespace Lunar\Admin\Support\Extending;

use Filament\Tables\Table;

abstract class ListPageExtension extends BaseExtension
{
public function extendTable(Table $table): Table
public function heading($title): string
{
return $title;
}

public function subheading($title): ?string
{
return $table;
return $title;
}

public function relationManagers(array $managers): array
Expand Down
18 changes: 18 additions & 0 deletions packages/admin/src/Support/Extending/RelationPageExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Lunar\Admin\Support\Extending;

use Illuminate\Database\Eloquent\Model;

abstract class RelationPageExtension extends BaseExtension
{
public function heading($title, Model $record): string
{
return $title;
}

public function subheading($title, Model $record): ?string
{
return $title;
}
}
7 changes: 7 additions & 0 deletions packages/admin/src/Support/Extending/ResourceExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,17 @@

namespace Lunar\Admin\Support\Extending;

use Filament\Tables\Table;

abstract class ResourceExtension extends BaseExtension
{
public function getRelations(array $managers): array
{
return $managers;
}

public function extendTable(Table $table): Table
{
return $table;
}
}
11 changes: 9 additions & 2 deletions packages/admin/src/Support/Extending/ViewPageExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,17 @@

namespace Lunar\Admin\Support\Extending;

use Illuminate\Database\Eloquent\Model;

abstract class ViewPageExtension extends BaseExtension
{
public function headerActions(array $actions): array
public function heading($title, Model $record): string
{
return $title;
}

public function subheading($title, Model $record): ?string
{
return $actions;
return $title;
}
}
14 changes: 14 additions & 0 deletions packages/admin/src/Support/Pages/BaseManageRelatedRecords.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Lunar\Admin\Support\Pages;

use Filament\Resources\Pages\ManageRelatedRecords;

abstract class BaseManageRelatedRecords extends ManageRelatedRecords
{
use Concerns\ExtendsFooterWidgets;
use Concerns\ExtendsHeaderActions;
use Concerns\ExtendsHeaderWidgets;
use Concerns\ExtendsHeadings;
use \Lunar\Admin\Support\Concerns\CallsHooks;
}
Loading

0 comments on commit 3749223

Please sign in to comment.