Using field layout widget with custom "fields" #11468
-
Is it possible to populate field layout designer with some custom "fields", not related to craft fields? I briefly looked through template files but didn't saw such an option. Let's say that i create a contact from plugin and have my own "form fields" which are not related to craft fields at all. But i still want to use form layout widget in control panel. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
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 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->sender;
if ($fieldLayout->type === 'target\element\type\class') {
$event->fields[] = MyField::class;
}
}
); (Note: for Craft 3, the event name is |
Beta Was this translation helpful? Give feedback.
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: