How to use Controller::asCpScreen() #13193
-
Hi, I'm wondering the best way to render the edit screen for a custom element type in the CP. In previous versions of Craft CMS I did most of the work in a template (e.g. I searched the Craft docs and here but didn't find much, except links to the What I have so far: public function actionEdit(?int $donationId = null, ?Donation $donation = null): Response
{
$this->requireCpRequest();
// ... some logic to get the donation being edited, check permissions, set some defaults, etc.
return $this->asCpScreen()
->title($isNewDonation ? 'Enter a new donation' : 'Edit Donation')
->addCrumb('Donations', 'donations')
->action('donations/donations/save')
->redirectUrl('donations')
->prepareScreen(
function(Response $response, string $containerId) use ($donation) {
$fieldLayout = $donation->getFieldLayout();
$form = $fieldLayout->createForm($donation, false, [
'registerDeltas' => true, // not sure about this
]);
/** @var Response|CpScreenResponseBehavior $response */
$response
->tabs($form->getTabMenu())
->content($form->render(false));
}
); I'm hoping I'm on the right track because I get a page rendered with a title, "Save" button, and tabs, but there are some problems such as the tab being empty until I click on a tab. I'm also hoping to be able to add the details sidebar (e.g. with Author, dates, and statuses) and other actions like "Delete". I don't mind digging through the code more to figure out some of this but would really appreciate any guidance you can give, thanks for your time! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
You're definitely on the right track, here! Yes, This section of the element type docs covers our recommendations for handling the edit view for your element. In short, letting Craft construct the view via the Beyond that, you can implement the The two |
Beta Was this translation helpful? Give feedback.
You're definitely on the right track, here! Yes,
asCpScreen()
is the preferred method for generating responses that target multiple contexts.This section of the element type docs covers our recommendations for handling the edit view for your element.
In short, letting Craft construct the view via the
elements/edit
controller action will provide virtually everything an element relies on.Beyond that, you can implement the
metadata()
,metaFieldsHtml()
, andprepareEditScreen()
methods to customize the screen and its output—without having to maintain your own controller action.The two
meta*
methods here are likely what you're looking for with respect to altering the contents of the sidebar. ✌️