Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
49 changes: 46 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -555,8 +555,51 @@ php artisan page-builder-plugin:make-block ContactForm --type=view --global

This will:
- Create a global block class in `app/Filament/{panel}/Blocks/Globals/` directory
- Automatically generate a Global Blocks resource for centralized management (on first global block creation)
- Create the necessary database migration for storing global block configurations
- Create a `Globals` block category for organization
- Show instructions for enabling the Global Blocks resource

#### Enabling the Global Blocks resource

To manage global blocks in your Filament admin panel, you need to register the GlobalBlocksPlugin in your panel provider:

```php
<?php

// In your AdminPanelProvider.php (or similar panel provider)

public function panel(Panel $panel): Panel
{
return $panel
// ... other configurations
->plugins([
\Redberry\PageBuilderPlugin\GlobalBlocksPlugin::make(),
// ... other plugins
]);
}
```

This will add a "Global Blocks" resource to your admin navigation under the "Content Management" group.

#### Configuring the Global Blocks resource

You can configure the Global Blocks resource behavior through the config file:

```php
<?php

// config/page-builder-plugin.php

return [
// ... other configurations

'global_blocks' => [
'enabled' => true, // Set to false to disable the resource
'resource' => \Redberry\PageBuilderPlugin\Resources\GlobalBlockConfigResource::class, // Custom resource class
],
];
```

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.

#### How global blocks work

Expand Down Expand Up @@ -592,7 +635,7 @@ class ContactForm extends BaseBlock

#### Global Blocks resource

When you create your first global block, a "Global Blocks" resource is automatically generated in your Filament admin panel. This resource allows you to:
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:

- View all available global blocks
- Configure each block's field values
Expand Down
5 changes: 5 additions & 0 deletions config/page-builder-plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,9 @@
'block_model_class' => Redberry\PageBuilderPlugin\Models\PageBuilderBlock::class,

'polymorphic_relationship_name' => 'page_builder_blockable',

'global_blocks' => [
'enabled' => true,
'resource' => Redberry\PageBuilderPlugin\Resources\GlobalBlockConfigResource::class,
],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

some apps have multiple panel providers, move this definitions to plugin registration step, so in case someone has multiple panels and want global blocks in only one of them they can configure it without lot of work

];
26 changes: 26 additions & 0 deletions database/migrations/create_global_block_configs_table.php.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
public function up()
{
Schema::create('global_block_configs', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('class_name')->unique();
$table->json('configuration')->nullable();
$table->timestamps();

$table->index('class_name');
});
}

public function down()
{
Schema::dropIfExists('global_block_configs');
}
};
63 changes: 1 addition & 62 deletions src/Commands/CreatePageBuilderPluginBlockCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,7 @@ public function handle(): int
}

if ($isGlobal && isset($isFirstGlobalBlock) && $isFirstGlobalBlock) {
$this->createGlobalBlocksResource();
$this->publishGlobalBlockMigration();
$this->info('To manage global blocks in Filament, add the GlobalBlocksPlugin to your panel:');
}

return self::SUCCESS;
Expand Down Expand Up @@ -161,64 +160,4 @@ protected function isFirstGlobalBlock(): bool

return empty($existingBlocks);
}
Comment on lines +151 to +162
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i do not think u will need this if u will remove those two function regarding exporting migrations and block resource


protected function createGlobalBlocksResource(): void
{
$resourceNamespace = $this->getClassNameSpaces('Resources');
$resourceClass = 'GlobalBlocksResource';
$resourceFullClass = $resourceNamespace . '\\' . $resourceClass;

$this->createFileFromStub(
'global-blocks-resource',
$this->appClassToPath($resourceFullClass),
[
'{{ class }}' => $resourceClass,
'{{ namespace }}' => $resourceNamespace,
'{{ resourceNamespace }}' => $resourceNamespace,
]
);

$pagesNamespace = $resourceNamespace . '\\' . $resourceClass . '\\Pages';

$this->createFileFromStub(
'global-blocks-list-page',
$this->appClassToPath($pagesNamespace . '\\ListGlobalBlocks'),
[
'{{ class }}' => 'ListGlobalBlocks',
'{{ namespace }}' => $pagesNamespace,
'{{ resourceClass }}' => $resourceClass,
'{{ resourceNamespace }}' => $resourceNamespace,
]
);

$this->createFileFromStub(
'global-blocks-edit-page',
$this->appClassToPath($pagesNamespace . '\\EditGlobalBlock'),
[
'{{ class }}' => 'EditGlobalBlock',
'{{ namespace }}' => $pagesNamespace,
'{{ resourceClass }}' => $resourceClass,
'{{ resourceNamespace }}' => $resourceNamespace,
]
);

$this->info("Created Global Blocks resource at: {$resourceFullClass}");
$this->info('The resource has been automatically registered and will appear in your Filament panel navigation.');
}

protected function publishGlobalBlockMigration(): void
{
$timestamp = now()->format('Y_m_d_His');
$migrationName = 'create_global_block_configs_table';
$migrationFile = database_path("migrations/{$timestamp}_{$migrationName}.php");

$this->createFileFromStub(
'create_global_block_configs_table.php',
$migrationFile,
[]
);

$this->info("Created migration: {$migrationFile}");
$this->warn("Don't forget to run 'php artisan migrate' to create the global_block_configs table.");
}
}
45 changes: 45 additions & 0 deletions src/GlobalBlocksPlugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace Redberry\PageBuilderPlugin;

use Filament\Contracts\Plugin;
use Filament\Panel;
use Redberry\PageBuilderPlugin\Resources\GlobalBlockConfigResource;

class GlobalBlocksPlugin implements Plugin
{
public static function make(): static
{
return app(static::class);
}

public function getId(): string
{
return 'page-builder-global-blocks';
}

public function register(Panel $panel): void
{
if (! config('page-builder-plugin.global_blocks.enabled', true)) {
return;
}

$resourceClass = config(
'page-builder-plugin.global_blocks.resource',
GlobalBlockConfigResource::class
);

if (! class_exists($resourceClass)) {
return;
}

$panel->resources([
$resourceClass,
]);
}

public function boot(Panel $panel): void
{
//
}
}
6 changes: 5 additions & 1 deletion src/PageBuilderPluginServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ public function configurePackage(Package $package): void
}
}

public function packageRegistered(): void {}
public function packageRegistered(): void
{
$this->app->singleton(GlobalBlocksPlugin::class);
}

public function packageBooted(): void
{
Expand Down Expand Up @@ -140,6 +143,7 @@ protected function getMigrations(): array
{
return [
'create_page_builder_blocks_table',
'create_global_block_configs_table',
];
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace {{ namespace }};
namespace Redberry\PageBuilderPlugin\Resources;

use Filament\Forms;
use Filament\Forms\Form;
Expand All @@ -9,9 +9,9 @@
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Redberry\PageBuilderPlugin\Models\GlobalBlockConfig;
use {{ resourceNamespace }}\{{ class }}\Pages;
use Redberry\PageBuilderPlugin\Resources\GlobalBlockConfigResource\Pages;

class {{ class }} extends Resource
class GlobalBlockConfigResource extends Resource
{
protected static ?string $model = GlobalBlockConfig::class;

Expand All @@ -34,14 +34,9 @@ public static function form(Form $form): Form
->required()
->disabled(),

Forms\Components\TextInput::make('class_name')
->label('Class Name')
->required()
->disabled(),

Forms\Components\Section::make('Block Configuration')
->schema(function (?GlobalBlockConfig $record) {
if (!$record || !class_exists($record->class_name)) {
if (! $record || ! class_exists($record->class_name)) {
return [];
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<?php

namespace {{ namespace }};
namespace Redberry\PageBuilderPlugin\Resources\GlobalBlockConfigResource\Pages;

use Filament\Actions;
use Filament\Resources\Pages\EditRecord;
use {{ resourceNamespace }}\{{ resourceClass }};
use Redberry\PageBuilderPlugin\Resources\GlobalBlockConfigResource;

class {{ class }} extends EditRecord
class EditGlobalBlock extends EditRecord
{
protected static string $resource = {{ resourceClass }}::class;
protected static string $resource = GlobalBlockConfigResource::class;

protected function getHeaderActions(): array
{
Expand All @@ -27,7 +27,7 @@ protected function getHeaderActions(): array
protected function mutateFormDataBeforeFill(array $data): array
{
$blockClass = $this->record->class_name;

if (class_exists($blockClass) && $this->record->configuration) {
try {
if (method_exists($blockClass, 'getBaseBlockSchema')) {
Expand Down Expand Up @@ -56,7 +56,7 @@ protected function mutateFormDataBeforeSave(array $data): array
{
$configuration = [];
$blockClass = $this->record->class_name;

if (class_exists($blockClass)) {
try {
if (method_exists($blockClass, 'getBaseBlockSchema')) {
Expand All @@ -78,7 +78,7 @@ protected function mutateFormDataBeforeSave(array $data): array
}

$data['configuration'] = $configuration;

if (class_exists($blockClass)) {
try {
if (method_exists($blockClass, 'getBaseBlockSchema')) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<?php

namespace {{ namespace }};
namespace Redberry\PageBuilderPlugin\Resources\GlobalBlockConfigResource\Pages;

use Filament\Actions;
use Filament\Resources\Pages\ListRecords;
use {{ resourceNamespace }}\{{ resourceClass }};
use Redberry\PageBuilderPlugin\Resources\GlobalBlockConfigResource;

class {{ class }} extends ListRecords
class ListGlobalBlocks extends ListRecords
{
protected static string $resource = {{ resourceClass }}::class;
protected static string $resource = GlobalBlockConfigResource::class;

protected function getHeaderActions(): array
{
Expand Down