Replies: 2 comments 4 replies
-
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
class FilamentPeekBlockCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'filament-peek:block {name : The name of the block to create}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Create a new Filament block class and corresponding Blade view';
/**
* Execute the console command.
*/
public function handle()
{
$name = $this->argument('name');
$className = Str::studly($name);
$kebabName = Str::kebab($name);
// Create the block directory if it doesn't exist
$blockDirectory = app_path('Filament/Peek/Blocks');
if (!File::exists($blockDirectory)) {
File::makeDirectory($blockDirectory, 0755, true);
$this->info("Created directory: {$blockDirectory}");
}
// Create the view directory if it doesn't exist
$viewDirectory = resource_path('views/filament/components/blocks');
if (!File::exists($viewDirectory)) {
File::makeDirectory($viewDirectory, 0755, true);
$this->info("Created directory: {$viewDirectory}");
}
// Generate the block class
$blockPath = $blockDirectory . '/' . $className . '.php';
$blockContent = $this->getBlockTemplate($className, $name);
if (File::exists($blockPath)) {
if (!$this->confirm("Block class {$className} already exists. Overwrite?")) {
$this->info('Block class creation cancelled.');
return;
}
}
File::put($blockPath, $blockContent);
$this->info("Created block class: {$blockPath}");
// Generate the Blade view
$viewPath = $viewDirectory . '/' . $kebabName . '.blade.php';
$viewContent = $this->getViewTemplate();
if (File::exists($viewPath)) {
if (!$this->confirm("Blade view {$kebabName}.blade.php already exists. Overwrite?")) {
$this->info('Blade view creation cancelled.');
return;
}
}
File::put($viewPath, $viewContent);
$this->info("Created Blade view: {$viewPath}");
$this->newLine();
$this->info("✅ Filament block '{$name}' created successfully!");
$this->newLine();
$this->line("Block class: App\\Filament\\Peek\\Blocks\\{$className}");
$this->line("Blade view: filament.components.blocks.{$kebabName}");
}
/**
* Get the block class template.
*/
private function getBlockTemplate(string $className, string $name): string
{
return <<<PHP
<?php
namespace App\Filament\Peek\Blocks;
use Filament\Forms\Components\Builder\Block;
use Filament\Forms\Components\RichEditor;
class {$className}
{
public static function make(
string \$name = '{$name}',
string \$context = 'form',
): Block {
return Block::make(\$name)
->schema([
RichEditor::make('text'),
]);
}
}
PHP;
}
/**
* Get the Blade view template.
*/
private function getViewTemplate(): string
{
return <<<'BLADE'
@props(['text'])
{!! $text !!}
BLADE;
}
} |
Beta Was this translation helpful? Give feedback.
0 replies
-
Hi @mouse0270 , thanks for sharing, this is pretty cool! I'll consider adding this to the demo project, it makes a lot of sense. In Peek 3.x (for Filament v4), I've decided to deprecate the Builder Preview feature, and I'm still thinking of a replacement solution. I'll keep this in mind as I explore in this direction. |
Beta Was this translation helpful? Give feedback.
4 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
so, one of the features I used to love from Fabricator was the ability to use a command that would create a block and template that I could easily modify. It just felt easier then manually creating everything.
I've created the following command for the Blocks
php artisan filament-peek:block {name}
. I am hoping to also make one for layouts along with Maybe making a command that will update content sections with all of the blocks in Peek, that way its a little less the user as to manually create and instead can just run a few commands to get setup.Removed link to file, don't like that, thought it would show it as text
Beta Was this translation helpful? Give feedback.
All reactions