From 90da826b0f317098479b6761c3b6071c68477ef0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Mateusz=20Bro=C5=BCy=C5=84ski?= Date: Thu, 21 Nov 2024 14:14:44 +0100 Subject: [PATCH] Customized modal without publish switch --- .../3_adding-fields-to-the-create-modal.md | 87 ++++++++++++++++++- 1 file changed, 86 insertions(+), 1 deletion(-) diff --git a/docs/content/2_guides/3_adding-fields-to-the-create-modal.md b/docs/content/2_guides/3_adding-fields-to-the-create-modal.md index b95ba36c4..1859e05fa 100644 --- a/docs/content/2_guides/3_adding-fields-to-the-create-modal.md +++ b/docs/content/2_guides/3_adding-fields-to-the-create-modal.md @@ -16,7 +16,7 @@ Often you might want to add some mandatory fields to the create modal of your mo In our module controller we can override the `getCreateForm` method and add the fields to render: :::filename::: -`app/Http/Controllers/Twill/YourModuleController.php` +`app/Http/Controllers/Twill/BlogController.php` :::#filename::: ```phptorch @@ -168,3 +168,88 @@ Result: :::#tab::: :::#tabs::: + +# Removing publish switch from create modal + +In our module controller we can override the `indexData` to remove publish switch from modal. We can also override `setUpController` method to disable language select box used for making a translation active. + +:::filename::: +`app/Http/Controllers/Twill/BlogController.php` +:::#filename::: + +```phptorch +##CODE## +disablePublish(); + $this->disableBulkPublish(); + // $this->disableEditor(); # uncomment this to disable full editor + // $this->enableEditInModal(); # uncomment this to enable editing in modal + + } + + protected function formData($request) + { + return [ 'controlLanguagesPublication' => false ]; # disable select box to make language active + } +} +``` + +If we also want to make all languages active by default, we can override `prepareFieldsBeforeCreate` function in our module repository: + +:::filename::: +`app/Repositories/BlogRepository.php` +:::#filename::: + +```phptorch +##CODE## +model = $model; + } + + ### make all languages active for this model + public function prepareFieldsBeforeCreate($fields): array + { + foreach ($fields['languages'] as $key => $language) { + $fields['languages'][$key]['published'] = true; + } + + return parent::prepareFieldsBeforeCreate($fields); // @phpstan-ignore-line + } + +} +``` + +![Customized create modal without publish switch](./assets/customized-without-publish-switch.png)