Cms: [3.5-dev] Add event to register custom field layout elements

Created on 11 Jul 2020  路  4Comments  路  Source: craftcms/cms

Description

It would be cool if plugins/modules could bring their own custom field UI elements - could we get an event right here, please?
https://github.com/craftcms/cms/blob/10c476aedfecd32c9f8d7d426722f5ea5ec8f6b0/src/models/FieldLayout.php#L238-L250

enhancement extensibility

Most helpful comment

Forgot about this request, but we added a new event for defining custom UI elements in Craft 3.5 RC5!

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

Event::on(
    FieldLayout::class,
    FieldLayout::EVENT_DEFINE_UI_ELEMENTS,
    function(DefineFieldLayoutElementsEvent $event) {
        $event->elements[] = MyUiElement::class;
    }
);

Recommend extending BaseUiElement as that will take care of a lot of the leg work for you. Examples:

All 4 comments

Yeah will think about this, but I think the Template UI element is going to generally be a better solution.

@brandonkelly the template UI element cannot store settings (unless you overwrite it). In case someone wishes to have different options an event is a better solution.

Imagine the headline wouldn't have the ability to store a string/message and you would have to create a different template for each headline/hint/warning.

If there was an event I would instantly convert several custom fields that exists only for displaying purpose into ui elements.

Forgot about this request, but we added a new event for defining custom UI elements in Craft 3.5 RC5!

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

Event::on(
    FieldLayout::class,
    FieldLayout::EVENT_DEFINE_UI_ELEMENTS,
    function(DefineFieldLayoutElementsEvent $event) {
        $event->elements[] = MyUiElement::class;
    }
);

Recommend extending BaseUiElement as that will take care of a lot of the leg work for you. Examples:

Was this page helpful?
0 / 5 - 0 ratings

Related issues

brandonkelly picture brandonkelly  路  3Comments

lukebailey picture lukebailey  路  3Comments

richhayler picture richhayler  路  3Comments

leigeber picture leigeber  路  3Comments

angrybrad picture angrybrad  路  3Comments