Advanced Guides
Content Transformers
Content Transformers are a way to re-structure, or enforce a specific structure, a Page's Content.
A Content Transformer is a class you can create to have a better defined structure to your content.
Creating a Content Transformer
To create a new Content Transformer, run the following command. The "Name" argument at the end can be an underscore-separated Name or a string literal such as "My Content Transformer."
1php artisan make:autoload-transformer NAME_OF_YOUR_CONTENT_TRANSFORMER
This will create a new file within your app/Filament/Content/Transformers/
directory, under the
name you've given, that looks like this:
1namespace App\Filament\Content\Transformers; 2 3use Mergeloop\Autoload\Filament\Content\AutoloadTransformer; 4 5class MyContentTransformer extends AutoloadTransformer 6{ 7 public function check($contentKey, $content): bool 8 { 9 return true;10 }11 12 public function transform(mixed $content): mixed13 {14 return $content;15 }16}
Within the check
method, this must return a boolean on whether this Transformer should run on the
given Content Block.
The transform
method is where you will take any content, that passed the `check` method, from the
Content Block and manipulated it into whatever structure you wish.
Full Example
In the below example, we have a Transformer that will only run on Content Blocks that have an array structure and will transform those into a comma-separated string.
1namespace App\Filament\Content\Transformers; 2 3use Mergeloop\Autoload\Filament\Content\AutoloadTransformer; 4 5class ArrayToStringTransformer extends AutoloadTransformer 6{ 7 public function check($contentKey, $content): bool 8 { 9 return \gettype($content) === 'array';10 }11 12 public function transform(mixed $content): mixed13 {14 return \implode(', ', $content);15 }16}