-
Notifications
You must be signed in to change notification settings - Fork 554
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/2.x' into laravel-9-support
- Loading branch information
Showing
25 changed files
with
362 additions
and
183 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
--- | ||
pageClass: twill-doc | ||
--- | ||
|
||
# Field grouping | ||
|
||
Twill supports grouping fields in the database using a json column. | ||
|
||
Examples: | ||
|
||
- An address section existing out of a street, city and postal code field | ||
- An external link with Label, url and target | ||
- ... | ||
|
||
Each set of grouped fields requires a column in the database. The grouped fields can be translatable or not. | ||
|
||
## Migration and Model setup | ||
|
||
The migration for adding a grouped field can look like this (using external link as example): | ||
|
||
### Translatable | ||
|
||
```php | ||
## Add a migration | ||
Schema::table('blog_translations', function (Blueprint $table) { | ||
$table->json('external_link')->nullable(); | ||
}); | ||
|
||
## Update your models fillable | ||
class Blog extends Model { | ||
public $translatedAttributes = [ | ||
... | ||
'external_link' | ||
]; | ||
|
||
protected $fillable = [ | ||
... | ||
'external_link' | ||
]; | ||
} | ||
|
||
## Update your Translation model and add the cast | ||
public $casts = [ | ||
'external_link' => 'array', | ||
]; | ||
``` | ||
|
||
### Non translatable | ||
|
||
```php | ||
## Add a migration | ||
Schema::table('blogs', function (Blueprint $table) { | ||
$table->json('external_link')->nullable(); | ||
}); | ||
|
||
## Update your models fillable | ||
class Blog extends Model { | ||
protected $fillable = [ | ||
... | ||
'external_link' | ||
]; | ||
} | ||
``` | ||
|
||
## Field setup | ||
|
||
To store the fields you want into the json we have to update the repository: | ||
|
||
```php | ||
protected $fieldsGroups = [ | ||
'external_link' => [ | ||
'link_target', | ||
'link_url', | ||
'link_label', | ||
], | ||
]; | ||
|
||
# The below can be setup optionally, documented below. | ||
public $fieldsGroupsFormFieldNamesAutoPrefix = false; | ||
public $fieldsGroupsFormFieldNameSeparator = '_'; | ||
``` | ||
|
||
Finally in our model form we can add the fields: | ||
|
||
```php | ||
@formField('input', [ | ||
'name' => 'link_target', | ||
'label' => 'Link target', | ||
'translated' => true | ||
]) | ||
@formField('input', [ | ||
'name' => 'link_url', | ||
'label' => 'Link url', | ||
'translated' => true | ||
]) | ||
@formField('input', [ | ||
'name' => 'link_label', | ||
'label' => 'Link label', | ||
'translated' => true | ||
]) | ||
``` | ||
|
||
### Using the field name separator | ||
|
||
In the repository file you can setup the following parameters: | ||
|
||
```php | ||
public $fieldsGroupsFormFieldNamesAutoPrefix = true; | ||
public $fieldsGroupsFormFieldNameSeparator = '.'; // Default is _ | ||
``` | ||
|
||
This will automatically group/ungroup these fields based on the separator: | ||
|
||
```php | ||
@formField('input', [ | ||
'name' => 'external_link.link_target', | ||
'label' => 'Link target', | ||
'translated' => true | ||
]) | ||
@formField('input', [ | ||
'name' => 'external_link.link_url', | ||
'label' => 'Link url', | ||
'translated' => true | ||
]) | ||
@formField('input', [ | ||
'name' => 'external_link.link_label', | ||
'label' => 'Link label', | ||
'translated' => true | ||
]) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.