Homepage
Main navigation
Main content
Additional information
October Tricks
Login / Sign up
Quality Guidelines
About
GitHub
Propose new content
October CMS resources and help articles
Simple and to the point. Optimized by the community.
×
Login / Sign up
Quality Guidelines
About
GitHub
Propose new content
Edit trick
Changes will be published after manual review
Title
Give your trick a describing title. Do
not
start with «How to...».
Your trick
Keep it short and concise! Markdown is supported.
Create a `Acme\Plugin\Models\FileOption` model (with a db table) then add a `$belongsTo` relation to it: ```php public $belongsTo = [ 'file' => \System\Models\File::class ]; ``` In the plugin `boot` method, add a `$hasOne` relationship for the `File` model: ```php File::extend(function ($model) { $model->hasOne['file_option'] = [ \Acme\Plugin\Models\FileOption::class, 'key' => 'system_file_id' ]; // create the relation otherwise the file fields (title, description) // aren't saved if the extended fields are empty $model->bindEvent('model.beforeCreate', function() use ($model) { $model->file_option = new \Acme\Plugin\Models\FileOption; }); }); ``` Extend the `File` backend form with the fields you wish to add (also in the `boot()` method): ```php Event::listen('backend.form.extendFields', function ($widget) { if (!$widget->model instanceof \System\Models\File) return; if (!$widget->getController() instanceof \Acme\Plugin\Models\YourModel) return; $widget->addFields([ 'file_option[color]' => [ 'label' => 'Color', 'type' => 'colorpicker', ], // ... other fields ]); }); ``` One problem I've noticed is that if any of the extended fields were empty the title and description field weren't saved upon change. The solution for now was to create the relationship at file upload hence the code ```php $model->bindEvent('model.beforeCreate', function() use ($model){ $model->file_option = new \Acme\Plugin\Models\FileOption; }); ``` And for existing files I has to run this code once : ```php Event::listen('backend.form.extendFields', function ($widget) { foreach (File::get() as $file) { if ($file->file_option === null) { $file->file_option = new \Acme\Plugin\Models\FileOption; $file->save(); } } }); ```
References
Add additional online resources to your trick
+ Add reference
Topics
If your trick fits more than one topic select multiple. Select at least one.
Backend
Plugin Development
CMS
Twig
Themes
Deployment
Tags
You can use existing tags or create new ones. Add at least one.
Submit for review
Cancel
We use cookies to measure the performance of this website. Do you want to accept these cookies?
Accept
Decline