The following example shows how to add a custom edit template for a module.
For this, I created a new twill module with the following command:
php artisan twill:module pages --hasBlocks --hasMedias --hasSlug --hasPosition --hasRevisions
//resources/views/admin/pages/create.blade.php | |
//duplicate from resources/views/vendor/twill/partials/create.blade.php | |
@formField('input', [ | |
'name' => $titleFormKey ?? 'title', | |
'label' => ucfirst($titleFormKey ?? 'title'), | |
'translated' => $translateTitle ?? false, | |
'required' => true, | |
'onChange' => 'formatPermalink' | |
]) | |
@formField('select', [ | |
'name' => 'template', | |
'label' => ucfirst('template'), | |
'translated' => false, | |
'required' => true, | |
'options' => [ | |
[ | |
'value' => 'template1', | |
'label' => 'Template 1' | |
], | |
[ | |
'value' => 'template2', | |
'label' => 'Template 2' | |
] | |
] | |
]) | |
@if ($permalink ?? true) | |
@formField('input', [ | |
'name' => 'slug', | |
'label' => 'Permalink', | |
'translated' => true, | |
'ref' => 'permalink', | |
'prefix' => $permalinkPrefix ?? '' | |
]) | |
@endif |
//app/Models/Page.php | |
<?php | |
namespace App\Models; | |
use A17\Twill\Models\Behaviors\HasBlocks; | |
use A17\Twill\Models\Behaviors\HasSlug; | |
use A17\Twill\Models\Behaviors\HasMedias; | |
use A17\Twill\Models\Behaviors\HasRevisions; | |
use A17\Twill\Models\Behaviors\HasPosition; | |
use A17\Twill\Models\Behaviors\Sortable; | |
use A17\Twill\Models\Model; | |
class Page extends Model implements Sortable | |
{ | |
use HasBlocks, HasSlug, HasMedias, HasRevisions, HasPosition; | |
protected $fillable = [ | |
'published', | |
'title', | |
'description', | |
'template' | |
// 'position', | |
// 'public', | |
// 'featured', | |
// 'publish_start_date', | |
// 'publish_end_date', | |
]; | |
// uncomment and modify this as needed if you use the HasTranslation trait | |
// public $translatedAttributes = [ | |
// 'title', | |
// 'description', | |
// 'active', | |
// ]; | |
// uncomment and modify this as needed if you use the HasSlug trait | |
public $slugAttributes = [ | |
'title', | |
]; | |
// add checkbox fields names here (published toggle is itself a checkbox) | |
public $checkboxes = [ | |
'published' | |
]; | |
// uncomment and modify this as needed if you use the HasMedias trait | |
// public $mediasParams = [ | |
// 'cover' => [ | |
// 'default' => [ | |
// [ | |
// 'name' => 'landscape', | |
// 'ratio' => 16 / 9, | |
// ], | |
// [ | |
// 'name' => 'portrait', | |
// 'ratio' => 3 / 4, | |
// ], | |
// ], | |
// 'mobile' => [ | |
// [ | |
// 'name' => 'mobile', | |
// 'ratio' => 1, | |
// ], | |
// ], | |
// ], | |
// ]; | |
} |
//app/Http/Controllers/Admin/PagesController.php | |
<?php | |
namespace App\Http\Controllers\Admin; | |
use A17\Twill\Http\Controllers\Admin\ModuleController; | |
class PageController extends ModuleController | |
{ | |
protected $moduleName = 'pages'; | |
public function edit($id, $submoduleId = null) | |
{ | |
$this->submodule = isset($submoduleId); | |
$this->submoduleParentId = $id; | |
if ($this->getIndexOption('editInModal')) { | |
return $this->request->ajax() | |
? response()->json($this->modalFormData($submodule ?? $id)) | |
: redirect(moduleRoute($this->moduleName, $this->routePrefix, 'index')); | |
} | |
$this->setBackLink(); | |
$record = $this->form($submoduleId ?? $id); | |
$pageTemplate = $this->viewPrefix .'.'. $record['item']->template; | |
$view = collect([ | |
"$pageTemplate", | |
"$this->viewPrefix.form", | |
"twill::$this->moduleName.form", | |
"twill::layouts.form", | |
])->first(function ($view) { | |
return view()->exists($view); | |
}); | |
return view($view, $record); | |
} | |
} |
//resources/views/admin/pages/template1.blade.php | |
@extends('twill::layouts.form') | |
@section('contentFields') | |
@formField('input', [ | |
'name' => 'description', | |
'label' => 'Description', | |
'maxlength' => 100 | |
]) | |
@formField('input', [ | |
'name' => 'template', | |
'label' => 'Template', | |
'maxlength' => 100 | |
]) | |
@stop |
//migrations file | |
<?php | |
use Illuminate\Database\Migrations\Migration; | |
use Illuminate\Database\Schema\Blueprint; | |
class CreatePagesTables extends Migration | |
{ | |
public function up() | |
{ | |
Schema::create('pages', function (Blueprint $table) { | |
// this will create an id, a "published" column, and soft delete and timestamps columns | |
createDefaultTableFields($table); | |
// feel free to modify the name of this column, but title is supported by default (you would need to specify the name of the column Twill should consider as your "title" column in your module controller if you change it) | |
$table->string('title', 200)->nullable(); | |
// your generated model and form include a description field, to get you started, but feel free to get rid of it if you don't need it | |
$table->text('description')->nullable(); | |
$table->string('template')->nullable(); | |
// add those 2 colums to enable publication timeframe fields (you can use publish_start_date only if you don't need to provide the ability to specify an end date) | |
// $table->timestamp('publish_start_date')->nullable(); | |
// $table->timestamp('publish_end_date')->nullable(); | |
// use this column with the HasPosition trait | |
$table->integer('position')->unsigned()->nullable(); | |
}); | |
// remove this if you're not going to use any translated field, ie. using the HasTranslation trait. If you do use it, create fields you want translatable in this table instead of the main table above. You do not need to create fields in both tables. | |
Schema::create('page_translations', function (Blueprint $table) { | |
createDefaultTranslationsTableFields($table, 'page'); | |
// add some translated fields | |
// $table->string('title', 200)->nullable(); | |
// $table->text('description')->nullable(); | |
}); | |
// remove this if you're not going to use slugs, ie. using the HasSlug trait | |
Schema::create('page_slugs', function (Blueprint $table) { | |
createDefaultSlugsTableFields($table, 'page'); | |
}); | |
// remove this if you're not going to use revisions, ie. using the HasRevisions trait | |
Schema::create('page_revisions', function (Blueprint $table) { | |
createDefaultRevisionsTableFields($table, 'page'); | |
}); | |
} | |
public function down() | |
{ | |
Schema::dropIfExists('page_revisions'); | |
Schema::dropIfExists('page_translations'); | |
Schema::dropIfExists('page_slugs'); | |
Schema::dropIfExists('pages'); | |
} | |
} |