Skip to content

Commit 90da826

Browse files
ordigitalifox
authored andcommitted
Customized modal without publish switch
1 parent 709768e commit 90da826

File tree

1 file changed

+86
-1
lines changed

1 file changed

+86
-1
lines changed

docs/content/2_guides/3_adding-fields-to-the-create-modal.md

Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Often you might want to add some mandatory fields to the create modal of your mo
1616
In our module controller we can override the `getCreateForm` method and add the fields to render:
1717

1818
:::filename:::
19-
`app/Http/Controllers/Twill/YourModuleController.php`
19+
`app/Http/Controllers/Twill/BlogController.php`
2020
:::#filename:::
2121

2222
```phptorch
@@ -168,3 +168,88 @@ Result:
168168

169169
:::#tab:::
170170
:::#tabs:::
171+
172+
# Removing publish switch from create modal
173+
174+
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.
175+
176+
:::filename:::
177+
`app/Http/Controllers/Twill/BlogController.php`
178+
:::#filename:::
179+
180+
```phptorch
181+
##CODE##
182+
<?php
183+
184+
namespace App\Http\Controllers\Twill;
185+
186+
use A17\Twill\Http\Controllers\Admin\NestedModuleController as BaseModuleController;
187+
use A17\Twill\Services\Forms\Fields\Input;
188+
use A17\Twill\Services\Forms\Fields\Wysiwyg;
189+
use A17\Twill\Services\Forms\Form;
190+
191+
class BlogController extends BaseModuleController
192+
{
193+
protected $moduleName = 'blogs';
194+
195+
public function getCreateForm(): Form
196+
{
197+
...
198+
}
199+
200+
protected function setUpController(): void
201+
{
202+
$this->disablePublish();
203+
$this->disableBulkPublish();
204+
// $this->disableEditor(); # uncomment this to disable full editor
205+
// $this->enableEditInModal(); # uncomment this to enable editing in modal
206+
207+
}
208+
209+
protected function formData($request)
210+
{
211+
return [ 'controlLanguagesPublication' => false ]; # disable select box to make language active
212+
}
213+
}
214+
```
215+
216+
If we also want to make all languages active by default, we can override `prepareFieldsBeforeCreate` function in our module repository:
217+
218+
:::filename:::
219+
`app/Repositories/BlogRepository.php`
220+
:::#filename:::
221+
222+
```phptorch
223+
##CODE##
224+
<?php
225+
226+
namespace App\Repositories;
227+
228+
use A17\Twill\Repositories\Behaviors\HandleTranslations;
229+
use A17\Twill\Repositories\Behaviors\HandleSlugs;
230+
use A17\Twill\Repositories\ModuleRepository;
231+
use App\Models\Category;
232+
233+
class BlogRepository extends ModuleRepository
234+
{
235+
use HandleTranslations, HandleSlugs;
236+
237+
public function __construct(Category $model)
238+
{
239+
$this->model = $model;
240+
}
241+
242+
### make all languages active for this model
243+
public function prepareFieldsBeforeCreate($fields): array
244+
{
245+
foreach ($fields['languages'] as $key => $language) {
246+
$fields['languages'][$key]['published'] = true;
247+
}
248+
249+
return parent::prepareFieldsBeforeCreate($fields); // @phpstan-ignore-line
250+
}
251+
252+
}
253+
```
254+
255+
![Customized create modal without publish switch](./assets/customized-without-publish-switch.png)

0 commit comments

Comments
 (0)