Statamic Builder
The Statamic Builder speeds up building Statamic sites. It offers a clear method to define sites, blueprints, fieldsets, collections, naviations and taxonomies using PHP classes. This approach enhances code readability and maintainability compared to writing YAML files.
For example, you can define a collection blueprint as follows:
<?php namespace App\Blueprints; use Tdwesten\StatamicBuilder\Blueprint;use Tdwesten\StatamicBuilder\FieldTypes\Section;use Tdwesten\StatamicBuilder\FieldTypes\Text;use Tdwesten\StatamicBuilder\FieldTypes\Tab; class PageBlueprint extends Blueprint{ public $title = 'Page'; public $handle = 'page'; public $hidden = false; public function registerTabs(): Array { return [ Tab::make('General', [ Section::make('General', [ Text::make('title') ->displayName('Title') ->instructions('The title of the page') ->localizable() ->required() ]), ]), ]; }}
Installation
You can install this addon with composer. Run the following command in your terminal to install the addon.
composer require tdwesten/statamic-builder
Blueprints and Fieldsets
This addon allows you to create blueprints and fieldsets in a fluent way. This makes it easier to define and maintain your blueprints and fieldsets.
How to create a blueprint
-
Create a new blueprint by running the following command for a page blueprint for example:
php artisan make:blueprint PageBlueprint -
Define your blueprint in the generated file. For example:
<?phpnamespace App\Blueprints;use Tdwesten\StatamicBuilder\Blueprint;use Tdwesten\StatamicBuilder\FieldTypes\Assets;use Tdwesten\StatamicBuilder\FieldTypes\Section;use Tdwesten\StatamicBuilder\FieldTypes\Text;use Tdwesten\StatamicBuilder\FieldTypes\Tab;class PageBlueprint extends Blueprint{public $title = 'Page';public $handle = 'page';public $hidden = false;public function registerTabs(): Array{return [Tab::make('General', [Section::make('General', [Text::make('title')->displayName('Title')->instructions('The title of the page')->required(),Assets::make('image')->displayName('Image')->maxItems(1)->instructions('The image of the page')->required(),]),]),];}} -
Register the blueprint in your
config/statamic/builder.php
file:<?phpreturn ['blueprints' => ['collections.pages' => ['page' => \App\Blueprints\PageBlueprint::class,],],]; -
That's it! You can now use your blueprint in your Statamic application.
How to create a fieldset
-
Create a new fieldset by running the following command for a hero fieldset for example:
php artisan make:fieldset HeroFieldset -
Define your fieldset in the generated file. For example add a title and image field to the hero fieldset:
<?phpnamespace App\Fieldsets;use Tdwesten\StatamicBuilder\Fieldset;use Tdwesten\StatamicBuilder\FieldTypes\Assets;use Tdwesten\StatamicBuilder\FieldTypes\Text;class HeroFieldset extends Fieldset{public function registerFields(): array{return [Text::make('title')->displayName('Title')->instructions('The title of the hero')->required(),Assets::make('image')->displayName('Image')->maxItems(1)->instructions('The image of the hero')->required(),];}} -
Register the fieldset in your
config/statamic/builder.php
file:<?phpreturn ['blueprints' => ['collections.pages' => ['page' => \App\Blueprints\PageBlueprint::class,],],'fieldsets' => [\App\Fieldsets\HeroFieldset::class,],]; -
Now you can use your fieldset in your blueprints. For example:
<?phpnamespace App\Blueprints;use App\Fieldsets\HeroFieldset;use Tdwesten\StatamicBuilder\Blueprint;class PageBlueprint extends Blueprint{public $title = 'Page';public $handle = 'page';public $hidden = false;public function registerTabs(): Array{return [Tab::make('General', [Section::make('General', [HeroFieldset::make('hero'),]),]),];}}
Supported Fieldtypes
All default Statamic field types are supported. You can create custom field types by utilizing the Field
class. For example to create a custom field type you can use the following code:
Field::make('custom_field') ->withAttributes([ 'type' => 'custom_type', 'display' => 'Custom Field', 'instructions' => 'This is a custom field', 'required' => true, 'options' => [ 'option1' => 'Option 1', 'option2' => 'Option 2', ], // Add more attributes here... ]);
How to register Collections and Taxonomies
This addon enables you to define collections and taxonomies in PHP classes, simplifying the process of defining and managing them.
How to register a collection
-
Generate a new collection blueprint, for example for an articles collection blueprint run the following command:
php artisan make:collection Articles -
Define your Articles collection blueprint in the generated file. For example, the file has all options available to define a collection blueprint. For example:
<?phpnamespace App\Collections;use Statamic\Facades\Site;use Tdwesten\StatamicBuilder\BaseCollection;class Articles extends BaseCollection{/*** Return the handle for the collection** Example: return 'blog';*/public static function handle(): string{return 'articles';}/*** Return the title for the collection** Example: return 'Blog';*/public function title(): string{return 'Articles';}// Add more options here...} -
Add the collection to the
config/statamic/builder.php
file:<?phpreturn ['collections' => [\App\Collections\Articles::class,],];
How to register a taxonomy
-
Generate a new taxonomy blueprint, for example for a categories taxonomy blueprint run the following command:
php artisan make:taxonomy Categories -
Define your taxonomy in the generated file. For example, the file has all options available to define a taxonomy. For example:
<?phpnamespace App\Taxonomies;use Statamic\Facades\Site;use Tdwesten\StatamicBuilder\BaseTaxonomy;class Categories extends BaseTaxonomy{/*** Return the handle for the taxonomy** Example: return 'tags';*/public static function handle(): string{// TODO: Change to your taxonomy handlereturn 'categories';}/*** Return the title for the taxonomy** Example: return 'Tags';*/public function title(): string{// TODO: Change to your taxonomy titlereturn 'Categories';}// Add more options here...} -
Add the taxonomy to the
config/statamic/builder.php
file:<?phpreturn ['taxonomies' => [\App\Taxonomies\Categories::class,],];public function title(): string{return 'Site Settings';}// Add more options here...}
-
Add the global set to the
config/statamic/builder.php
file:<?phpreturn ['globals' => [\App\Globals\SiteSettings::class,],];
How to create a Site
[!WARNING]
The sites are cached forever. When adding a site, you need to clear the cache.
-
Create a new site by running the following command:
php artisan make:site Blog -
Define your site in the generated file. For example:
<?phpnamespace App\Sites;use Tdwesten\StatamicBuilder\BaseSite;class Blog extends BaseSite{/*** Return the handle for the site** Example: return 'default';*/public function handle(): string{return 'blog';}/*** Return the handle for the site** Example: return 'Default';*/public function name(): string{return 'Blog';}/*** Return the base url for the site** Example: return '/';*/public function url(): string{return '/blog';}/*** Return the locale of the site** Example: return '/';*/public function locale(): string{return 'en_US';}/*** Return the array of extra attributes for the site** Example: return ['foo' => 'bar'];*/public function attributes(): array{return [];}} -
Register the Site in your
config/statamic/builder.php
file:<?phpreturn ['sites' => [\App\Sites\Blog::class],]; -
Clear the cache:
php artisan cache:clear