beebmx / kirby-enum
Enum adds the ability to display and set enumeration content in the panel.
Installs: 46
Dependents: 1
Suggesters: 0
Security: 0
Stars: 6
Watchers: 0
Forks: 0
Open Issues: 0
Type:kirby-plugin
pkg:composer/beebmx/kirby-enum
Requires
Requires (Dev)
- getkirby/cms: ^4.5|^5.0
- laravel/pint: ^1.0
- pestphp/pest: ^3.0
- spatie/ray: ^1.0
README
Enum for Kirby
Enum adds the ability to display and set enumeration content in the panel.
Overview
Installation
Download
Download and copy this repository to /site/plugins/kirby-enum.
Composer
composer require beebmx/kirby-enum
Field properties
| Name | Type | Default | Description |
|---|---|---|---|
| as | string | select |
Set the field you want to display and manipulate the enum data. |
| enum | string | null |
Set the namespace of the enum to populate the field. |
Example
fields: enum: label: Status type: enum enum: App\Enums\Status
Note
When you reference an enum it must be a fully qualified class name,
should be available with a namespace.
An enum class can be defined like this:
namespace App\Enums; enum Status: string { case Inactive = 'inactive'; case Active = 'active'; case Archived = 'archived'; }
Displaying field
When you use an enum field, it's just a wrapper around some Kirby fields. By default, it will display the value as a select field,
but you can change this behavior by setting the as property in your blueprint:
fields: enum: label: Status type: enum enum: App\Enums\Status as: radio
Here's the list of the available options for the enum field:
Note
You can set fields properties of every field to customize the behavior of the enum field.
Labels
By default, the text displayed in the options will be the name of the enum case, but you can customize this by implementing HasLabel interface in your enum class:
namespace App\Enums; use Beebmx\KirbyEnum\Contracts\HasLabel; enum Network: string implements HasLabel { case Facebook = 'facebook'; case Instagram = 'instagram'; case TikTok = 'tiktok'; case Mastodon = 'mastodon'; public function toLabel(): string { return match ($this) { self::Facebook => 'Facebook network', self::Instagram => 'Instagram network', self::TikTok => 'TikTok network', self::Mastodon => 'Mastodon network', }; } }
Usage in templates
Enum comes with a convenient field method to retrieve the proper enum case in your templates.
<?php $status = $page->enum()->toEnum(); $status->value; // 'active' $status->name; // 'Active'
If you have implemented custom methods in your enum class, you can call them as well:
<?php $network = $page->network()->toEnum(); $network->toIcon();
License
Licensed under the MIT.
Credits
- Fernando Gutierrez @beebmx
- All Contributors
