Skip to content

Commit c9f09c8

Browse files
committed
refactor: move global blocks resource to package with configurable registration
1 parent f72f8c0 commit c9f09c8

File tree

9 files changed

+141
-84
lines changed

9 files changed

+141
-84
lines changed

README.md

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -555,8 +555,51 @@ php artisan page-builder-plugin:make-block ContactForm --type=view --global
555555

556556
This will:
557557
- Create a global block class in `app/Filament/{panel}/Blocks/Globals/` directory
558-
- Automatically generate a Global Blocks resource for centralized management (on first global block creation)
559-
- Create the necessary database migration for storing global block configurations
558+
- Create a `Globals` block category for organization
559+
- Show instructions for enabling the Global Blocks resource
560+
561+
#### Enabling the Global Blocks resource
562+
563+
To manage global blocks in your Filament admin panel, you need to register the GlobalBlocksPlugin in your panel provider:
564+
565+
```php
566+
<?php
567+
568+
// In your AdminPanelProvider.php (or similar panel provider)
569+
570+
public function panel(Panel $panel): Panel
571+
{
572+
return $panel
573+
// ... other configurations
574+
->plugins([
575+
\Redberry\PageBuilderPlugin\GlobalBlocksPlugin::make(),
576+
// ... other plugins
577+
]);
578+
}
579+
```
580+
581+
This will add a "Global Blocks" resource to your admin navigation under the "Content Management" group.
582+
583+
#### Configuring the Global Blocks resource
584+
585+
You can configure the Global Blocks resource behavior through the config file:
586+
587+
```php
588+
<?php
589+
590+
// config/page-builder-plugin.php
591+
592+
return [
593+
// ... other configurations
594+
595+
'global_blocks' => [
596+
'enabled' => true, // Set to false to disable the resource
597+
'resource' => \Redberry\PageBuilderPlugin\Resources\GlobalBlockConfigResource::class, // Custom resource class
598+
],
599+
];
600+
```
601+
602+
To disable the Global Blocks resource completely, set `enabled` to `false`. To extend the resource with custom functionality, create your own resource class that extends the package's resource and specify it in the `resource` configuration.
560603

561604
#### How global blocks work
562605

@@ -592,7 +635,7 @@ class ContactForm extends BaseBlock
592635

593636
#### Global Blocks resource
594637

595-
When you create your first global block, a "Global Blocks" resource is automatically generated in your Filament admin panel. This resource allows you to:
638+
The "Global Blocks" resource is provided by the package and becomes available once you register the GlobalBlocksPlugin in your panel. This resource allows you to:
596639

597640
- View all available global blocks
598641
- Configure each block's field values

config/page-builder-plugin.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,9 @@
44
'block_model_class' => Redberry\PageBuilderPlugin\Models\PageBuilderBlock::class,
55

66
'polymorphic_relationship_name' => 'page_builder_blockable',
7+
8+
'global_blocks' => [
9+
'enabled' => true,
10+
'resource' => Redberry\PageBuilderPlugin\Resources\GlobalBlockConfigResource::class,
11+
],
712
];
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
return new class extends Migration
8+
{
9+
public function up()
10+
{
11+
Schema::create('global_block_configs', function (Blueprint $table) {
12+
$table->id();
13+
$table->string('name');
14+
$table->string('class_name')->unique();
15+
$table->json('configuration')->nullable();
16+
$table->timestamps();
17+
18+
$table->index('class_name');
19+
});
20+
}
21+
22+
public function down()
23+
{
24+
Schema::dropIfExists('global_block_configs');
25+
}
26+
};

src/Commands/CreatePageBuilderPluginBlockCommand.php

Lines changed: 1 addition & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,7 @@ public function handle(): int
121121
}
122122

123123
if ($isGlobal && isset($isFirstGlobalBlock) && $isFirstGlobalBlock) {
124-
$this->createGlobalBlocksResource();
125-
$this->publishGlobalBlockMigration();
124+
$this->info("To manage global blocks in Filament, add the GlobalBlocksPlugin to your panel:");
126125
}
127126

