Skip to content

Using field layout widget with custom "fields" #11468

Answered by brandonkelly
piotrpog asked this question in Ideas
Discussion options

You must be logged in to vote

Yes, this is possible by creating a “native field” class, which extends craft\fieldlayoutelements\BaseField.

There are a few built-in examples, such as craft\fieldlayoutelements\users\AddressField, which provides the “Addresses” field for user field layouts in Craft 4.

Once you have a native field class defined, you can register it to a field layout using the EVENT_DEFINE_NATIVE_FIELDS event:

use craft\events\DefineFieldLayoutFieldsEvent;
use craft\models\FieldLayout;
use yii\base\Event;

Event::on(
    FieldLayout::class,
    FieldLayout::EVENT_DEFINE_NATIVE_FIELDS,
    function(DefineFieldLayoutFieldsEvent $event) {
        /** @var FieldLayout $fieldLayout */
        $fieldLayout = $event

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@piotrpog
Comment options

@brandonkelly
Comment options

Answer selected by brandonkelly
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Ideas
Labels
None yet
2 participants