{$content}
{$title}
{$message}
HTML; } public static function fake(\Faker\Generator $faker): array { return [ 'type' => $faker->randomElement(['info', 'warning', 'error', 'success']), 'title' => $faker->sentence(3), 'message' => $faker->paragraph(), 'dismissible' => $faker->boolean(), ]; } } // Register and use EditorPhp::register(['alert' => AlertBlock::class]); $json = '{ "time": 1672852569662, "blocks": [ { "type": "alert", "data": { "type": "warning", "title": "Important Notice", "message": "Please read the documentation carefully.", "dismissible": true } } ], "version": "2.26.4" }'; echo EditorPhp::make($json)->render(); ``` -------------------------------- ### Laravel HTTP Response for Editor.js Source: https://context7.com/bumpcore/editor.php/llms.txt Return EditorPhp instances directly from controllers. The response format (JSON or HTML) is automatically determined by the request's `Accept` header, making it suitable for both API and browser clients. ```php namespace App\Http\Controllers; use App\Models\Post; use BumpCore\EditorPhp\EditorPhp; use Illuminate\Http\Request; class PostController extends Controller { public function show(Request $request, Post $post) { // Returns JSON if request expects JSON (API calls) // Returns rendered HTML otherwise (browser requests) return $post->content; } public function api(Post $post) { // Force JSON response return response()->json($post->content->toArray()); } public function preview(Request $request) { // Preview Editor.js content before saving $editor = EditorPhp::make($request->input('content')); return $editor; // Auto-responds with JSON or HTML } } // API route returns: // { // "time": 1672852569662, // "blocks": [...], // "version": "2.26.4" // } // Browser route returns rendered HTML ``` -------------------------------- ### Generate Fake Data for Custom Block Source: https://github.com/bumpcore/editor.php/blob/1.x/README.md Add a static `fake` method to your custom block class to enable fake data generation for that block when using Editor.php. ```php use BumpCore\EditorPhp\Block\Block; class MyCustomBlock extends Block { // ... public static function fake(\Faker\Generator $faker): array { $items = []; foreach (range(0, $faker->numberBetween(0, 10)) as $index) { $items[] = [ 'name' => fake()->name(), 'html' => $faker->randomHtml(), ]; } return [ 'text' => fake()->text(255), 'items' => $items, ]; } // ... } ``` -------------------------------- ### Check for Block Data Existence Source: https://github.com/bumpcore/editor.php/blob/1.x/README.md Utilize `has` method on the data object or the block instance to check if a specific data key exists. ```php $data->has('custom.data'); // or $this->has('custom.data'); ``` -------------------------------- ### Extend Built-in Image Block for Custom Uploads Source: https://context7.com/bumpcore/editor.php/llms.txt Extend the `Image` block to implement custom upload logic. Use a static method for temporary uploads and an instance method for processing and moving files to permanent storage. Register the custom block with `EditorPhp::register()`. ```php use BumpCore\EditorPhp\EditorPhp; use BumpCore\EditorPhp\Blocks\Image; use BumpCore\EditorPhp\Block\Block; class MyImageBlock extends Image { // Static method for temporary uploads (API endpoint) public static function uploadTemp(string $fileName = 'image'): array { $file = request()->file($fileName); $path = $file->store('temp/images', 'public'); return [ 'success' => 1, 'file' => [ 'url' => asset('storage/' . $path), ], ]; } // Instance method for final upload processing public function upload(): void { $tempUrl = $this->get('file.url'); // Move from temp to permanent storage $permanentUrl = $this->moveToPermament($tempUrl); // Update the block data $this->set('file.url', $permanentUrl); } private function moveToPermament(string $tempUrl): string { // Your upload logic here return str_replace('temp/', 'images/', $tempUrl); } } // Register the custom image block EditorPhp::register(['image' => MyImageBlock::class]); // Process uploads when saving $editor = EditorPhp::make($json); $editor->blocks->each(function(Block $block) { if ($block instanceof MyImageBlock) { $block->upload(); } }); // Save the updated JSON $finalJson = $editor->toJson(); ``` -------------------------------- ### Render Editor.js Content to HTML Source: https://context7.com/bumpcore/editor.php/llms.txt Render Editor.js content to HTML using the render() or toHtml() methods. The output can be cast directly to a string. Templates can be switched between Tailwind CSS and Bootstrap 5. ```php use BumpCore\EditorPhp\EditorPhp; $editor = EditorPhp::make($json); // Using the render() method echo $editor->render(); // Using toHtml() method echo $editor->toHtml(); // Or cast directly to string echo $editor; // Switch between template frameworks EditorPhp::useTailwind(); // Use Tailwind CSS templates (default) EditorPhp::useBootstrapFive(); // Use Bootstrap 5 templates // Check current template $template = EditorPhp::usingTemplate(); // Returns 'tailwind' or 'bootstrap-five' ``` -------------------------------- ### Register and Use Custom Macros in EditorPhp Source: https://github.com/bumpcore/editor.php/blob/1.x/README.md Define and use custom macros with `EditorPhp::macro()`. Macros allow you to extend the functionality of the editor instance, similar to Laravel's macroable traits. ```php use BumpCore\EditorPhp\EditorPhp; // Registering new macro. EditorPhp::macro( 'getParagraphs', fn () => $this->blocks->filter(fn (Block $block) => $block instanceof Paragraph) ); $editor = EditorPhp::make($json); // This will return a collection that only contains paragraphs. $paragraphs = $editor->getParagraphs(); ``` -------------------------------- ### Register Custom Blocks in EditorPhp Source: https://github.com/bumpcore/editor.php/blob/1.x/README.md Register custom block classes with `EditorPhp::register()`. You can choose to merge new blocks with existing ones or override them completely by passing `true` as the second argument. ```php use BumpCore\EditorPhp\EditorPhp; // This will merge without erasing already registered blocks. Other blocks will still remain with the recently registered `image` and `paragraph` blocks. EditorPhp::register([ 'image' => \Blocks\MyCustomImageBlock::class, 'paragraph' => \Blocks\MyCustomParagraphBlock::class, ]); // This will override already registered blocks. We now only have `image` and `paragraph` blocks. EditorPhp::register([ 'image' => \Blocks\MyCustomImageBlock::class, 'paragraph' => \Blocks\MyCustomParagraphBlock::class, ], true); ``` -------------------------------- ### Access and Modify Block Data Source: https://context7.com/bumpcore/editor.php/llms.txt Access and modify block data using dot notation through the `Data` object or block shortcuts. Data can be retrieved, set, or checked for existence. ```php use BumpCore\EditorPhp\EditorPhp; use BumpCore\EditorPhp\Block\Block; $editor = EditorPhp::make($json); $editor->blocks->each(function(Block $block) { // Method 1: Using the data object directly $data = $block->data; $text = $data->get('text'); $data->set('text', 'Modified text'); // Method 2: Invoking the data object $text = $data('text'); // Method 3: Using block shortcuts (recommended) $text = $block->get('text'); $block->set('text', 'New text'); // Check if data exists if ($block->has('caption')) { echo $block->get('caption'); } // Access nested data with dot notation $imageUrl = $block->get('file.url'); $block->set('file.url', 'https://example.com/new-image.jpg'); }); ``` -------------------------------- ### Convert EditorPhp Instance to Array Source: https://github.com/bumpcore/editor.php/blob/1.x/README.md Convert an `EditorPhp` instance to an associative array using the `toArray()` method. This is useful for inspecting or further processing the editor's content. ```php use BumpCore\EditorPhp\EditorPhp; $editor = EditorPhp::make($json); // This will return ['time' => ..., 'blocks' => [...], 'version' => '...'] $array = $editor->toArray(); ``` -------------------------------- ### Define Validation Rules for Block Data Source: https://github.com/bumpcore/editor.php/blob/1.x/README.md Implement the `rules` method within a custom block to define validation criteria for its data, leveraging Laravel's validation library. ```php use BumpCore\EditorPhp\Block\Block; class MyCustomBlock extends Block { // ... public function rules(): array { return [ 'text' => 'required|string|max:255', 'items' => 'sometimes|array', 'items.*.name' => 'required|string|max:255', 'items.*.html' => 'required|string|min:255', ]; } // ... } ``` -------------------------------- ### Render Editor.php Instance in Blade View Source: https://github.com/bumpcore/editor.php/blob/1.x/README.md An `EditorPhp` instance can be directly embedded within a Blade template using double curly braces for rendering. ```blade {{-- blog.show.blade.php --}}By: {$authorName}
"; } return "{$this->get('text')}
"; } } ``` -------------------------------- ### Define Validation Rules for EditorPhp Blocks Source: https://context7.com/bumpcore/editor.php/llms.txt Implement the `rules()` method in your custom block class to define validation rules using Laravel's validation syntax. This ensures data integrity for block content. Invalid blocks will have empty data. ```php use BumpCore\EditorPhp\Block\Block; class ValidatedBlock extends Block { public function rules(): array { return [ 'text' => 'required|string|max:1000', 'level' => 'integer|min:1|max:6', 'items' => 'sometimes|array', 'items.*.name' => 'required|string|max:255', 'items.*.url' => 'required|url', 'file.url' => 'required|url', 'withBorder' => 'boolean', ]; } public function render(): string { // If validation fails, $this->data will be empty if (!$this->has('text')) { return ''; // Skip rendering invalid blocks } return "{$this->get('text')}
"; } } // Usage $editor = EditorPhp::make($json); // Blocks with invalid data will have empty data arrays ``` -------------------------------- ### Cast Model Attribute to Editor.php Instance Source: https://github.com/bumpcore/editor.php/blob/1.x/README.md Use `EditorPhpCast` in your Eloquent model's `$casts` property to automatically cast a database attribute to an `EditorPhp` instance. ```php use BumpCore\EditorPhp\Casts\EditorPhpCast; use Illuminate\Database\Eloquent\Model; class Post extends Model { protected $casts = [ 'content' => EditorPhpCast::class, ]; } // ... $post = Post::find(1); // Content is `EditorPhp` instance in here. echo $post->content->render(); ``` -------------------------------- ### Manipulate Editor.js Blocks Source: https://context7.com/bumpcore/editor.php/llms.txt Access and manipulate blocks using Laravel collection methods. Blocks can be transformed, filtered, or accessed individually. ```php use BumpCore\EditorPhp\EditorPhp; use BumpCore\EditorPhp\Block\Block; use BumpCore\EditorPhp\Blocks\Paragraph; use BumpCore\EditorPhp\Blocks\Header; $editor = EditorPhp::make($json); // Access all blocks $blocks = $editor->blocks; // Transform paragraph text by stripping HTML tags $editor->blocks->transform(function(Block $block) { if ($block instanceof Paragraph) { $block->set('text', strip_tags($block->get('text'))); } return $block; }); // Filter to get only header blocks $headers = $editor->blocks->filter(fn(Block $block) => $block instanceof Header); // Get the first paragraph $firstParagraph = $editor->blocks->first(fn(Block $block) => $block instanceof Paragraph); // Count blocks by type $paragraphCount = $editor->blocks->filter(fn(Block $block) => $block instanceof Paragraph)->count(); ``` -------------------------------- ### Render Editor.js Content in Laravel Blade Views Source: https://context7.com/bumpcore/editor.php/llms.txt Easily render Editor.js content within your Blade templates. You can render the entire content, iterate over specific block types, or generate elements like a table of contents from header blocks. ```blade {{-- resources/views/posts/show.blade.php --}}{$this->get('text')}
"; } } // Allow all tags and attributes for entire block class UnsanitizedBlock extends Block { public function allows(): array|string { return '*'; // No sanitization } } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.