128127
return self::SUCCESS;
@@ -161,64 +160,4 @@ protected function isFirstGlobalBlock(): bool
161160

162161
return empty($existingBlocks);
163162
}
164-
165-
protected function createGlobalBlocksResource(): void
166-
{
167-
$resourceNamespace = $this->getClassNameSpaces('Resources');
168-
$resourceClass = 'GlobalBlocksResource';
169-
$resourceFullClass = $resourceNamespace . '\\' . $resourceClass;
170-
171-
$this->createFileFromStub(
172-
'global-blocks-resource',
173-
$this->appClassToPath($resourceFullClass),
174-
[
175-
'{{ class }}' => $resourceClass,
176-
'{{ namespace }}' => $resourceNamespace,
177-
'{{ resourceNamespace }}' => $resourceNamespace,
178-
]
179-
);
180-
181-
$pagesNamespace = $resourceNamespace . '\\' . $resourceClass . '\\Pages';
182-
183-
$this->createFileFromStub(
184-
'global-blocks-list-page',
185-
$this->appClassToPath($pagesNamespace . '\\ListGlobalBlocks'),
186-
[
187-
'{{ class }}' => 'ListGlobalBlocks',
188-
'{{ namespace }}' => $pagesNamespace,
189-
'{{ resourceClass }}' => $resourceClass,
190-
'{{ resourceNamespace }}' => $resourceNamespace,
191-
]
192-
);
193-
194-
$this->createFileFromStub(
195-
'global-blocks-edit-page',
196-
$this->appClassToPath($pagesNamespace . '\\EditGlobalBlock'),
197-
[
198-
'{{ class }}' => 'EditGlobalBlock',
199-
'{{ namespace }}' => $pagesNamespace,
200-
'{{ resourceClass }}' => $resourceClass,
201-
'{{ resourceNamespace }}' => $resourceNamespace,
202-
]
203-
);
204-
205-
$this->info("Created Global Blocks resource at: {$resourceFullClass}");
206-
$this->info('The resource has been automatically registered and will appear in your Filament panel navigation.');
207-
}
208-
209-
protected function publishGlobalBlockMigration(): void
210-
{
211-
$timestamp = now()->format('Y_m_d_His');
212-
$migrationName = 'create_global_block_configs_table';
213-
$migrationFile = database_path("migrations/{$timestamp}_{$migrationName}.php");
214-
215-
$this->createFileFromStub(
216-
'create_global_block_configs_table.php',
217-
$migrationFile,
218-
[]
219-
);
220-
221-
$this->info("Created migration: {$migrationFile}");
222-
$this->warn("Don't forget to run 'php artisan migrate' to create the global_block_configs table.");
223-
}
224163
}

src/GlobalBlocksPlugin.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace Redberry\PageBuilderPlugin;
4+
5+
use Filament\Contracts\Plugin;
6+
use Filament\Panel;
7+
use Redberry\PageBuilderPlugin\Resources\GlobalBlockConfigResource;
8+
9+
class GlobalBlocksPlugin implements Plugin
10+
{
11+
public static function make(): static
12+
{
13+
return app(static::class);
14+
}
15+
16+
public function getId(): string
17+
{
18+
return 'page-builder-global-blocks';
19+
}
20+
21+
public function register(Panel $panel): void
22+
{
23+
if (!config('page-builder-plugin.global_blocks.enabled', true)) {
24+
return;
25+
}
26+
27+
$resourceClass = config(
28+
'page-builder-plugin.global_blocks.resource',
29+
GlobalBlockConfigResource::class
30+
);
31+
32+
if (!class_exists($resourceClass)) {
33+
return;
34+
}
35+
36+
$panel->resources([
37+
$resourceClass,
38+
]);
39+
}
40+
41+
public function boot(Panel $panel): void
42+
{
43+
//
44+
}
45+
}

src/PageBuilderPluginServiceProvider.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,10 @@ public function configurePackage(Package $package): void
5252
}
5353
}
5454

