possibility to inject custom attributes of element types to field layout #10334
-
I am working on a custom element type which has custom attributes. currently to render this custom attributes i create tab headers and pass to twig and render tab contents in twig.
now i am thinking about a way to inject my custom attributes to field layout tabs so i can use i tried to put my custom attributes in separate custom.twig file and inject it in field layout via Templete element UI but it seems it only accepts project root templates -probably because of security concerns, and also i am not sure if my element is accessible this way to custom.twig file-. is there any way to programmatically attach element type's custom attributes to field layout? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
You can do this by registering your attributes’ fields as “standard fields” on the field layout, as mandatory layout elements. (They will be called “native fields” in Craft 4, but otherwise works the same.) You can do that with the EVENT_DEFINE_STANDARD_FIELDS event from your plugin’s use craft\events\DefineFieldLayoutFieldsEvent;
use craft\fieldlayoutelements\StandardTextField;
use craft\models\FieldLayout;
use yii\base\Event;
Event::on(
FieldLayout::class,
FieldLayout::EVENT_DEFINE_STANDARD_FIELDS,
function(DefineFieldLayoutFieldsEvent $event) {
/** @var FieldLayout $fieldLayout */
$fieldLayout = $event->sender;
if ($fieldLayout->type === MyElementType::class) {
$event->fields[] = [
'class' => StandardTextField::class,
'attribute' => 'width',
'mandatory' => true, // whether the field *must* be included in field layouts
'required' => true, // whether the field should show that it is required
'translatable' => true,
];
}
}
); That example is for a text field, using |
Beta Was this translation helpful? Give feedback.
-
@brandonkelly
|
Beta Was this translation helpful? Give feedback.
You can do this by registering your attributes’ fields as “standard fields” on the field layout, as mandatory layout elements. (They will be called “native fields” in Craft 4, but otherwise works the same.)
You can do that with the EVENT_DEFINE_STANDARD_FIELDS event from your plugin’s
init()
method: