Homepage
Main navigation
Main content
Additional information
Login / Sign up
Quality Guidelines
About
GitHub
Propose new content
Winter 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.
Let's say you have a `Client` model with `hasMany` relation to an `Invoice` model and you would like to automatically assign the client relation widget when creating an Invoice for the Client in the create form. The Invoice model has this relation: ```php $belongsTo = [ 'client' => [Client::class], ]; ``` The Invoice form `fields.yaml` has a "client field" like below: ```yaml client: type: relation label: Client emptyOption: -- Select Client -- ``` Add this to your Invoice model: ```php public function __construct(array $attributes = []) { parent::__construct($attributes); $this->bindEvent('model.form.filterFields', function ($formWidget, $fields, $context) { $model = $formWidget->model; if ($context === 'create' && $model) { $model->client = $client = $formWidget->getController()->relationObject->getParent(); $fields->client->value = $client->id; } }); } ```
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