55-
public function packageRegistered(): void {}
55+
public function packageRegistered(): void
56+
{
57+
$this->app->singleton(GlobalBlocksPlugin::class);
58+
}
5659

5760
public function packageBooted(): void
5861
{
@@ -140,6 +143,7 @@ protected function getMigrations(): array
140143
{
141144
return [
142145
'create_page_builder_blocks_table',
146+
'create_global_block_configs_table',
143147
];
144148
}
145149
}

stubs/global-blocks-resource.stub renamed to src/Resources/GlobalBlockConfigResource.php

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace {{ namespace }};
3+
namespace Redberry\PageBuilderPlugin\Resources;
44

55
use Filament\Forms;
66
use Filament\Forms\Form;
@@ -9,9 +9,9 @@
99
use Filament\Tables\Table;
1010
use Illuminate\Database\Eloquent\Builder;
1111
use Redberry\PageBuilderPlugin\Models\GlobalBlockConfig;
12-
use {{ resourceNamespace }}\{{ class }}\Pages;
12+
use Redberry\PageBuilderPlugin\Resources\GlobalBlockConfigResource\Pages;
1313

14-
class {{ class }} extends Resource
14+
class GlobalBlockConfigResource extends Resource
1515
{
1616
protected static ?string $model = GlobalBlockConfig::class;
1717

@@ -34,11 +34,6 @@ public static function form(Form $form): Form
3434
->required()
3535
->disabled(),
3636

37-
Forms\Components\TextInput::make('class_name')
38-
->label('Class Name')
39-
->required()
40-
->disabled(),
41-
4237
Forms\Components\Section::make('Block Configuration')
4338
->schema(function (?GlobalBlockConfig $record) {
4439
if (!$record || !class_exists($record->class_name)) {

stubs/global-blocks-edit-page.stub renamed to src/Resources/GlobalBlockConfigResource/Pages/EditGlobalBlock.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
<?php
22

3-
namespace {{ namespace }};
3+
namespace Redberry\PageBuilderPlugin\Resources\GlobalBlockConfigResource\Pages;
44

55
use Filament\Actions;
66
use Filament\Resources\Pages\EditRecord;
7-
use {{ resourceNamespace }}\{{ resourceClass }};
7+
use Redberry\PageBuilderPlugin\Resources\GlobalBlockConfigResource;
88

9-
class {{ class }} extends EditRecord
9+
class EditGlobalBlock extends EditRecord
1010
{
11-
protected static string $resource = {{ resourceClass }}::class;
11+
protected static string $resource = GlobalBlockConfigResource::class;
1212

1313
protected function getHeaderActions(): array
1414
{
@@ -104,4 +104,4 @@ protected function getRedirectUrl(): string
104104
{
105105
return $this->getResource()::getUrl('index');
106106
}
107-
}
107+
}

stubs/global-blocks-list-page.stub renamed to src/Resources/GlobalBlockConfigResource/Pages/ListGlobalBlocks.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
<?php
22

3-
namespace {{ namespace }};
3+
namespace Redberry\PageBuilderPlugin\Resources\GlobalBlockConfigResource\Pages;
44

55
use Filament\Actions;
66
use Filament\Resources\Pages\ListRecords;
7-
use {{ resourceNamespace }}\{{ resourceClass }};
7+
use Redberry\PageBuilderPlugin\Resources\GlobalBlockConfigResource;
88

9-
class {{ class }} extends ListRecords
9+
class ListGlobalBlocks extends ListRecords
1010
{
11-
protected static string $resource = {{ resourceClass }}::class;
11+
protected static string $resource = GlobalBlockConfigResource::class;
1212

1313
protected function getHeaderActions(): array
1414
{
@@ -32,4 +32,4 @@ public function refreshGlobalBlocks(): void
3232

3333
$this->redirect($this->getResource()::getUrl('index'));
3434
}
35-
}
35+
}

0 commit comments

Comments
 (0)