beebmx / kirby-enum
Enum adds the ability to display and set enumeration content in the panel.
Fund package maintenance!
Requires
- php: ^8.4
- getkirby/composer-installer: ^1.2
Requires (Dev)
- getkirby/cms: ^6.0
- laravel/pint: ^1.0
- pestphp/pest: ^5.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Enum for Kirby
Enum adds the ability to display and set enumeration content in the panel.
Overview
- 1. Compatibility
- 2. Installation
- 3. Field properties
- 4. Usage in templates
- 5. Upgrading from 1.x
- 6. License
- 7. Credits
Compatibility
| Version | Kirby 4 | Kirby 5 | Kirby 6 | PHP |
|---|---|---|---|---|
| 2.x | ❌ | ❌ | ✅ | 8.4+ |
| 1.0.0 – 1.0.3 | ✅ | ✅ | ❌ | 8.2+¹ |
¹ The toEnum() field method of 1.x requires PHP 8.4.
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.
When you use as: tags, only the enum cases are accepted (accept: options).
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()->toCase(); $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()->toCase(); $network->toIcon();
Note
toCase() returns null when the field is empty, when the value is not a case of the enum
or when it's not an enum field in the blueprint.
Multiple values
When you display the field as checkboxes, multiselect or tags, use toCases() to retrieve an array of enum cases:
<?php foreach ($page->networks()->toCases() as $network) { echo $network->toLabel(); }
Note
toCases() returns an empty array when the field is empty, and skips the values that are not cases of the enum.
It uses the separator of the field when it's set (tags).
Nested fields
You can also use enum fields inside structure, object, blocks and layout fields:
fields: articles: type: structure fields: status: type: enum enum: App\Enums\Status
<?php foreach ($page->articles()->toStructure() as $article) { $status = $article->status()->toCase(); }
Note
Nested content fields only know their page (or site, file or user), not the field they are inside,
so the enum field is found by its name. If you use the same field name with different enum classes,
give them different names. A top level field always wins over a nested one with the same name.
Warning
The entries field is not supported, it only accepts a fixed list of Kirby field types.
For a list of enum values use a single enum field as multiselect, tags or checkboxes with
toCases(), or a structure field with an enum field inside.
Upgrading from 1.x
Kirby 6 comes with its own toEnum() field method,
and core field methods can't be replaced by plugins, so the field method of this plugin is now toCase():
<?php // 1.x $status = $page->enum()->toEnum(); // 2.x $status = $page->enum()->toCase();
toCase() reads the enum from the blueprint, so it works with plain enums and with methods like App\Enums\Status::published().
Kirby's toEnum() needs the enum class and only works with backed enums:
<?php $status = $page->enum()->toEnum(App\Enums\Status::class);
License
Licensed under the MIT.
Credits
- Fernando Gutierrez @beebmx
- All Contributors
