New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Event for adding custom tabs to element "Edit" screens at runtime #7258
Comments
Just added a new This will trigger right before Note that at this stage you are dealing with FieldLayoutTab and FieldLayoutElement models, not the raw HTML. It is up to each individual field layout element to define its rendered HTML, via formHtml(). Here’s an example of how to add a new tab to a field layout, with a text field, HR, and rendered template: use craft\elements\Entry;
use craft\events\CreateFieldLayoutFormEvent;
use craft\fieldlayoutelements\HorizontalRule;
use craft\fieldlayoutelements\StandardTextField;
use craft\fieldlayoutelements\Template;
use craft\models\FieldLayout;
use craft\models\FieldLayoutTab;
use yii\base\Event;
Event::on(
FieldLayout::class,
FieldLayout::EVENT_CREATE_FORM,
function(CreateFieldLayoutFormEvent $event) {
if ($event->element instanceof Entry) {
$event->tabs[] = new FieldLayoutTab([
'name' => 'My Tab',
'elements' => [
new StandardTextField([
'attribute' => 'myTextField',
'label' => 'My Text Field',
]),
new HorizontalRule(),
new Template([
'template' => '_layout-elements/info'
]),
],
]);
}
}
); You can tell if the form should be rendered statically via |
Craft 3.6 RC2 is out now with that new event. |
Craft 3.6 has now been officially released ✨ |
Just finished a hefty refactor of our Commerce plugin's tab UI to use this new event instead of our previous hacks. It's so much cleaner (and less bug-prone) now! @brandonkelly this was a really nice implementation. The FieldLayoutElement models are easy to work with, and all the abstractions feel like they're in just the right spots. Bravo, and thanks! 🙏🏼 |
p.s. To my future self (and maybe to @mattstein for docs)... 😃 The This is easily solved by using the And if you're getting really fancy, trying to render templates from other plugins, modules, or Craft, then you can invoke the {{ craft.app.view.renderTemplate(
'some-other-plugin/_includeThatNeedsToBeRenderedInCp',
{ ... },
'cp'
) | raw }} |
For plugin-provided UI, it’s probably better to just define your own custom field layout UI elements, by extending |
Continuing the conversation we started in the Commerce repo...
It'd be great to have a way for plugins to add custom tabs to element edit screens at runtime.
Something like...
For Commerce Orders, an additional "static" rendering would also potentially be needed:
It's already technically possible to add custom tabs, by hijacking the Twig context in template hooks... but it feels icky. Having an officially supported method would be much nicer. 😄
The text was updated successfully, but these errors were encountered: